I'm trying to get the qty of products in a plugin with a method called beforeAddProduct, with this method I can manipulate things before being added to cart.

I need to check that the qty added to cart is 24 or 7, not less not more, in a Bundle product.

This is my code:

class Plugin
    protected $_attributeRepository;
    protected $logger;
    protected $cart;
     * @var \Magento\Quote\Model\Quote
    protected $quote;

     * Plugin constructor.
     * @param \Magento\Checkout\Model\Session $checkoutSession
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        Logger $logger,
        AttributeSetRepositoryInterface $attributeRepository,

    ) {
        $this->quote = $checkoutSession->getQuote();
        $this->logger = $logger;
        $this->_attributeRepository = $attributeRepository; 

     * beforeAddProduct
     * @param      $subject
     * @param      $productInfo
     * @param null $requestInfo
     * @return array
     * @throws LocalizedException
    public function beforeAddProduct($subject, $productInfo, $requestInfo = null)

        $attributeSetRepository = $this->_attributeRepository->get($productInfo->getAttributeSetId());
        $attributeSetName = $attributeSetRepository->getAttributeSetName();

        if($attributeSetName == "7barrios-packs-personalizados"){

            $items = $this->quote->getAllItems();
            $totalProducts = 0;
            foreach($items as $item) {
                $this->logger->info("Name: ".$item->getName());
                $this->logger->info("Quantity: ".$item->getQty()); 
                $totalProducts += $item->getQty(); //THIS IS WHAT I NEED TO GET

             if ($totalProducts < 24) {
                throw new LocalizedException(__('Add 24 products from bundle!'));

        return [$productInfo, $requestInfo];

I only can get the qty of products when they are in cart, but nof before in this method, what can I do?

Can $subject or $productInfo give me the qty of products?


I'm still not able to get the qty products but found that the original method uses getQtyRequest() for getting qty.

I'm trying this, but I'm getting nothing, any ideas?

   public function beforeAddProduct(Cart $subject, $productInfo, $requestInfo = null){

       $product = $subject->getProduct();
        $request = $subject->getQtyRequest($product, $requestInfo);
        $this->logger->info("Product QTY REQUEST BEFORE: ".$result);

        return [$productInfo, $requestInfo];



1 Answer 1


Yes, but it would require some additional logic. See how the original method gets the requested quantity here

Btw, there is an easier way to get the requested quantity. You can create a before plugin on method \Magento\Quote\Model\Quote::addProduct and get the requested quantity in argument $request, see the sample code below:


namespace NameSpace\Module\Plugin;

class QuotePlugin

     * @var \Psr\Log\LoggerInterface
    private $logger;

    public function __construct(\Psr\Log\LoggerInterface $logger)
        $this->logger = $logger;

    public function beforeAddProduct(
        \Magento\Quote\Model\Quote $subject,
        \Magento\Catalog\Model\Product $product,
        $request = null,
        $processMode = \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL
    ) {

            $this->logger->info("BUNDLE PRODUCT QTY REQUEST BEFORE: " . $request->getData('qty'));
            $this->logger->info("BUNDLE OPTION REQUEST BEFORE: " . json_encode($request->getData('bundle_option')));
            $this->logger->info("BUNDLE OPTION QTY REQUEST BEFORE: " . json_encode($request->getData('bundle_option_qty')));
        return [$product, $request, $processMode];



<?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\Quote\Model\Quote" >
        <plugin name="before-add-to-cart" type="NameSpace\Module\Plugin\QuotePlugin"/>

See the testing video on my local instance here

  • Can you see my updated question please? I'm trying to get it from getQtyRequest() but not luck yet. May 14, 2020 at 17:16
  • @OscarVazquez getQtyRequest is a private method, so you can't call $subject-> getQtyRequest(). Please review and implement it in your plugin.
    – Toan Tam
    May 14, 2020 at 17:21
  • how should I call the method ? $subject->_getQtyRequest() won't work either I guess, I think this method is the responsible for returning the qty, but don't know how to use it. May 14, 2020 at 18:12
  • I've updated my response using the second approach.
    – Toan Tam
    May 15, 2020 at 3:54
  • Thanks Man! you saved my life, it works perfectly May 15, 2020 at 15:38

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.