• Resolved simonsilkyocean

    (@simonsilkyocean)


    When displaying the shop or categories by default sorting (?orderby=menu_order) the sort order isn’t by the menu order. Equally sorting by the latest doesn’t seem to work for us either as we have 7 new products added this morning which I would expect to be the first 7 in this page but they’re not. Any help would be great.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Actually the menu order attribute is assigned to the parent products, when you hide the parent product from catalog page and only showing the Variations now “Menu Order” attribute don’t have scope here, and thats why it cant filter the products anymore,

    What you can do is:

    1- Create a custom filed for Variations and added the order there for each variation.
    2- Create a custom Filter option for catalog and make it default.

    Create Custom Field for Variation like follows:

    add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
    add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
    add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );
    
    function variation_settings_fields( $loop, $variation_data, $variation ) {
        woocommerce_wp_text_input(
            array(
                'id'            => "variation_order{$loop}",
                'name'          => "variation_order[{$loop}]",
                'value'         => get_post_meta( $variation->ID, 'variation_order', true ),
                'label'         => __( 'Variation Order', 'woocommerce' ),
                'desc_tip'      => true,
                'description'   => __( 'Add Customer Order', 'woocommerce' ),
                'wrapper_class' => 'form-row form-row-full',
            )
        );
    }
    
    function save_variation_settings_fields( $variation_id, $loop ) {
        $text_field = $_POST['variation_order'][ $loop ];
    
        if ( ! empty( $text_field ) ) {
            update_post_meta( $variation_id, 'variation_order', esc_attr( $text_field ));
        }
    }
    
    function load_variation_settings_fields( $variation ) {     
        $variation['variation_order'] = get_post_meta( $variation[ 'variation_id' ], 'variation_order', true );
    
        return $variation;
    }

    Add Custom Filter option

    add_filter( 'woocommerce_catalog_orderby', 'aj_add_custom_sorting_options' );
    
    function aj_add_custom_sorting_options( $options ){
    	$options['variation_order'] =  __( 'Popularity', 'porto') ;
    	
    	return $options;
    
    }

    Showing result with filter selection

    add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
        function am_woocommerce_catalog_orderby( $args ) {
    
       if ( isset( $_GET['orderby'] ) && 'variation_order' === $_GET['orderby'] ) {
            $args['meta_key'] = 'variation_order';
            $args['orderby'] = 'meta_value_num';
            $args['order'] = 'asc';
        }
        return $args;
    }

    Hope that helps you.

    Thread Starter simonsilkyocean

    (@simonsilkyocean)

    That’s awesome – thanks @shoaib-fareed. Really appreciate the reply, I’d got as far as adding the custom field but wasn’t really sure how to use it for the variation order.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sort order is ignored when showing variations’ is closed to new replies.