从Child Sku Magento 2获取父sku(可配置或捆绑包)



在订单页面上(后端(我需要能够使用子sku获得父sku。

我尝试了从Magento论坛上删除的几个代码和stackoverflow上的类似问题,但没有成功。

我能够通过使用getTypeId()来确定产品是否只是一个没有父母的简单产品,但是在那之后,我尝试的一切都无法在父母的sku上获得。

Magento 2.2.6

可配置,捆绑包和分组都是不同的,需要不同的手段来获得父母。这些方法在相关文件中列出。

Magento Configurable Product Model Product type type configurable.php

/** 
* Retrieve configurable parent ids array by required child
*
* @param  int|array $childId
* @return array
*/
public function getParentIdsByChild($childId)
{
   return $this->_catalogProductTypeConfigurable->getParentIdsByChild($childId);
} 

Magento Bundle Model Product type.php

/**
* Retrieve bundle parent ids array by required child
*
* @param int|array $childId
* @return array
*/
public function getParentIdsByChild($childId)
{
   return $this->_bundleSelection->getParentIdsByChild($childId);
}

Magento GroupedProduct Model Product type type grouped.php

/**
* Retrieve grouped parent ids array by requested child
*
* @param int|array $childId
* @return array
*/
public function getParentIdsByChild($childId)
{
   return $this->productLinks->getParentIdsByChild(
       $childId,
       MagentoGroupedProductModelResourceModelProductLink::LINK_TYPE_GROUPED
  );
}

最后,如果您需要快速从mysql获取SKU。

#Select parent of a sku
 SELECT
  parent.sku
FROM
  catalog_product_entity parent
WHERE parent.row_id =
  (SELECT
    relationship.parent_id
  FROM
    catalog_product_super_link relationship
  WHERE relationship.product_id =
    (SELECT
      child.entity_id
    FROM
      catalog_product_entity child
    WHERE child.sku = "SKU")
  );

最新更新