Вопрос

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?

UPDATE

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];
    }

Greetings!

Это было полезно?

Решение

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:

QuotePlugin.php

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
    ) {

        if($request){
            $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];
    }

}

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

See the testing video on my local instance here

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top