woocommerce how to list products in cart according to category?

I am using woocommerce with wordpress. I need to show products in cart by category.for ex. if user select a product from category1 and add to cart then add product in cart from category2 and again add any other product from category1.Then i need to products of category1 and then category 2.I need to show products in this way in cart.I am trying to changing the code in cart.php file.

Cart

Read More
 Category1

 product name   category       qty    price
 aaa           category1      2      50
 bbb               category1      1       10

Category2

product name   category       qty    price
test1          category2      2      40
test2          category2      1      10     

$taxonomies = get_terms( array(
            'taxonomy' => 'product-cat',
            'hide_empty' => false,
        ) );

        foreach ( $taxonomies as $_tax ) 
        {
             $allfactories[] = $_tax->name;
        }




        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

            //print_r($cart_item);

            $cart_cat = $cart_item['wdm_user_custom_data_value'];

            $terms = wp_get_object_terms($cart_item['product_id'], 'product-cat');
            foreach ($terms as $term) {
               $product_cat = $term->name;
               $factory[]  = $term->name;
            }

            if($product_cat==$cart_cat)
            {

            $_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
            $product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

            }

        }   

Related posts

Leave a Reply