How to Get Simple Product from Magento 2 Configurable Product Before Add to Cart
As a Magento 2 developer, you come across a variety of business demands that need to be fulfilled to optimize the profit or customer service.
As a Magento 2 developer myself, I recently came across a client who sells the configurable products having a different combination of the simple products offering different payment methods. A combination can be bought with recurring payment method and another can be bought as a one time purchase. Now, as the recurring product cannot be combined with the other payment options, it was required to get simple product from Magento 2 configurable product Before add to cart and set up the restriction accordingly.
The below code helps you get the simple products from the configuration product being added to cart to handle the various validations and restrictions based on it.
Method to Get Simple Product from Magento 2 Configurable Product Before Add to Cart”
- Create events.xml at app\code\Vendor\Extension\etc\
1234567<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"><event name="controller_action_predispatch_checkout_cart_add"><observer name="restrict_sales_model_cart_add_before" instance="Vendor\Extension\Observer\Cartadd"/></event></config> - Create Cartadd.php at app/code/Vendor/Extension/Observer/
1234567891011121314151617181920212223242526272829303132333435<?phpnamespace Vendor\Extension\Observer;use Magento\Framework\Event\ObserverInterface;use Magento\Framework\App\Response\RedirectInterface;use Magento\Checkout\Model\Cart;use Magento\Framework\App\RequestInterface;use Magento\Catalog\Model\Product;use Magento\ConfigurableProduct\Model\Product\Type\Configurable;class Cartadd implements ObserverInterface{protected $cart;protected $redirect;protected $request;protected $product;protected $configurableproduct;public function __construct(RedirectInterface $redirect, Cart $cart, RequestInterface $request, Product $product, Configurable $configurableproduct){$this->redirect = $redirect;$this->cart = $cart;$this->request = $request;$this->product = $product;$this->configurableproduct = $configurableproduct;}public function execute(\Magento\Framework\Event\Observer $observer){$postValues = $this->request->getPostValue();$productId = $postValues['product'];$addProduct = $this->product->load($productId);if ($addProduct->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE){$attributes = $postValues['super_attribute'];$simple_product = $this->configurableproduct->getProductByAttributes($attributes, $addProduct);}}}
That’s it! Likewise you can get simple product ID from configurable product in Magento 2 and learn to get child product ID of configurable product by swatch options using jQuery.
Hope the readers find the easy solution right away unlike me to satisfy a client or do their Magento 2 development task quickly with this method!
If you get stuck in between, don’t hesitate to discuss your doubts with me in the Comments section below. I’d be happy to help 😊
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Create Console Command in Magento 2
8 Ways to Check Magento Version
Next