0

I have configure product id 2671

And selected attributes with value

  • attributes id 280[color] and selected value 142[Black]
  • attributes id 757[size] and selected value 181[M]

Based on these values how can I get simple product id of above selected attributes ids and values.

  • On attribute select, did you get configurable product id? – Rohan Hapani Apr 3 at 8:34
  • Yes, configurable product id is 2671 – Aditya Shah Apr 3 at 8:53
2

Magento provides function for that, by passing attributes in array format you can get simple product of those variants.

/magento/module-configurable-product/Model/Product/Type/Configurable.php

  • product id 2671

And selected attributes with value

  • attributes id 280[color] and selected value 142[Black]
  • attributes id 757[size] and selected value 181[M]

Retrieve used product by attribute values

public function __construct(
    \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurableProTypeModel,
    \Magento\Catalog\Model\Product $product
) {
    $this->_configurableProTypeModel = $configurableProTypeModel;
    $this->_product = $product;
}

$product = $this->productRepository->getById($product_id);
// Valid format for attributes 
//$attributesInfo = array( $attributeId => $attributeValue);
$attributesInfo = array( 280 => 145,757=>181);

$associateProduct = $this->_configurableProTypeModel->getProductByAttributes($attributesInfo, $product);
1

You can use this below code to get simple product id based on configurable product ID.

If you get value of selected configurable product id in js file, then you can pass value using ajax and call controller to get simple product id. If you want to get simple product id by compare value of selected attribute value, then you can use commented code also.

Add this below code in your controller :

<?php
namespace RH\Blog\Controller\Index;

class Test extends \Magento\Framework\App\Action\Action {

    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;
    /**
     * [__construct description]
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository [description]
     * @param \Magento\Store\Model\StoreManagerInterface      $storeManager      [description]
     */
    public function __construct(
        ....
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManager
        ....
    ) {
        ....
        $this->productRepository = $productRepository;
        $this->storeManager = $storeManager;
        ....
    }

    public function execute() {
        $configurable_id = '2671';
        $product = $this->productRepository->getById($configurable_id);
        $storeId = $this->storeManager->getStore()->getId();
        $productTypeInstance = $product->getTypeInstance();
        $productTypeInstance->setStoreFilter($storeId, $product);
        $usedProducts = $productTypeInstance->getUsedProducts($product);

        foreach ($usedProducts as $child) {
            echo "Simple Product ID : ".$child->getId() . "<br/>";
            // $selected_option_val = $child->getAttributeText('color');
            // if ($selected_option_val == 'your_selected_attr_val') {
            //  return $child->getId();
            // }
        }
    }
}
  • Thanks for the answer +1 for the efforts :) – Aditya Shah Apr 4 at 5:49
1

Try this.

public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Catalog\Model\ResourceModel\Eav\Attribute $eavAttribute,
    \Magento\Catalog\Model\ProductRepository $productRepository
) {
    $this->storeManager = $storeManager;
    $this->eavAttribute = $eavAttribute;
    $this->productRepository = $productRepository;
}

public function getSimpleProductId()
{
    $simple_product_id = null;
    $configurable_product_id = 2671;
    $color_id = 280;
    $size_id = 757;

    $color_value = 142;
    $size_value = 181;

    $color_code = $this->eavAttribute->load($color_id)->getAttributeCode();
    $size_code = $this->eavAttribute->load($size_id)->getAttributeCode();

    $product = $this->getProductById($configurable_product_id);
    $productTypeInstance = $product->getTypeInstance();
    $simple_products = $productTypeInstance->getUsedProducts($product);   
    foreach ($simple_products  as $simple)
    {
        if($simple->getData($color_code) == $color_value && $simple->getData($size_code) == $size_value)
        {
            $simple_product_id = $simple->getId();
        }
    }

    return $simple_product_id;
}

public function getProductById($id)
{
    return $this->productRepository->getById($id, false, $this->storeManager->getStore()->getId());
}
  • Thanks for the answer +1 for the efforts :) – Aditya Shah Apr 4 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.