0

I want to create a button in customer account page.

My aim is add a button to remove all active carts.

To do this what shall I do? do I need to override any files?

Please let me know if there is anything else could follow up for guide

I am using Magento 2.2.10

4
  • Take a look at the empty cart/basket button on the cart page Commented Dec 12, 2019 at 8:31
  • Sorry I didn’t get that Commented Dec 12, 2019 at 10:34
  • Jack Brooks you can create custom module
    – Robinio
    Commented Dec 12, 2019 at 14:35
  • How do you do that @RobinioTor any examples Commented Dec 12, 2019 at 15:45

2 Answers 2

2

If you don't know how to create module, use the link, then use my code below

Create index.php in VendorName/ModuleName/Block/CustomerAccount

<?php

  namespace VendorName\ModuleName\Block\CustomerAccount;

   use Magento\Checkout\Model\Cart as CustomerCart;

  class Index extends \Magento\Framework\View\Element\Template
   {

   protected $checkoutSession;

   protected $cart;

    protected $_urlInterface;

    public function __construct(
            \Magento\Backend\Block\Widget\Context $context,
            \Magento\Framework\UrlInterface $urlInterface,
             \Magento\Checkout\Model\Session $checkoutSession,
              CustomerCart $cart,
             array $data = [])
    {
        $this->checkoutSession = $checkoutSession;
        $this->cart = $cart;
        $this->_urlInterface = $urlInterface;
        parent::__construct($context, $data);
      }

    public function getCurrUrl()
     {

       return $this->_urlInterface->getCurrentUrl();

     } 


    public function removeActiveCart(){

      $allItems = $this->checkoutSession->getQuote()->getAllVisibleItems();

      foreach ($allItems as $item) {
        $itemId = $item->getItemId();
        $this->cart->removeItem($itemId)->save();
     }

    }
}

Create default.xml in VendorName/ModuleName/view/frontend/layout

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_account_navigation">
            <block class="VendorName\ModuleName\Block\CustomerAccount\Index" name="customer_account" template="VendorName_ModuleName::removecart.phtml" />
        </referenceBlock>
    </body>
</page>

Create removecart.phtml in VendorName/ModuleName/view/frontend/templates

<?php 
    $currentUrl= $this->getCurrUrl();

     if(isset( $_POST['remove']) AND $_POST['remove']=='true'){
       $this->removeActiveCart();
    }
?>

<form action="<?php echo $currentUrl; ?>" method="POST" id="removecart">
    <fieldset>
        <input type='hidden' name='remove' value="true">
    </fieldset>
    <button type="submit"><?php echo __("Remove Active Cart");?></button>
</form>

finish php bin/magento setup:di:compile

3
  • How can I do this in admin area? I have created a custom tab in customer view in admin so I want to add button there as well is that possible Commented Dec 14, 2019 at 20:39
  • This code for account page from frontend, in your question I didn't see mention of backend
    – Robinio
    Commented Dec 16, 2019 at 9:22
  • maybe it will help you magento.stackexchange.com/questions/206047/…
    – Robinio
    Commented Dec 16, 2019 at 9:39
1

You might not even need module. Might work with template button logic

https://github.com/magento/magento2/blob/9544fb243d5848a497d4ea7b88e08609376ac39e/app/code/Magento/Checkout/view/frontend/templates/cart/form.phtml#L59-L66

        <button type="button"
                name="update_cart_action"
                data-cart-empty=""
                value="empty_cart"
                title="<?= $block->escapeHtml(__('Clear Shopping Cart')) ?>"
                class="action clear" id="empty_cart_button">
            <span><?= $block->escapeHtml(__('Clear Shopping Cart')) ?></span>
        </button>
2
  • Can I do this for backend in admin area? Commented Dec 14, 2019 at 20:40
  • This would work for single customer on frontend. I misunderstood request. Backend you would need to delete entries from quote table github.com/DominicWatts/QuoteCleaner although you would do all orders rather than specific ones within a specific date range. Plus remove the cron / shell script option. Plus create a button to trigger something via a controller in admin area. I dont think you will find anything online. This is a very specific scenario. Commented Dec 14, 2019 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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