magento-2-get-simple-product-from-configurable

How To Get A Simple Product From Configurable In Magento 2 

by Trang Tran

As a Magento 2 store owner, you must have been confused in finding the suitable product types, especially with two popular types: simple products with custom options or configurable products.

We can not say whether simple products with custom options or configurable products are better since it depends on the situation and your catalog. Ideally, we have a solution that combines both types but remains a simple product attribute and seamless checkout. Magento 2 get simple product from configurable is a savior!

Let’s dive into the guide now!

When Do You Need to Get Simple Products From Configurable Products?

Magento 2 configurable products bring many benefits for store owners, such as efficient inventory management, quick generation, better performance, etc. However, in some circumstances, you need to get the simple product from configurable. This method helps you display product data stored in simple product attributes, which is essential for frontend pages.

In fact, many Magento 2 stores sell configurable products that can be combined with simple products to enable several payment options. Your customer can purchase a combination via a regular payment option, but another can only be purchased once. 

magento-2-get-simple-product-from-configurable

However, the recurring product cannot be used with other payment methods. Hence, before adding a product to the cart, Magento 2 get simple product from configurable is necessary to configure the restriction appropriately.

On the other hand, when working with configurable products, you must compare simple product attributes with the items already in the shopping basket to decide whether or not to add them. In order to do this in Magento 2, you must first get the simple product from the configurable one.

Then, let’s learn how to get simple product from configurable in Magento 2.

Step-By-Step to Get a Simple Product From Configurable in Magento 2

First, let’s clarify that this is unavailable in Magento 2 default. Thus, if you want to do this technique, you need coding experience. Otherwise, you can hire a developer or ask the agency to help build your website.

Step 1: Create events.xml at app\code\Vendor\Extension\etc\

Enter the following file location: app\code\Vendor\Extension\etc\events.xml

Next, add the following code:

<?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=”Vendor\Extension\Observer\Cartadd”/>
    </event>
</config>

Step 2: Create Cartadd.php at app/code/Vendor/Extension/Observer/

In the next step, navigate to the following file path.

app\code\Vendor\Extension\Observer\Cartadd.php

Additionally, add the code as shown below.

<?php
namespace 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);
        }
    }
}

Step 3: Run commands

Finally, run the following instructions after finishing the procedures mentioned above.

sudo php bin/magento setup:di:compile
sudo php bin/magento cache:flush

As you can see, all three steps require adding code to Magento 2. Consider an alternative way to show simple products without coding requirements. We have a suggestion for you below! 

Go Further With Magento 2 Simple Details on Configurable Product

Magento 2 Simple Details on Configurable Product is an optimized solution to assist store owners in streamlining the appearance of their configurable items. It creates customizable products by giving each one a distinct identity and showing information about each child item on a single page. Customers may quickly find the child product based on colors, sizes, and other characteristics.

magento-2-get-simple-product-from-configurable

Specifically, customers can view all information for a single selected option without refreshing the page. Six qualities help generate distinctive product identities:

  • URL and metadata
  • Name and SKU
  • Price, special price, and tier price
  • Images
  • Short description and Full description
  • Include the “more information” tab
  • Stock status and quantity

Moreover, using this module will enhance your SEO performance. Obtaining a higher search engine rating depends heavily on metadata. It would be better to optimize the information for each product to prevent duplicating meta descriptions, meta titles, and meta keywords.

The product extension can automatically produce child product metadata. Therefore, you have a higher likelihood of increasing website visitors.

Features:

  • Offer dynamic content updates: name, SKU, descriptions, and other attributes;
  • Generate a URL for each selected option;
  • Import Pre-select;
  • Show child product names at Dynamic Category/Product Listing Update;
  • Enable Review Swatch;
  • Set custom options for associated simple products.
magento-2-get-simple-product-from-configurable

>>> DOWNLOAD Magento 2 Simple Details on Configurable Product to enrich customer experience now!

Levering this module can save you time and effort compared to manually adding code. It can reduce the risks of not being familiar with coding. Besides, with only one app, you can improve customer buying experience with visual showing, SEO performance, and marketing campaigns.

Of course, it costs you more than manually. However, instead of monthly payments, Magento 2 Simple Details on Configurable Product will offer you a one-time payment, free lifetime update, and no hidden fee.

Conclusion

The post has clarified the manual way of getting the simple products from configurable. Also, we suggest the alternative to enhance your configurable product page. Ideally, you can use both ways at the same time. 

Generally, Magento 2 getting simple products from a configurable technique is essential if you want to improve customer satisfaction. 

BSS Commerce is one of the leading Multi-platform eCommerce solutions and web development services providers worldwide. With experienced and certified developers, we commit to bringing high-quality products and services to optimize your business effectively.

CONTACT NOW to let us know your problems. We are willing to support you every time.

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.