在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';
}
但是,这会影响我的整体默认排序设置“按受欢迎程度”,因为当我在我的商店页面上查看它排序不正确但如果我手动更改它以按其他方式排序然后返回它正确排序。
我该如何解决这个问题,或者如何通过人气来手动设置商店的其余部分,因为这可能会解决问题?
更新:使用过滤器钩子,您需要始终返回第一个函数参数变量,而不仅仅是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;
}
它现在应该工作。
有关: