如何从可配置产品获取子产品ID

时间:2018-12-27 08:57:08

标签: magento2

我正在尝试从可配置产品ID中提取所有子产品ID。

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
        $product = $objectManager->create('\Magento\Catalog\Model\Product');



        $storeManager = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface'); 
        $currentStore = $storeManager->getStore();
        $mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
        $productURL = $mediaUrl.'catalog/product';
        $collection = $this->_productCollectionFactory->create();
        $productCollection = $collection->addAttributeToSelect('*')->addAttributeToFilter('type_id','configurable');
        $vendor_product = array();

        $productData = $product->getData();
        foreach($productCollection as $prodObj){
            $productData = array();
            $product = $product->load($prodObj->getId());

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

 foreach ($usedProducts  as $child) {
$productData['childrenIds'][] = $child->getId();
}
  

在所有情况下,我都会获得我的第一个可配置产品的ID

5 个答案:

答案 0 :(得分:1)

要从可配置产品中获取子产品ID,可以使用以下代码:

 public function __construct(
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }


    public function execute()
    { 
        $_productId=57786;
        $_product=$this->productRepository->getById($_productId);
        $childIs=$_product->getExtensionAttributes()->getConfigurableProductLinks();

        print_r($childIs);
    }

输出如下:

Array
(
    [31981] => 31981
    [31982] => 31982
    [31983] => 31983
)

希望它会对您有所帮助。

答案 1 :(得分:0)

<?php

namespace Vendor\Module\Block;


class ParentAndChilds extends \Magento\Framework\View\Element\Template
{

    /**
    * @var Context
    */

    protected $context;

    /**
    * @var ProductRepositoryInterface
    */

    protected $productRepository;

    /**
    * @var SearchCriteriaBuilder
    */

    protected $searchCriteriaBuilder;

    /**
     * @var LinkManagementInterface
     */

    protected $linkManagement;


    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\ConfigurableProduct\Api\LinkManagementInterface $linkManagement,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        array $data = []

    )
    {
        $this->linkManagement = $linkManagement;
        $this->productRepository = $productRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        parent::__construct($context, $data);
    }



    public function getParentsAndChilds() 
    {
            $searchCriteria = $this->searchCriteriaBuilder
                ->addFilter('type_id', 'configurable')
                ->create();

            $configurableProducts = $this->productRepository
                ->getList($searchCriteria);

            $parentAndChildProducts = array();
            foreach ($configurableProducts->getItems() as $configurableProduct) {
                $childProducts = $this->linkManagement
                    ->getChildren($configurableProduct->getSku());

                foreach ($childProducts as $childProduct) {
                    $parentAndChildProducts[$configurableProduct->getId()][] = $childProduct->getId();
                }
            }

            return $parentAndChildProducts;
    }

}

答案 2 :(得分:0)

$_children = $_product->getTypeInstance()->getUsedProducts($_product); foreach ($_children as $child){ $childProducts[] = $child; }

$ _ product是可配置的产品对象

答案 3 :(得分:0)

您可以使用以下代码获取儿童ID。

$product = $block->getProduct();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$configurable_product_id = $product->getId();
$configurableProduct = $objectManager->get('Magento\Catalog\Model\ProductRepository')->getById($configurable_product_id);

$children = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);       $childIds = array();
foreach ($children as $child){
  $childIds[] = $child->getId();
}
sort($childIds);
print_r($childIds);

这是示例代码,但我建议不要直接使用$objectManage r,您可以使用“插件上的块方法”来获取产品对象。

答案 4 :(得分:0)

要让所有简单的儿童(包括缺货产品)使用:

$children = $product
          ->getTypeInstance()
          ->getChildrenIds($product->getId());

          print_r($children);