0

Purpose:

  • Restrict cart item quantity update when total products are more than 10. So I have created around plugin for UpdateItemQty controller which check total quantities and return error when items are more than 10.

Problem:

  • Error return successfully but problem is it translate error string as per default store. I have checked current store and found that current store is not set. So website shows that I am in en-int store but this controller shows current store is de-de.

UpdateItemQty.php:

<?php
/**
 * @author Ronak Chauhan 
 */

namespace MyModule\Checkout\Plugin\Controller\Cart;

use Closure;
use Exception;
use Magento\Checkout\Controller\Cart\UpdateItemQty as ParentClass;
use Magento\Checkout\Model\Cart\RequestQuantityProcessor;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Serialize\Serializer\Json;
use MyModule\Checkout\Helper\Data;

class UpdateItemQty
{
    /** @var Json */
    protected $json;

    /** @var Data */
    protected $helper;

    /** @var RequestInterface */
    protected $request;

    /** @var RequestQuantityProcessor */
    private $quantityProcessor;

    public function __construct(
        Json $json,
        Data $helper,
        RequestQuantityProcessor $quantityProcessor,
        RequestInterface $request
    ) {
        $this->json = $json;
        $this->helper = $helper;
        $this->request = $request;
        $this->quantityProcessor = $quantityProcessor;
    }

    /**
     * @param ParentClass $subject
     * @param Closure $proceed
     *
     * @return mixed
     */
    public function aroundExecute(ParentClass $subject, Closure $proceed)
    {
        try {
            $cartRequestData = $this->request->getParam('cart');
            $cartData = $this->quantityProcessor->process($cartRequestData);

            if (true) {
                $cartRestrictionError = __(
                    "We are happy that you like so many products! Unfortunately, %1 is the limit for number of items in your shopping cart.",
                    10
                );
                return $subject->getResponse()->representJson(
                    $this->json->serialize(
                        $this->helper->getResponseData(
                            mb_strtoupper($cartRestrictionError, 'UTF-8')
                        )
                    )
                );
            }
        } catch (Exception $e) {
            return $subject->getResponse()->representJson(
                $this->json->serialize($this->helper->getResponseData($e->getMessage()))
            );
        }
        return $proceed();
    }
}

In above around plugin as well as Original controller I have checked current store but getting default store only.

If anyone has idea it would be appreciated. Thanks!

1

If you want to do something when quantities are updating in cart page then please use below plugin method.

Step 1 : Please create di.xml file under path Namespace/PackageName/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Model\Cart">
        <plugin name="interceptUpdateProductToCart" type="Namespace\PackageName\Plugin\RestrictCartPlugin" sortOrder="10"/>
    </type>
</config>

Step 2: Please create RestrictCartPlugin.php under path Namespace/PackageName/Plugin/RestrictCartPlugin

<?php

namespace Namespace\PackageName\Plugin;

class RestrictCartPlugin
{
    public function aroundUpdateItems(\Magento\Checkout\Model\Cart $subject, callable $proceed, $data)
    {
        //do your stuff here around update quantities of items
    }
}

Please check above way.

You can set current store programmatically as per below

/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */

 protected $_storeManager;

public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
    $this->_storeManager = $storeManager;
}

// now you can set your current store like

$this->_storeManager->setCurrentStore('YOUR_STORE_ID');
5
  • quation updated. May 28 at 8:51
  • Please try to set current store pro grammatically in your controller.
    – ZealousWeb
    May 28 at 9:56
  • Updated answer.
    – ZealousWeb
    May 28 at 9:58
  • you can set current store in the starting of code.
    – ZealousWeb
    May 28 at 10:04
  • I already tried this but it's not working. Jun 4 at 7:20

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.