2

I have configurable product with 3 colors(red,Blue,green) in drop down.If I have selected red color product,How to get red color product Id in product page

If anyone knows please help me

  • you want that id in js or in php class? – Ronak Chauhan Dec 19 '17 at 8:55
  • php class...... – Mano M Dec 19 '17 at 8:56
  • You can append product id in swatch create function in swatch-renderer.js so you can have that id while you select that option. – Ronak Chauhan Dec 19 '17 at 9:03
  • please show your code where you want to get product id. – Rakesh Jesadiya Dec 19 '17 at 12:18
  • updated my post – Mano M Dec 19 '17 at 12:23
4

We can use this function to get the child product of configurable product.
We need to pass the configurable product id, attribute code and value.

public function getChildProductOfConfigByAttrValue($configProdId, $attr_code, $attr_value){
$product = $this->productRepository->getById($configProdId);
$storeId = $this->_storeManager->getStore()->getId();

$productTypeInstance = $product->getTypeInstance();
$productTypeInstance->setStoreFilter($storeId, $product);
$usedProducts = $productTypeInstance->getUsedProducts($product);     

foreach ($usedProducts  as $child) {

            $customValue = $child->getAttributeText($attr_code);
            if($customValue == $attr_value){
                return $child->getId();
            }

    }
}
  • $value means..how to get value? – Mano M Dec 20 '17 at 7:06
  • $value will be 'red' for your case. – Ankur Verma Dec 20 '17 at 7:07
  • $attr will be 'color'. – Ankur Verma Dec 20 '17 at 7:09
  • OK.I will try this... – Mano M Dec 20 '17 at 7:10
  • 1
    // You can get it from the cart object. It will give you the simple product id of congifurable product. You just need to pass the configurable product id. Like-> $parent_product_id = 10; foreach ($cart->getQuote()->getAllVisibleItems() as $item) { $productId = $item->getProduct()->getId(); if($productId == $parent_product_id){ if ($option = $item->getOptionByCode('simple_product')) { $option->getProduct()->getId(); } } } – Ankur Verma Dec 20 '17 at 7:36

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.