JPWeb
JPWeb

Reputation: 195

Change ordering arguments for cross sells in Woocommerce

Trying to order my cross sells on the cart page to the menu order defined on the product. Ordered the specific products 1 2 3 4 5 etc. but it displays 5 4 3 2 1. So I thought to try change the order to asc/desc to fix that but this is the code ive got so far. What would be the way to solve this? Thanks!

add_filter( 'woocommerce_cross_sells_orderby', 'custom_cross_sells_orderby', 10, 1 );
function custom_cross_sells_orderby( $orderby, $order ){
    $orderby = 'menu_order';
    $order = 'DESC';
    return $orderby;
    return $order;
}

Upvotes: 2

Views: 1033

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254492

There is separated hooks for cross sells order by and order. The default behavior for cross sells orderis already "desc", so Try to use "asc" instead:

// Order by
add_filter( 'woocommerce_cross_sells_orderby', 'filter_cross_sells_orderby', 10, 1 );
function filter_cross_sells_orderby( $orderby ){
    return 'menu_order'; // Default is 'rand'
}

// Order
add_filter( 'woocommerce_cross_sells_order', 'filter_cross_sells_order', 10, 1 );
function filter_cross_sells_order( $order ){
    return 'asc'; // Default is 'desc'
}

Code goes in function.php file of your active child theme (or active theme). It should better work.

Upvotes: 3

Related Questions