【问题标题】:How do I order a single category by date and the rest of the catalog by popularity?如何按日期订购单个类别,按受欢迎程度订购目录的其余部分?
【发布时间】:2018-11-13 16:30:16
【问题描述】:

我希望我的整个 WooCommerce 目录按受欢迎程度排序(只是标准设置),但我还希望我的特价商品按日期排序,因为我们在一周的固定日期发布新交易。

我在这里找到了这段代码,但它似乎根本没有改变特价商品类别的顺序:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
function custom_catalog_ordering_args( $args ) {
    $product_category = 'specials'; // <== HERE define your product category

    // Only for defined product category archive page
    if( ! is_product_category($product_category) ) return $args;

    // Set default ordering to 'date ID', so "Newness"
    $args['orderby'] = 'date ID';

    if( $args['orderby'] == 'date ID' )
        $args['order'] = 'DESC'; // Set order by DESC

    return $args;
}

我如何获得我想要的订单?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    您使用了错误的钩子,woocommerce_get_catalog_ordering_args 用于添加您不需要的新排序选项,因为 WooCommerce 已经有 sortby 最新选项。

    所以为了修改特定类别的默认排序选项,您需要使用woocommerce_default_catalog_orderby钩子,如下所示:

    add_filter('woocommerce_default_catalog_orderby', 'custom_catalog_ordering_args', 20, 1);
    function custom_catalog_ordering_args($sortby)
    {
        $product_category = 'specials'; // <== HERE define your product category slug 
    
        // Only for defined product category archive page
        if (! is_product_category($product_category)) {
            return;
        }
        return 'date';
    }
    

    上面的代码已经过测试并按预期工作,只需将代码添加到您的functions.php 即可开始使用

    【讨论】:

    • 是否有任何理由表明此代码会影响我按受欢迎程度排序的默认产品,因为它似乎将其丢弃并且没有准确地给出最受欢迎的产品。当我在商店页面上更改排序方式并将其改回流行度时,它会给我一个准确的流行度排序。
    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多