0

I have a custom module which, among other things, when installed will create a page. I want to pass a cart ID (encrypted) to this page as a param and then get this cart ID and delete. This needs to be be done via an ID, not via session.

This is what I have so far (effectively only creates the page):

<?php
    namespace Zitec\Concierge\Setup\Patch\Data;

    use Magento\Cms\Model\PageFactory;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Framework\Setup\Patch\DataPatchInterface;

    class AddCartClearingPage implements DataPatchInterface
    {
        /** @var ModuleDataSetupInterface */
        private $moduleDataSetup;

        /** @var PageFactory */
        private $pageFactory;
        private \Magento\Framework\App\Request\Http $request;
        protected  $_modelCart;
        protected $_checkoutSession;

        /**
         * @param ModuleDataSetupInterface $moduleDataSetup
         * @param PageFactory $pageFactory
         */
        public function __construct(
            \Magento\Framework\App\Request\Http $request,
            ModuleDataSetupInterface $moduleDataSetup,
            PageFactory $pageFactory
        ) {
            $this->moduleDataSetup = $moduleDataSetup;
            $this->pageFactory = $pageFactory;
            $this->request = $request;
        }

        /**
         * {@inheritdoc}
         */
        public function apply()
        {
            $pageContent = <<<HTML
        <div class="content">
            <div class="title">
                <h2>Clearing Cart</h2>
            </div>
        </div>
    HTML;

            $pageData = [
                'title' => 'Title',
                'identifier' => 'title',
                'page_layout' => 'identifier',
                'content_heading' => 'Clearing Cart',
                'content' => $pageContent,
                'is_active' => 1,
                'stores' => [\Magento\Store\Model\Store::DEFAULT_STORE_ID],
                'sort_order' => 11
            ];

            $this->moduleDataSetup->startSetup();

            /** @var \Magento\Cms\Model\Page $page */
            $page = $this->pageFactory->create();
            $page->setData($pageData)->save();

            $this->moduleDataSetup->endSetup();

            /**
             * Clearing the cart
             */
            # Not sure this is working as expected
            $cartID = $this->request->getParams();
            $cart = $this->_modelCart;
            # I dont think this is correct either
            $quoteItems = $this->checkoutSession->getQuote()->getItemsCollection();
            foreach($quoteItems as $item)
            {
                $cart->removeItem($item->getId())->save();
            }
        }
    }

Any suggestions on how to accomplish the remaining?

0

4 Answers 4

0

try this code

<?php

namespace Zitec\Concierge\Setup\Patch\Data;

use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\Quote\Item;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddCartClearingPage implements DataPatchInterface
{
    .................................................
    protected $quoteFactory;
    protected $itemmodel;
    .................................................

    public function __construct(
        .................................................
        QuoteFactory $quoteFactory,
        Item $itemmodel,
        .................................................
    ) {
        .................................................
        $this->quoteFactory = $quoteFactory;
        $this->itemmodel = $itemmodel;
        .................................................
    }

    public function apply()
    {
        .................................................
        .................................................
        
        $quoteId = 2; // pass your quote Id here
        $quoteData = $this->quoteFactory->create()->load($quoteId);
        $allItems = $quoteData->getAllItems();
        foreach ($allItems as $item)
        {
            $cartItemId = (int) $item->getItemId();
            $itemObj = $this->itemmodel->load($cartItemId);
            $itemObj->delete();
        }
        $quoteData->delete();
        
        .................................................
        .................................................
    }
}

I Hope This Help You.

0

if you want to remove all the item from cart or want to disable current cart than you can just set

$cart->setIsActive(0)
0

You can clear shopping cart using below code

<?php
namespace Zitec\Concierge\Setup\Patch\Data;

use Magento\Cms\Model\PageFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Checkout\Model\Cart;
use Magento\Quote\Api\CartRepositoryInterface;
class AddCartClearingPage implements DataPatchInterface
{
    protected $_modelCart;
    protected $quoteRepository;
    public function __construct(
         Cart $modelCart,
         CartRepositoryInterface $quoteRepository
    ) 
    {
       $this->_modelCart = $modelCart;
       $this->quoteRepository = $quoteRepository;
    }

    public function apply()
    {
        $cartId=123;
        $quote = $this->quoteRepository->get($cartId);
        $visitems = $quote->getAllVisibleItems();
        foreach ($visitems as $item) {
            $itemId = $item->getItemId();
            $this->_modelCart->removeItem($itemId)->save();
        }
    }
}

you can also clear cart using single line

$this->_modelCart->truncate();

0

I suggest you try this:

namespace Vendor\Extension\Controller\Index;
use Magento\Checkout\Model\Cart;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\App\Action\Action;
class Clearcart extends Action
{
    protected $_modelCart;
    protected $checkoutSession;
    public function __construct(CheckoutSession $checkoutSession, Cart $modelCart)
    {
        $this->checkoutSession = $checkoutSession;
        $this->_modelCart = $modelCart;
    }
    public function execute()
    {
        $cart = $this->_modelCart;
        $quoteItems = $this->checkoutSession->getQuote()->getItemsCollection();
        foreach ($quoteItems as $item) {
            $cart->removeItem($item->getId())->save();
        }
    }
}

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.