Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

php - Add a new custom default ordering catalog option in Woocommerce

I am trying to get the default order of Woocommerce to be order by SKU. I have changed the order in the woocommerce settings and added SKU like this:

function sv_add_sku_sorting( $args ) {

    $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

    if ( 'sku' == $orderby_value ) {
        $args['orderby'] = 'meta_value';
        $args['order'] = 'asc';
        // ^ lists SKUs alphabetically 0-9, a-z; change to desc for reverse alphabetical
        $args['meta_key'] = '_sku';
    }

    return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'sv_add_sku_sorting' );

function sv_sku_sorting_orderby( $sortby ) {
    $sortby['sku'] = 'Sorteer op referentie';
    // Change text above as desired; this shows in the sorting dropdown
    return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );
add_filter( 'woocommerce_default_catalog_orderby_options', 'sv_sku_sorting_orderby' );

On pageload it still orders on popularity instead of on SKU. But the dropdown shows order by SKU (Sorteer op referentie)

If I go to another ordering and go back it will correctly order them with ?orderby=sku in the querystring.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated (Dec 2018)

You are not using the right hooks in the right way to get default ordering catalog by sku. There is also some missing needed additional code:

add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_sku' );
function enable_catalog_ordering_by_sku( $args ) {

    if ( isset( $_GET['orderby'] ) ) {
        if ( 'sku' == $_GET['orderby'] ) {
            return array(
                'orderby'  => 'meta_value_num',
                'order'    => 'ASC',
                'meta_key' => '_sku',
            );
        }
        // Make a clone of "menu_order" (default option)
        elseif ( 'natural_order' == $_GET['orderby'] ) {
            return array( 'orderby'  => 'menu_order title', 'order' => 'ASC' );
        }
    }

    return $args;
}

add_filter( 'woocommerce_catalog_orderby', 'add_catalog_orderby_by_sku' );
function add_catalog_orderby_by_sku( $orderby_options ) {
    // Insert "Sort by product reference (sku)" and the clone of "menu_order"
    return array(
        'sku'           => __("Sort by product reference (sku)", "woocommerce"),
        'natural_order' => __("Sort by natural shop order", "woocommerce"), // <== To be renamed at your convenience
    ) + $orderby_options ;
}


add_filter( 'woocommerce_default_catalog_orderby', 'default_catalog_orderby_sku' );
function default_catalog_orderby_sku( $default_orderby ) {
    return 'sku';
}

add_action( 'woocommerce_product_query', 'product_query_by_sku' );
function product_query_by_sku( $q ) {
    if ( ! isset( $_GET['orderby'] ) && ! is_admin() ) {
        $q->set( 'orderby', 'meta_value_num' );
        $q->set( 'order', 'ASC' );
        $q->set( 'meta_key', '_sku');
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

enter image description here

Related: Sort WooCommerce Products by SKU Numeric (1.2.3.4.5.6........)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...