Last active
April 23, 2020 18:11
-
-
Save maxrice/6541634 to your computer and use it in GitHub Desktop.
WooCommerce - sort the cart alphabetically by the product's title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'woocommerce_cart_loaded_from_session', function() { | |
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; | |
}, 100 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Checkout Sort WooCommerce Products on Cart and Order plugin.
https://kartechify.com/product/sort-woocommerce-products-in-cart-and-order/