User is trying to add a product from third party to Magento store. The product has been already created in Magento and prod id
is passed in third party page. Using event-observer, the product is being added using below code from third part and redirected to Magento cart page.
Module A
events.xml
<event name="controller_action_predispatch_mymodule_result_addproduct"> <observer name="addproduct_display" instance="Vendor\ModuleA\Observer\ThirdPartyAdd" /> </event>
ThirdPartyAdd.php
namespace Vendor\ModuleA\Observer; class ThirdPartyAdd implements \Magento\Framework\Event\ObserverInterface { public function __construct( \Magento\Store\Model\StoreManagerInterface $ storeManager, \Magento\Checkout\Model\Cart $ cart, \Magento\Catalog\Model\Product $ product, \Magento\Quote\Model\QuoteFactory $ quote, \Magento\Catalog\Model\ProductFactory $ productFactory, \Magento\Framework\App\ResponseFactory $ responseFactory, \Magento\Framework\UrlInterface $ url, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $ productCollectionFactory, \Magento\Eav\Model\Config $ eavConfig ) { $ this->_storeManager = $ storeManager; $ this->_cart = $ cart; $ this->_product = $ product; $ this->quote = $ quote; $ this->_productFactory = $ productFactory; $ this->_responseFactory = $ responseFactory; $ this->_url = $ url; $ this->_productCollectionFactory = $ productCollectionFactory; $ this->eavConfig = $ eavConfig; } public function execute(\Magento\Framework\Event\Observer $ observer) { if (isset($ _POST['source']) && $ _POST['source'] == 'third party') { for ($ i = 0; $ i < sizeof($ _POST['prod_id']); $ i++) { $ productModel = $ this->_productFactory->create(); $ product = $ productModel->load($ _POST['prod_id'][$ i]); $ product_type = $ product->getTypeID(); if ($ product_type == 'simple') { $ params = array( 'qty' => $ _POST['prod_qty'][$ i], ); $ this->_cart->addProduct($ product, $ params); $ this->_cart->save(); } if ($ product_type == 'configurable') { $ simple_products = $ product->getTypeInstance()->getUsedProducts($ product); $ attributes = $ product->getTypeInstance(true)->getConfigurableAttributesAsArray($ product); $ attr_label = explode('-', $ _POST['prod_attr'][$ i]); foreach ($ simple_products as $ simple_product) { $ super_attributes = array(); foreach ($ attributes as $ attribute) { foreach ($ attribute['values'] as $ value) { if(in_array($ value['label'], $ attr_label)){ echo $ value['label']; $ super_attributes[$ attribute['attribute_id']] = $ value['value_index']; } } } } $ this->_cart->addProduct($ product, array( 'qty' => $ _POST['prod_qty'][$ i], 'super_attribute' => $ super_attributes )); $ this->_cart->save(); } } $ customerBeforeAuthUrl = $ this->_url->getUrl('checkout/cart'); $ this->_responseFactory->create()->setRedirect($ customerBeforeAuthUrl)->sendResponse(); die(); } }
The issue is product quantity exceeds, it shows the exception We don't have as many product name as you requested
and stops the execution.
How do I show this exception message on Magento cart page ?