简体   繁体   中英

Change default sorting order for multiple specific woocommerce product categories

I'm trying to change the default orderby for multiple product categories, but can't work out how to adapt the code found here Cant change default sorting order of specific woocommerce category to 'popularity' to apply the change to more than one category.

Here's the code I currently have that is changing the default orderby for my comic book preorders category:

add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' );

function custom_default_catalog_orderby() {

    $product_category = 'comic-book-pre-orders';

    if ( is_product_category( $product_category ) ) {
        return 'sku_asc';
    }
    else {
        return 'date';
}
}

Ideally I'd be able to apply the custom orderby to my 'comic-book-subscriptions' category as well as a couple of others potentially.

Any help would be very much appreciated!

Kind regards, JP

Think I managed to work it out!

Here's the code I'm now using:

add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' );

function custom_default_catalog_orderby() {

    $product_category = array( 'comic-book-pre-orders', 'comic-book-subscriptions' );

    if ( is_product_category( $product_category ) ) {
        return 'sku_asc';
    }
    else {
        return 'date';
}
}

If anyone has a better approach please let me know :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM