1

I need to add button/link into the basket page which will be able to remove a single item. Each item should have separate button for remove that item.

How can I insert it into Magento_Checkout:cart/item/default.phtml file?

1
  • jquery ? you can add with jquery, create functions of jquery to hide a item, Access it in jquery using ID of item. and apply function of jquery like ('#item1').hide();
    – Learner
    Sep 29 '17 at 8:19
1

You can look how it's done in the base layout: magento 2 cart

checkout_cart_item_renderers.xml adds template with remove link to item renderer:

<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.default.actions.remove" template="Magento_Checkout::cart/item/renderer/actions/remove.phtml"/>

And remove.phtml template itself:

<a href="#"
title="<?php echo $block->escapeHtml(__('Remove item')); ?>"
class="action action-delete"
data-post='<?php /* @escapeNotVerified */ echo $block->getDeletePostJson(); ?>'>
    <span>
        <?php /* @escapeNotVerified */ echo __('Remove item')?>
    </span>
</a>

UPD: You can use direct helper call in the Magento_Checkout:cart/item/default.phtml but this is very, very bad practice and I hope you won't use on the site

    <a href="#"
         title="<?php echo $block->escapeHtml(__('Remove item')); ?>"
         class="action action-delete"
         data-post='<?php echo $this->helper('Magento\Checkout\Helper\Cart')->getDeletePostJson($_item) ?>'>
         <span><?php echo __('Remove item')?></span>
    </a>
8
  • can I just use something like button inside default.phtml without using xml part?
    – Joey
    Oct 2 '17 at 7:28
  • Magento remove item from cart via AJAX request, but removing item from quote is not the only triggered process. It also updates browser local storage and regenerates the minicart content and counter. Oct 2 '17 at 8:57
  • Technically you can use url example.com/checkout/cart/delete/id{quote_item_is}/form_key/{form_key}, but it will redirect you to the homepage, won't update minicart, expose form key in GET params. I think you can't do it without writing additional code Oct 2 '17 at 9:02
  • can you help me? I need to have 2 buttons to each item in basket to update its quantity and remove the particular item. I've used this line, but it doesnt display anything <?php / @escapeNotVerified / echo $block->getActions($_item); ?>
    – Joey
    Oct 2 '17 at 9:04
  • 1
    it adds an undeclared dependency to the code, helper should be injected into the block. Templates should operate only block methods. I've tested code on blank and luma themes and it displays the remove button, can't say what was chamnged in your theme and what should be added to make it work Oct 2 '17 at 10:14

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.