---
title: "Magento 2: Get Simple Product from Configurable Product before Add to Cart"
url: "https://magecomp.com/blog/magento-2-get-simple-product-from-configurable-product-before-add-to-cart/"
date: "2021-11-29T10:00:06+00:00"
modified: "2025-02-18T05:28:06+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 451
reading_time: "3 min read"
summary: "Hello Magento Friends,"
description: "Get Simple Product from Configurable Product before Add to Cart in Magento 2 to verify whether it is already in cart or not. Learn more"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Show Payment Methods for Particular Categories in Magento 2"
    url: "https://magecomp.com/blog/show-payment-methods-for-particular-categories-magento-2/"
  - title: "Get Active Payment Methods with Multi select in Backend"
    url: "https://magecomp.com/blog/get-active-payment-methods-with-multi-select-in-backend/"
  - title: "How to Display Tier Price at Category Page in Magento 2"
    url: "https://magecomp.com/blog/display-tier-price-at-category-page-magento-2/"
---

# Magento 2: Get Simple Product from Configurable Product before Add to Cart

_Published: November 29, 2021_  
_Author: Dhiren Vasoya_  

![Get Simple Product from Configurable Product before Add to Cart In M2](https://magecomp.com/blog/wp-content/uploads/2021/11/Get-Simple-Product-from-Configurable-Product-before-Add-to-Cart-In-M2-1024x512.webp)

**Hello Magento Friends,**

Today I am here with a solution for Magento 2: Get Simple Product from Configurable Product before Add to Cart.
[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2-2.webp)](https://magecomp.com/services/magento-upgrade-services/)

A configurable product is a set of simple products that can be bought together or separately. You can [**Create Configurable Product Programmatically in Magento 2**](https://magecomp.com/blog/create-configurable-product-programmatically-in-magento-2/).
When working with configurable products, you need to check simple product attributes with the products already in the cart in order to determine whether to add them to the shopping cart or not. For this, you need to get the simple product from the configurable product before adding it to the cart in Magento 2.
Let’s learn the steps for How to Get Simple Product from Configurable Product before Add to Cart in Magento 2.
## Steps to Get Simple Product from Configurable Product before Add to Cart in Magento 2:
**Step 1:** Go to the below file Path
**app\code\Vendor\Extension\etc\events.xml**

Now, add the code as follows
```
<?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="simple_product_cart_add_before" instance="VendorExtensionObserverCartadd"/>
    </event>
</config>
```

**Step 2:** Next move to the following file path
**app\code\Vendor\Extension\Observer\Cartadd.php**

And add the code as mentioned below
```
namespace VendorExtensionObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppResponseRedirectInterface;
use MagentoCheckoutModelCart;
use MagentoFrameworkAppRequestInterface;
use MagentoCatalogModelProduct;
use MagentoConfigurableProductModelProductTypeConfigurable;

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(MagentoFrameworkEventObserver $observer)
    {
         $postValues = $this->request->getPostValue();
         $productId = $postValues['product'];
         $addProduct = $this->product->load($productId);

         if ($addProduct->getTypeId() == MagentoConfigurableProductModelProduct	TypeConfigurable::TYPE_CODE)
         {
              $attributes = $postValues['super_attribute'];
              $simple_product =
	             $this->configurableproduct->getProductByAttributes($attributes, 	$addProduct);
	             echo $simple_product->getID();
         }
     }
}
```

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-4.webp)](https://magecomp.com/magento-malware-removal.html)

**Step 3:** After completing the above steps, run the below commands
```
sudo php bin/magento setup:di:compile
sudo php bin/magento cache:flush
```

## Conclusion:
This way you can Get Simple Product from Configurable Product before Add to Cart in Magento 2. Also, check out [Magento 2 Sticky Add To Cart](https://magecomp.com/magento-2-sticky-add-to-cart.html) extension for displaying an appealing sticky bar and Add to Cart button. For any queries, mention in the comment box without hesitation. See you next time with another solution till then stay with us!
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-2.webp)](https://magecomp.com/magento-performance-optimization.html)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/magento-2-get-simple-product-from-configurable-product-before-add-to-cart/](https://magecomp.com/blog/magento-2-get-simple-product-from-configurable-product-before-add-to-cart/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-10-08 09:03:51 UTC_  
