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
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 :
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.
$copy = $related;
at line 285 so that the IDs don't get scrambled'orderby' => $orderby,
and'order' => $order,
at lines 300 & 301$args['orderby'] = 'post__in';$args['order'] = 'ASC';
at line 532Finally, $args array looks like that :
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 :
Please check if this helps.