Let’s assume you know configurable product id and used child product super attribute details.
Example from Magento Sample data configurable product name Chaz Kangeroo Hoodie, Configurable product id is 67.
Super attribute Array details,

[super_attribute] => Array
(
    [93] => 49
    [254] => 167
)

Using the bove details how we can get details of child item.

Refer below code snippet for Get child item id of a Configurable product by child’s Attribute details.

<?php
public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable
) {
    $this->productRepository = $productRepository;
    $this->configurable = $configurable;
}
/* Pass Configurable product id as $configId 
 * Pass array of super attribute of child item
 */
public function getChildFromProductAttribute($configId,$superAttribute) {
    $_configProduct = $this->productRepository->getById($configId);
    $usedChild = $this->configurable->getProductByAttributes($superAttribute ,$_configProduct);
    $childProductId = $usedChild->getId();
    return $childProductId;
}

Call function,

$configId = 67; //Configurable Product id
$superAttribute = Array(93 => 49,254 => 167); //childs super attribute details
$childId = $this->getChildFromProductAttribute($configId,$superAttribute);

Result is 52(Product name Chaz Kangeroo Hoodie-XS-Black). Using above way we got the child item data.


yanping
24 声望0 粉丝

« 上一篇
链接#的参数
下一篇 »
remove reivew

引用和评论

0 条评论