2017-03-16 156 views
0

我正在尋找在WooCommerce上的WordPress購物車頁面上重新訂購產品表。目前列出的產品是從最舊的 - 最新的(從添加到購物車的順序)開始的,並且希望具有相反的效果,希望最近添加最上面和最老的最下面。更改WooCommerce中購物車的順序

do_action('woocommerce_before_cart'); ?> 

<div class="cart_container"> 

<form class="cart-form" action="<?php echo esc_url(WC()->cart->get_cart_url()); ?>" method="post"> 

<?php do_action('woocommerce_before_cart_table'); ?> 

是否有可能加入orderby調用cart_url什麼時候?

回答

1

要做任何類型的購物車訂購,你必須使用 woocommerce_cart_loaded_from_session鉤;並以 的順序反向使用PHP array_reverse函數。

下面是代碼:

add_action('woocommerce_cart_loaded_from_session', 'wh_cartOrderItemsbyNewest'); 

function wh_cartOrderItemsbyNewest() { 

    //if the cart is empty do nothing 
    if (WC()->cart->get_cart_contents_count() == 0) { 
     return; 
    } 

    //array to collect cart items 
    $cart_sort = []; 

    //add cart item inside the array 
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 
     $cart_sort[$cart_item_key] = WC()->cart->cart_contents[$cart_item_key]; 
    } 

    //replace the cart contents with in the reverse order 
    WC()->cart->cart_contents = array_reverse($cart_sort); 
} 

代碼放在文件中function.php您的活動子主題(或主題)的。或者也可以在任何插件php文件中使用。
代碼已經過測試和工作。

希望這會有所幫助!

+0

完美,正是我期待的,謝謝!任何想法如何返回我的迷你車相同的訂單? – Kelly

+0

@凱利:我對迷你手推車不甚瞭解,但會盡快找到解決方案。 –

0

您可以修改woocommerce插件的cart/cart.php模板文件。當循環以「WC() - > cart-> get_cart()」在購物車頁面上開始時,您可以先將此數組放入單獨的數組中,然後使用此反向數組以相反的順序顯示購物車產品。

建議使用此選項是因爲您實際上沒有與woocommerce對象進行交互,因此它涉及較少的處理。你只是把它們顛倒過來。

相關問題