Сортировка корзины по алфавиту

Сортировка корзины по алфавиту


Александр

// Сортировка корзины по алфавиту
add_action( 'woocommerce_cart_loaded_from_session', 'alex_sort_cart_alphabetically', 25 );
function alex_sort_cart_alphabetically() {
   $products_to_sort = array();
   $cart_contents = array();
   foreach ( WC()->cart->get_cart_contents() as $key => $item ) {
    $products_to_sort[ $key ] = $item['data']->get_title();
   }
   natsort( $products_to_sort );
   foreach( $products_to_sort as $cart_key => $product_title ) {
       $cart_contents[ $cart_key ] = WC()->cart->cart_contents[ $cart_key ];
   }
   WC()->cart->cart_contents = $cart_contents;
}

// Код добавить в файл functions.php

Report Page