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

Barry
  • 587
  • 1
  • 5
  • 22
Jack Brooks
  • 319
  • 4
  • 15

2 Answers2

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

Robinio
  • 771
  • 10
  • 32
  • 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 – Jack Brooks Dec 14 '19 at 20:39
  • This code for account page from frontend, in your question I didn't see mention of backend – Robinio Dec 16 '19 at 09:22
  • maybe it will help you https://magento.stackexchange.com/questions/206047/how-to-add-a-button-to-the-customer-edit-page-within-the-admin – Robinio Dec 16 '19 at 09: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>
Dominic Pixie
  • 7,520
  • 4
  • 17
  • 56
  • Can I do this for backend in admin area? – Jack Brooks Dec 14 '19 at 20:40
  • This would work for single customer on frontend. I misunderstood request. Backend you would need to delete entries from quote table https://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. – Dominic Pixie Dec 14 '19 at 21:36