{"id":466,"date":"2019-06-17T09:30:20","date_gmt":"2019-06-17T09:30:20","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/06\/17\/get-simple-product-from-magento-2-configurable-product-before-add-to-cart\/"},"modified":"2025-05-22T16:34:01","modified_gmt":"2025-05-22T11:04:01","slug":"get-simple-product-from-magento-2-configurable-product-before-add-to-cart","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-simple-product-from-magento-2-configurable-product-before-add-to-cart\/","title":{"rendered":"How to Get Simple Product from Magento 2 Configurable Product Before Add to Cart"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As a Magento 2 developer myself, I recently came across a client who sells the configurable products having&nbsp;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&nbsp;to&nbsp;<strong><em>get simple product from Magento 2 configurable product Before add to cart<\/em><\/strong>&nbsp;and set up the restriction accordingly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Simple Product from Magento 2 Configurable Product Before Add to Cart\u201d<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>events.xml<\/strong>&nbsp;at&nbsp;<em><strong><em><strong>app\\code\\Vendor\\Extension\\etc\\<\/strong><\/em><\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\">\n    &lt;event name=\"controller_action_predispatch_checkout_cart_add\">\n        &lt;observer name=\"restrict_sales_model_cart_add_before\" instance=\"Vendor\\Extension\\Observer\\Cartadd\"\/>\n    &lt;\/event>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>Cartadd.php<\/strong>&nbsp;at&nbsp;<em><em><strong><em><em><strong>app\/code\/Vendor\/Extension\/Observer\/<\/strong><\/em><\/em><\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Observer;\nuse Magento\\Framework\\Event\\ObserverInterface;\nuse Magento\\Framework\\App\\Response\\RedirectInterface;\nuse Magento\\Checkout\\Model\\Cart;\nuse Magento\\Framework\\App\\RequestInterface;\nuse Magento\\Catalog\\Model\\Product;\nuse Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable;\nclass Cartadd implements ObserverInterface\n{\n    protected $cart;\n    protected $redirect;\n    protected $request;\n    protected $product;\n    protected $configurableproduct;\n    public function __construct(RedirectInterface $redirect, Cart $cart, RequestInterface $request, Product $product, Configurable $configurableproduct)\n    {\n        $this->redirect = $redirect;\n        $this->cart = $cart;\n        $this->request = $request;\n        $this->product = $product;\n        $this->configurableproduct = $configurableproduct;\n    }\n    public function execute(\\Magento\\Framework\\Event\\Observer $observer)\n    {\n        $postValues = $this->request->getPostValue();\n        $productId = $postValues['product'];\n        $addProduct = $this->product->load($productId);\n        if ($addProduct->getTypeId() == \\Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable::TYPE_CODE) \n        {\n            $attributes = $postValues['super_attribute'];\n            $simple_product = $this->configurableproduct->getProductByAttributes($attributes, $addProduct);\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it! Likewise you can&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/get-simple-product-id-from-configurable-product-in-magento-2\/\">get simple product ID from configurable product in Magento 2<\/a>&nbsp;and learn to get child product ID of configurable product by swatch options using jQuery.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-466","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=466"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/466\/revisions"}],"predecessor-version":[{"id":15377,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/466\/revisions\/15377"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}