簡體   English   中英

更改特定 woocommerce 類別存檔的默認排序順序

[英]Change default sorting order for specific woocommerce category archive

在 Woocommerce 中,我試圖更改按日期順序遞減的“新”產品類別存檔頁面的默認排序順序,以便首先列出最新添加的內容。

我嘗試了 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 文件中。 測試和工作。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM