Here we will see how to Reorder cart items alphabetically in Woo Commerce by custom coding.

Reorder cart items alphabetically

Put the below code in function.php file :

add_action('woocommerce_cart_loaded_from_session', reorder_cart, 100);

function reorder_cart() {
    global $woocommerce;
    $products_in_cart = array();
    foreach ($woocommerce->cart->cart_contents as $key => $item) {
        $products_in_cart[$key] = $item['data']->get_title();
    }
    natsort($products_in_cart);
    $cart_contents = array();
    foreach ($products_in_cart as $cart_key => $product_title) {
        $cart_contents[$cart_key] = $woocommerce->cart->cart_contents[$cart_key];
    }
    $woocommerce->cart->cart_contents = $cart_contents;
}

The above code will sort the products in alphabetical order.

There are currently no comments.