• Resolved rplinux

    (@rplinux)


    Hi,

    Is it possible to have multiple cart on cart page which are separated by product’s root category.
    Let A,B and C are root product categories and there are subcategories and products in these root category.
    I want to show different cart for A, B and C at a time on cart page.
    I should able to add different root category product at a time and on cart page added root categories appear with there cart. should have different update cart and checkout button for each carts.
    Also could I able to set minimum order amount for each root category.

Viewing 14 replies - 1 through 14 (of 14 total)
  • This is possible, but there are quite a few changes that you will need to do.
    Here are some steps to achieve this:
    – The WC cart will be one, you will be separating the cart items by a meta key
    1. Add identification meta key to each cart items, on add to cart. This way you can identify and group the items.

    2. Modify the cart template to match your requirements. Different tables for each item group or some sort of visual identification mark.

    3. Updating the cart should be pretty straight forward as there are no multiple carts there are just items groups, so you can just update the cart item as normal.

    4. If you are to checkout each cart item group as a separate cart item, you will have to keep track of the cart items and modify the totals accordingly on both cart and checkout.

    5. The minimum order amount you can cover by just checking against the cart item group total.

    Thread Starter rplinux

    (@rplinux)

    @vankaa could you please explain through code.

    Which part?

    Thread Starter rplinux

    (@rplinux)

    Point 4 and 5.

    Thread Starter rplinux

    (@rplinux)

    https://drive.google.com/open?id=0BwJIQAf9mVSLNUZVZDVNMWZqRkE

    Please check this to understand what I want to do.
    This has multiple carts, on single cart page and individual checkout and update too.

    On Add to cart you add your category group to the item data:

    // On Add to cart add the item custom data
    add_filter( 'woocommerce_add_cart_item_data', 'prefix_add_cart_item_data' );
    function prefix_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
    	// Add your logic to find the item category group.
    	$category_group = "RAZ CHRISTMAS";
    
    	$cart_item_data['item_category_group'] = $category_group;
    
    	return $cart_item_data;
    }

    Than in cart you have to split your items into their respective groups. For each cart you will replace the global totals with the totals of the respective group.

    For checkout you can either remove the other groups from the cart and just checkout the one that was chosen or filter the order total and the items displayed on the checkout page.

    As I said before, this is not something that you do in couple of hours. There are quite a few changes you will need to do.

    Thread Starter rplinux

    (@rplinux)

    Ok could you share code how to separate cart product by category and show them as individual cart

    Just loop through the items and group them together then output your cart tables:

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            // array( [group-name] => array( [cart_item_key] => [cart_item] ) )
    	$group_items[ sanitize_title( $cart_item['item_category_group'] ) ][ $cart_item_key ] = $cart_item;
    }
    
    // Then loop through each of the groups and output your cart tables
    foreach ( $group_items as $group_name => $group ) {
    	// start your table
    	foreach( $group as $gcart_key => $gcart_item ) {
    		// out put your items
    	}
    }
    Thread Starter rplinux

    (@rplinux)

    Ok I have done grouping and showed them on cart separately. Now how to check out individual group?

    Thread Starter rplinux

    (@rplinux)

    Thank you @vankaa
    I am facing difficulty in checkout of those individual groups.
    Which hook is applied when we click proceed to checkout at cart page.

    vankaa

    (@vankaa)

    Cart is almost the same as checkout. However, here you have to look at your gateway in addition to the totals. If your gateway send item by item the total, than you either need to filter its properties or filter the whole cart and remove the other items. Basically you have to make sure you charge only for the items that are checking out.

    I would recommend removing the other items on checkout. It will save you a lot of headache on filtering stuff. But you do what matches your requirements.

    Set an input in each cart form or submit button to let you know which cart the customer is checking out, then separate things based on this input.

    What exactly you can’t figure out?

    Thread Starter rplinux

    (@rplinux)

    Ok @vankaa, What I am doing is, When User click on proceed to checkout button I call a ajax and then first save the whole cart as user meta then I remove other categories product from cart, but before remove I again save them as user meta so that now in cart only products of the category which I want to checkout remained. Now after that redirect to checkout page. so, on checkout page cart contain only checkout category products which appear on order review and subtotal calculated accordingly.
    Now I need hook which run when in click on place order so that I empty cart and reload it with saved remaining categories products.
    Also I need hook which run when I go to cart page from any where so that on cart page I reload saved whole cart. For ex. I go to checkout page then cart only contain checkout category products now if I go to cart again without order then I should see all the categories products which were before I clicked proceed to checkout. I saved whole cart before I go to checkout page I just need to reload that cart. The problem is which hook runs only on cart page so that when I go to cart page from any where I just reload saved cart which contains whole cart.

    vankaa

    (@vankaa)

    This will not work as you think. You cannot just add the cart when the customer visits the cart page from the checkout page because the cart is one and the checkout page will use it. Imaging if the customer has both the cart and checkout page open, then which cart will you checkout? You can add the cart back, but may be after the order was placed and/or paid for. May be watch for woocommerce_cart_emptied or woocommerce_checkout_create_order or woocommerce_checkout_order_processed. Look around for hooks, test the process and see which hook(s) fit your needs.

    We haven’t heard back from you in a while, so I’m going to mark this as resolved. If you have any further questions, you can start a new thread.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Multiple cart on cart page separated by root product category’ is closed to new replies.