微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-在Woocommerce中添加新的自定义默认订购目录选项

我正在尝试通过SKU获取Woocommerce的认订单.
我已经在woocommerce设置中更改了订单,并添加了SKU,如下所示:

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' );

页面加载时,它仍然按受欢迎程度而不是SKU进行订购.
但是下拉列表显示按SKU排序(Sorteer op Referentie)

如果我转到另一个排序并返回,它将正确地在查询字符串中使用?orderby = sku对其进行排序.

解决方法:

更新(2018年12月)

您没有以正确的方式使用正确的钩子来通过sku获取认订购目录.还有一些缺少的所需附加代码

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');
    }
}

代码进入您的活动子主题(或活动主题)的function.PHP文件中.经过测试和工作.

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐