简体   繁体   中英

Change order of Cart in WooCommerce

I'm looking to reorder the product table on the Cart page in WooCommerce on WordPress. Currently the products listed go from oldest - newest (from order of adding to cart) and want to have the opposite, looking to have most recent added on top and oldest on bottom.

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' ); ?>

Would it be possible to add orderby when calling the cart_url ?

To do any kind of cart ordering you have to use woocommerce_cart_loaded_from_session hook; and to reverse the order simply use PHP array_reverse function.

Here is the code:

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);
}

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works.

Hope this helps!

The accepted answer has one major flaw: it creates a race condition and an infinite AJAX refresh loop with multiple tabs open ( see here ).

The way I was able to get around this is using action hooks:

  1. Before the cart contents are looped through, we reverse the contents and save the new, reversed order
  2. After the cart contents are looped through, we repeat step 1 to restore the original order

There are three areas (by default) where the cart items are looped on the frontend, so the action hooks I use cover each of those areas.

Here's the tested code:

function reverse_cart_contents() {
  $cart_contents = WC()->cart->get_cart_contents();

  if($cart_contents) {
    $reversed_contents = array_reverse($cart_contents);
    WC()->cart->set_cart_contents($reversed_contents);
  }
}
add_action('woocommerce_before_mini_cart', 'reverse_cart_contents');
add_action('woocommerce_after_mini_cart', 'reverse_cart_contents');
add_action('woocommerce_before_cart', 'reverse_cart_contents');
add_action('woocommerce_after_cart', 'reverse_cart_contents');
add_action('woocommerce_review_order_before_cart_contents', 'reverse_cart_contents');
add_action('woocommerce_review_order_after_cart_contents', 'reverse_cart_contents');

You can modify woocommerce plugin's cart/cart.php template file. When loop starts with "WC()->cart->get_cart()" on cart page, you can first take this array into a separate one, reverse and then use this reversed array for showing cart products in reverse order.

This option is suggested because you don't actually interact with woocommerce object and hence it involves lesser processing. You're just showing them upside down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM