【发布时间】:2020-11-17 05:01:25
【问题描述】:
我正在尝试更改默认 order by 的名称,为 name 添加新 order by,并删除几个不相关的默认选项:
// Remove the other sort order options and add and rename 1
add_filter('woocommerce_catalog_orderby', 'change_default_sorting_options');
function change_default_sorting_options($options)
{
unset($options['popularity']);
unset($options['rating']);
unset($options['date']);
unset($options['price']);
unset($options['price-desc']);
$options['name'] = 'Sort by Name';
$options['menu_order'] = 'Catalog Sort Order';
return $options;
}
这很好用。默认是 menu_order 选项。
然后,当类别为“20 Gauge”时,我希望目录按“名称”排序。添加此代码结果会按名称对“20 Gauge”类别进行排序,但会从排序顺序下拉列表中删除默认选项 (menu_order):
add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');
function custom_default_catalog_orderby()
{
if (is_product_category(array('20-gauge'))) {
return 'name'; // sort by latest
} else {
return 'menu_order';
}
}
【问题讨论】:
标签: php wordpress sorting woocommerce hook-woocommerce