【问题标题】:How to get simple product from configurable product in magento 2如何从magento 2中的可配置产品中获取简单产品
【发布时间】:2021-12-22 17:48:25
【问题描述】:

我想从产品详细信息和类别页面中的可配置产品中获取所选样本的产品 ID。

【问题讨论】:

  • 你能说明你尝试了什么,你遇到了什么错误吗?

标签: magento2 magento2.4


【解决方案1】:

以下示例代码演示了如何通过对象管理器加载可配置产品并将所有子产品(关联的简单产品)作​​为数组获取。

$configProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);
 
$_children = $configProduct->getTypeInstance()->getUsedProducts($configProduct);
foreach ($_children as $child){
    echo "Here are your child Product Ids ".$child->getID()."\n";
}
echo "count: ".count($_children);

【讨论】:

    【解决方案2】:
    You can print your child products ids ( of configurable products) via making a small change to your code as follow
    
    foreach($collection as $_product) {
            $logger->info("Here are Parent Product Name".$_product->getName());
            $_children = $_product->getTypeInstance()->getUsedProducts($_product);
            foreach ($_children as $child){
                $logger->info("Here are your child Product Ids ".$child->getID());
            }
        }
    
    After this look at to your log files and you will have your child IDS.
    

    【讨论】:

      猜你喜欢
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多