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.
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';
}
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.
$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 :
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';
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.