mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
21 lines
603 B
Text
21 lines
603 B
Text
add_action( 'woocommerce_cart_loaded_from_session', wc_sort_cart_items_alphabetically' );
|
|
|
|
function wc_sort_cart_items_alphabetically() {
|
|
|
|
// READ CART ITEMS
|
|
$products_in_cart = array();
|
|
foreach ( WC()->cart->get_cart_contents() as $key => $item ) {
|
|
$products_in_cart[ $key ] = $item['data']->get_title();
|
|
}
|
|
|
|
// SORT CART ITEMS
|
|
natsort( $products_in_cart );
|
|
|
|
// ASSIGN SORTED ITEMS TO CART
|
|
$cart_contents = array();
|
|
foreach ( $products_in_cart as $cart_key => $product_title ) {
|
|
$cart_contents[ $cart_key ] = WC()->cart->cart_contents[ $cart_key ];
|
|
}
|
|
WC()->cart->cart_contents = $cart_contents;
|
|
|
|
}
|