【问题标题】:How do I manually set WooCommerce product sorting to "Popularity"?如何手动将 WooCommerce 产品排序设置为“人气”?
【发布时间】:2019-09-04 23:27:45
【问题描述】:

在 WooCommerce 中,我使用以下代码将默认排序设置为特定产品类别存档页面的按日期排序:

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

但是,这会影响我“按受欢迎程度”的整体默认排序设置,因为当我查看商店页面时,它的排序不正确,但如果我手动将其更改为按其他排序,然后再返回它会正确排序。

我该如何解决这个问题,或者我如何手动设置商店的其余部分以通过 php 流行度来订购,因为这可能会解决问题?

【问题讨论】:

    标签: php wordpress sorting woocommerce product


    【解决方案1】:

    更新:使用过滤器挂钩,您需要始终返回第一个函数参数变量,而不仅仅是没有值或默认函数变量的return 单独论点......所以在你的代码中:

    add_filter('woocommerce_default_catalog_orderby', 'custom_catalog_ordering_args', 10, 1);
    function custom_catalog_ordering_args( $orderby )
    {
        $product_category = 'specials'; // <== HERE define your product category slug 
    
        // For all other archives pages
        if ( ! is_product_category($product_category)) {
            return $orderby; // <====  <====  <====  <====  <====  HERE
        }
        // For the defined product category archive page
        return 'date'; 
    }
    

    或者这样更好:

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

    它现在应该可以工作了。

    相关:

    【讨论】:

      猜你喜欢
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      • 2020-04-26
      相关资源
      最近更新 更多