簡體   English   中英

如何更改 WooCommerce“我的賬戶/訂單”按鈕點擊排序

[英]How to change sorting in WooCommerce "my-account/orders" on button click

我正在嘗試將按鈕添加到 WooCommerce 客戶前端視圖中的“我的帳戶/我的訂單”選項卡,這會更改點擊時客戶訂單的排序順序。

幾天來我一直在努力讓它發揮作用。 我想我的方法是錯誤的,但我對 PHP 和 wordpress 鈎子/邏輯的了解太有限,無法找到更好的解決方案。 請幫忙!

我的子主題的“functions.php”中有這段代碼。

謝謝。

add_filter('woocommerce_my_account_my_orders_query', 'my_account_orders_query_change_sorting_DESC' , 20);
function my_account_orders_query_change_sorting_DESC($args)
{
    $args['order'] = 'DESC'; 
    return $args;
}

add_filter('woocommerce_my_account_my_orders_query', 'my_account_orders_query_change_sorting_ASC' , 20);
function my_account_orders_query_change_sorting_ASC($args)
{
    $args['order'] = 'ASC'; 
    return $args;
}
add_action('woocommerce_before_account_orders', 'switch_date' , 20);
function switch_date($has_orders)
{
    if ($has_orders) {
        echo '<form method="post">
        <input type="submit" name="button1"
                value="ASC" />
          
        <input type="submit" name="button2"
                value="DESC" />
        </form>';
    }
    if (isset($_POST['button1'])) 
    {
        apply_filters('woocommerce_my_account_my_orders_query', 'my_account_orders_query_change_sorting_ASC' , 20); 
    }
    else if(isset($_POST['button2'])) 
    {
        apply_filters('woocommerce_my_account_my_orders_query', 'my_account_orders_query_change_sorting_DESC' , 20); 
    } 
}

我解決了。 :) 問題是我不明白網站加載的順序。 兩個鈎子是不必要的並且會互相覆蓋。 apply_filters() 是不必要的。 這就像一個魅力:

add_action('woocommerce_before_account_orders', 'add_resort_form');
function add_resort_form($has_orders)
{   
    if ($has_orders) {
        echo '<form method="post">
        <input type="submit" name="button1"
                value="ASC" />          
        <input type="submit" name="button2"
                value="DESC" />
        </form>';
    }
}

add_filter('woocommerce_my_account_my_orders_query', 'my_account_orders_query_change_sorting');
function my_account_orders_query_change_sorting($args)
{
    if (isset($_POST['button1'])) {
        $value = 'ASC';
    }
    else if(isset($_POST['button2'])) {
        $value = 'DESC';    
    }
    $args['order'] = $value; 
    return $args;
}

暫無
暫無

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

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