0

I was trying to Divide Cart items Based on Attribute value. I tried to fetch the attribute on Cart Page.

Basically I have Attribute is_ship_immediately with yes or no option. I have products with yes or no options in the cart. If option is yes for products in cart then I want to show option yes items under the title(Ship immediately). Then If option is no I want to Show option no items under title(Not Ship immediately).

1

You will need to change the cart item templates probably, depending on your design.

There you'll get a collection of the cart items. From the items you can fetch the product model using the getProduct method. Then finally you may call the getData('is_ship_immediately') on the product model to get your data, and then structure your cart accordingly.

A simple working example would be to change Magento_Checkout::cart/form.phtml at around line number 36 where this is written

<?php foreach ($block->getItems() as $_item): ?>
    <?= $block->getItemHtml($_item) ?>
<?php endforeach ?>

to this

<?php
    $cartItems = [
        'ship_immediately'      => [],
        'dont_ship_immediately' => []
    ];
    foreach ($block->getItems() as $_item) {
        if ($_item->getProduct()->getData("is_ship_immediately")) {
            $cartItems['ship_immediately'][] = $block->getItemHtml($_item);
        } else {
            $cartItems['dont_ship_immediately'][] = $block->getItemHtml($_item);
        }
    }
?>

<?php /* SHIP THESE ITEMS IMMEDIATELY!!! */ ?>
<tr><th><?= __("Ship immediately") ?></th></tr>
<?= implode("", $cartItems['ship_immediately']) ?>

<?php /* Meh...These can wait */ ?>
<tr><th><?= __("Don't ship immediately") ?></th></tr>
<?= implode("", $cartItems['dont_ship_immediately']) ?>

NOTE

DO NOT CHANGE THE TEMPLATE FILE DIRECTLY.

Override it in your theme or write an afterPlugin to the \Magento\Checkout\Block\Cart\Grid class's getTemplate function where you would return your own cart template, and then make the changes there

P.S

Since the product information may change, I'd suggest you keep the is_ship_immediately data on your quote_item as well as order_item level

| improve this answer | |
  • Thank you with a bit of change in your code it worked for me. – Raghav May 21 '18 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.