One Hour
One Hour

Reputation: 17

Sorting of Woocommerce cross-sells

i want to sort products in woocommerce cross-sells by date or some other parametres. Now products are showing in alphabetical order. I have found one solution:

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

But when i add this code in me functions.php error 505 appears. May be do you know why? Or may be you do you know other solution? Thank you.

Upvotes: 1

Views: 129

Answers (1)

BhAvik Gajjar
BhAvik Gajjar

Reputation: 458

simply add the following code in the file called functions.php in your theme folder:

<?php
// Filters

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
add_filter( 'woocommerce_cross_sells_orderby', 'custom_woocommerce_catalog_orderby' );

 // Apply custom args to main query
function custom_woocommerce_get_catalog_ordering_args( $args ) {
    $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

    if ( 'oldest_to_recent' == $orderby_value ) {
        $args['orderby'] = 'date';
        $args['order'] = 'ASC';
    }

    return $args;
}

// Create new sorting method
function custom_woocommerce_catalog_orderby( $sortby ) {

    $sortby['oldest_to_recent'] = __( 'Oldest to most recent', 'woocommerce' );

    return $sortby;
}

Upvotes: 1

Related Questions