skip to Main Content

I am using "Related Products for WooCommerce" plug-in 1.4.6 by WebToffee to display related products in each product page of an eCommerce website.
I am trying to display related products in the same order as they were entered for each product (a very specific order for each product) but to no avail. I have tried every sorting option of the plug-in.

Does anyone know how to achieve that with some additional PHP ?
For instance, for upsells, I am using the following code in the functions.php file of the theme, which works as expected :

// ORDER BY
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
function filter_woocommerce_upsells_orderby( $orderby ){
    return "none";
};

// ORDER
add_filter( 'woocommerce_upsells_order', 'filter_woocommerce_upsells_order', 10, 1 );
function filter_woocommerce_upsells_order($order){
    return 'menu_order'; 
};

Thank you in advance.

2

Answers


  1. Chosen as BEST ANSWER

    I think I have found the solution. It is a hack of the file "related.php" located in wp-content/plugins/wt-woocommerce-related-products/woocommerce/single-product

    This php uses the WP_Query() function with an array "$args". By default, the $args array looks as follows :

    array(5) { ["post_type"]=> string(7) "product" ["posts_per_page"]=> string(2) "20" ["orderby"]=> string(4) "rand" ["order"]=> string(3) "ASC" ["post__in"]=> array(14) { [10]=> int(12925) [5]=> int(13049) [12]=> int(13081) [4]=> int(12568) [1]=> int(12991) [13]=> int(12328) [3]=> int(12566) [2]=> int(12999) [9]=> int(13085) [8]=> int(12589) [11]=> int(13047) [6]=> int(12570) [7]=> int(12619) [0]=> int(12695) } }
    

    The trick is to make sure that the list of IDs in the "post__in" array are in the right order, and then to "force" "orderby" and "order" to the right values.

    • step 1 : insert $copy = $related; at line 285 so that the IDs don't get scrambled
    • step 2 : disable 'orderby' => $orderby, and 'order' => $order, at lines 300 & 301
    • step 3 : insert $args['orderby'] = 'post__in';$args['order'] = 'ASC'; at line 532

    Finally, $args array looks like that :

    array(5) { ["post_type"]=> string(7) "product" ["posts_per_page"]=> string(2) "20" ["post__in"]=> array(14) { [0]=> int(12695) [1]=> int(12991) [2]=> int(12999) [3]=> int(12566) [4]=> int(12568) [5]=> int(13049) [6]=> int(12570) [7]=> int(12619) [8]=> int(12589) [9]=> int(13085) [10]=> int(12925) [11]=> int(13047) [12]=> int(13081) [13]=> int(12328) } ["orderby"]=> string(8) "post__in" ["order"]=> string(3) "ASC" }
    

    This kind of hack is not very elegant, but this is the best I've found. BTW, I also tried the following in functions.php file, but it doesn't work as expected :

    // ORDER BY
    add_filter( 'wt_related_products_orderby', 'filter_woocommerce_wt_related_orderby', 10, 1 );
    function filter_woocommerce_wt_related_orderby( $orderby ){
        return "post__in";
    }
    // ORDER
    add_filter( 'wt_related_products_order', 'filter_woocommerce_wt_related_order', 10, 1 );
    function filter_woocommerce_wt_related_order($order){
        return 'ASC'; 
    }
    

  2. Please check if this helps.

    // ORDER BY
    add_filter( 'wt_related_products_orderby', 'filter_woocommerce_wt_related_orderby', 10, 1 );
    function filter_woocommerce_wt_related_orderby( $orderby ){
        return "none";
    }
    
    add_filter( 'wt_related_products_order', 'filter_woocommerce_wt_related_order', 10, 1 );
    function filter_woocommerce_wt_related_order($order){
        return 'menu_order'; 
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search