woocommerceproductcartordersemail-notifications

Sort products in cart by category and also apply this sorting method on orders pages and email notifications in WooCommerce


I sort my cart by category using this code snippet:

//order in cart
function woocommerce_before_cart_contents(){
    global $woocommerce;
    $cat_wisw_pros = array();
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product_id = $cart_item['product_id'];
        $cat_ids = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
        foreach ( $cat_ids as $id ) {
            $cat_wisw_pros[$id][$cart_item_key] = $cart_item;
        }
    }
    ksort( $cat_wisw_pros ); // Cat ID wise sort
    $grouped_cart_items = array();
    foreach ( $cat_wisw_pros as $cat_id => $cart_items ) {
        foreach ( $cart_items as $cart_item_key => $cart_item ) {
            if( !array_key_exists( $cart_item_key, $grouped_cart_items ) )
                $grouped_cart_items[$cart_item_key] = $cart_item;
        }
    }
    $woocommerce->cart->cart_contents = $grouped_cart_items;
}

add_action( 'woocommerce_before_cart_contents', 'woocommerce_before_cart_contents' );

This works perfectly, as you can see on the picture:

enter image description here

Red Boxes: The product category "Basic Products-GNC-Verkauf" is the first one, and then comes "Werbematerial/Luxusproben".

But as soon I paid, there isn't the sorted order. So the problem is, that on the invoice the sorted order vanishes. But I need, that the customer can see that the order is sorted.

How can I apply the order needs to be sorted?


Solution

  • The custom sort order is no longer applied when leaving the cart page because you are using woocommerce_before_cart_contents hook.

    You can replace it with the woocommerce_cart_loaded_from_session hook, so the custom sort order is also applied on the order received page. Furthermore, I made some adjustments to your existing code:

    So you get:

    // Order in cart and order review page
    function action_woocommerce_cart_loaded_from_session( $cart ) {
        // Initialize
        $cat_cart_items = array();
        $grouped_cart_items = array();
    
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            // Get the product categories ID for this item
            $cat_ids = $cart_item['data']->get_category_ids();
    
            // Push to array
            foreach ( $cat_ids as $id ) {
                $cat_cart_items[$id][$cart_item_key] = $cart_item;
            }
        }
    
        // Sort an array by key in ascending order
        ksort( $cat_cart_items );
    
        // Loop through cat cart items
        foreach ( $cat_cart_items as $cart_items ) {
            // Loop through cart items
            foreach ( $cart_items as $cart_item_key => $cart_item ) {
                // Checks an array for a specified key and if the key does not exist
                if ( ! array_key_exists( $cart_item_key, $grouped_cart_items ) ) {
                    // Push to array
                    $grouped_cart_items[$cart_item_key] = $cart_item;
                }
            }
        }
    
        // Cart contents
        $cart->cart_contents = $grouped_cart_items;
    }
    add_action( 'woocommerce_cart_loaded_from_session', 'action_woocommerce_cart_loaded_from_session', 10, 1 );