gpt4 book ai didi

php - 更改特定 woocommerce 类别存档的默认排序顺序

转载 作者:行者123 更新时间:2023-12-04 15:47:43 26 4
gpt4 key购买 nike

在 Woocommerce 中,我试图更改按日期顺序递减的"new" 产品类别存档页面的默认排序顺序,以便首先列出最新添加的内容。

我尝试了 2 个不同的代码片段。两者都更改了默认顺序,但首先显示最旧的产品。

我已将两个代码段中的 ASC 更改为 DESC,并且都没有对排序顺序进行任何更改。

我是编码的新手,感谢您对我出错的地方的帮助。

第一次尝试:

add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' );

function custom_default_catalog_orderby() {

$product_category = array( 'new' );

if ( is_product_category( $product_category ) ) {
return 'date_desc';
}
}

第二次尝试:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
function custom_catalog_ordering_args( $args ) {
$product_category = 'new'; // <== 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;
}

感谢任何帮助。

最佳答案

请尝试使用以下完整代码,该代码仅适用于“DESC”订单中按“日期”排序的特定产品类别存档页面:

// Utility conditional function targetting specific product category archive page
function is_product_category_orderby(){
// HERE your specific product category setting
return is_product_category('new');
}

// Set custom orderby "Date DESC" for specific product category archive
add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_date_desc' );
function enable_catalog_ordering_by_date_desc( $args ) {
if ( isset( $_GET['orderby'] ) && is_product_category_orderby() ) {
if ( 'date_desc' == $_GET['orderby'] ) {
return array(
'orderby' => 'date ID',
'order' => 'DESC',
);
}
// Make a clone of "menu_order" (the default option)
elseif ( 'natural_order' == $_GET['orderby'] ) {
return array( 'orderby' => 'menu_order title', 'order' => 'ASC' );
}
}
return $args;
}

// Add custom orderby "Date DESC" option for specific product category archive
add_filter( 'woocommerce_catalog_orderby', 'add_catalog_orderby_date_desc' );
function add_catalog_orderby_date_desc( $orderby_options ) {
if ( is_product_category_orderby() ) {
// Insert "Sort by modified date" and the clone of "menu_order" adding after others sorting options
return array(
'date_desc' => __("Sort by decreasing date ", "woocommerce"),
'natural_order' => __("Sort by natural shop order", "woocommerce"), // <== To be renamed at your convenience
) + $orderby_options ;
}
return $orderby_options ;
}

// Set default orderby to "Date DESC" option for specific product category archive
add_filter( 'woocommerce_default_catalog_orderby', 'default_catalog_orderby_date_desc' );
function default_catalog_orderby_date_desc( $default_orderby ) {
if ( is_product_category_orderby() )
$default_orderby = 'date_desc';

return $default_orderby;
}

// Set default orderby query to "Date DESC" option for specific product category archive
add_action( 'woocommerce_product_query', 'product_query_by_date_desc' );
function product_query_by_date_desc( $q ) {
if ( ! isset( $_GET['orderby'] ) && is_product_category_orderby() && ! is_admin() ) {
$q->set( 'orderby', 'date ID' );
$q->set( 'order', 'DESC' );
}
}

代码进入事件子主题(或事件主题)的 function.php 文件。测试和工作。

enter image description here

关于php - 更改特定 woocommerce 类别存档的默认排序顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55011566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com