Reading list Switch to dark mode

    How to get configurable associated products id in magento2

    Updated 15 July 2016

    Here we’ll learn that how to get configurable associated products id  in magento2

    Step1# : I’ll create Data.php class file in app/code/NameSpace/ModuleName/Helper

    <?php
    
    /**
     * TestModule helper data
     * @category  Webkul
     * @package   Webkul_TestModule
     * @author    Webkul
     * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    namespace NameSpace\ModuleName\Helper;
    
    use Magento\Catalog\Model\Product;
    use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableProTypeModel;
    
    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
        /**
         * @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable
         */
        private $_configurableProTypeModel;
    
        /**
         * @var \Magento\Catalog\Model\Product
         */
        private $_product;
    
        /**
         * @param ConfigurableProTypeModel $configurableProTypeModel,
         * @param Product                  $product,
         */
        public function __construct(
            ConfigurableProTypeModel $configurableProTypeModel,
            Product $product
        ) {
            $this->_configurableProTypeModel = $configurableProTypeModel;
            $this->_product = $product;
            parent::__construct($context);
        }
    
        /**
         * return Congigurable associated product id.
         * @param object $productId
         * @param array  $nameValueList
         * @return bool|int
         */
    
        /* array structure 
           $nameValueList = [['Name'=>'Ram','Value'=>'3GB'],['Name'=>'Color','Value'=>'Red']] ;
        */
        public function getConfAssoProductId($productId, $nameValueList)
        {
            $assocateProId = false;
            //load product
    
            $product = $this->_product->load($productId);
            //get configurable products attributes array with all values with lable (supper attribute which use for configuration) 
            $optionsData = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
            $superAttrList = [];
            $superAttrOptions = [];
            $attributeValues = [];
    
            // prepare array with attribute values 
            foreach ($optionsData as $option) {
                $superAttrList[] = [
                    'name' => $option['frontend_label'],
                    'code' => $option['attribute_code'],
                    'id' => $option['attribute_id']
                ];
                $superAttrOptions[$option['attribute_id']] = $option['options'];
                foreach ($nameValueList as $nameValue) {
                    if ($nameValue['Name'] == $option['frontend_label']) {
                        foreach ($option['options'] as $attrOpt) {
                            if ($nameValue['Value'] == $attrOpt['label']) {
                                $attributeValues[$option['attribute_id']] = $attrOpt['value'];
                            }
                        }
                    }
                }
            }
    
            if (count($attributeValues) == count($nameValueList)) {
                // pass this prepared array with $product 
                $assPro = $this->_configurableProTypeModel->getProductByAttributes($attributeValues, $product);
                // it return complete product accoring to attribute values which you pass
                $assocateProId = $assPro->getEntityId();
            }
            return $assocateProId;
        }
    }
    

    Thanks 🙂

    Searching for an experienced
    Magento 2 Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    3 comments

  • Petter Granberg
    Hi,
    Great snippet! I’m looking for the opposite. Get the parent SKU (configurable) from simple product id or product sku.
    • Webkul Support
      Sure. Will try to write another article on it soon.
      • Petter Granberg
        Wow, quick answer! That would be great. I’m trying to do this in a Block Class and then echo in a template file.
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home