• Resolved futuresys

    (@futuresys)


    Hi,

    How do we change the email template to show the products in the same custom order as we have set them on the website?

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey @futuresys,

    The order customers add products to the cart ends up being the order they appear in invoices and the back end of the site too.

    It’s possible to change this tough with a bit of code. This Business Bloomer post shows how to sort these alphabetically.

    https://businessbloomer.com/woocommerce-sort-cart-items-alphabetically-az/

    You may be able to adapt that snippet to work with a different sort order.

    Take care,

    Thread Starter futuresys

    (@futuresys)

    Thank you @3sonsdevelopment, That certainly helped.

    I’ve adjusted the code to sort all products added to an order by the “menu_order”.

    add_action( 'woocommerce_cart_loaded_from_session', 'fs_sort_cart_items_by_menu_order' );
     
    function fs_sort_cart_items_by_menu_order() {
     
        // Read Cart Items
        $cart_products = array();
        foreach ( WC()->cart->get_cart_contents() as $key => $item ) {
            $cart_products[$key] = $item['data']->{'menu_order'};
        }
    
        // Sort Cart Items
        natsort( $cart_products );
     
        // Assign sorted items to cart
        $cart_contents = array();
        foreach ( $cart_products as $cart_key => $menu_order ) {
            $cart_contents[ $cart_key ] = WC()->cart->cart_contents[ $cart_key ];
        }
        WC()->cart->cart_contents = $cart_contents;
         
    }

    Hey @futuresys,

    Thanks for sharing that with us!

    Thread Starter futuresys

    (@futuresys)

    @3sonsdevelopment, pleasure. Always good to give back.

    Stay safe!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Sort Products on Order Confirmation Email’ is closed to new replies.