1

On the order page(backend) I need to be able to obtain the parent SKU using a child SKU.

I've tried several code snipped from both the Magento forums and similar questions here on StackOverflow without success.

I'm able to determine if a product is just a simple product without a parent by using getTypeId() but after that everything I try fails to result in getting at the parent SKU.

Magento 2.2.6

Til
  • 5,150
  • 13
  • 26
  • 34
user4817258
  • 135
  • 1
  • 10
  • Magento has its own stack exchange with lots of great thorough answers from magento professionals. https://magento.stackexchange.com/ – willboudle Dec 20 '19 at 17:46

1 Answers1

2

Configurable, bundle and grouped are all different and require different means to get their parents. The methods are listed within the relevant files.

Magento\ConfigurableProduct\Model\Product\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\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,
       \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED
  );
}

Lastly, in case you need to quickly get the sku from mysql .

#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")
  );
willboudle
  • 274
  • 5
  • 18
  • I am trying Retrieve bundle parent ids follow your way but this alway return array empty. Maybe it's wrong – huykon225 May 30 '21 at 04:57
  • @huykon225 not sure about the code, but the query needs to be edited to work. There is no such field as 'row_id', I think he meant 'entity_id'. This worked for me: SELECT parent.`entity_id`, parent.`sku` FROM catalog_product_entity parent WHERE parent.`entity_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") ); – Mohammed Joraid Aug 28 '22 at 20:29
  • @MohammedJoraid, row_id is actually from Adobe Commerce ( the enterprise version). entity_id still exists, but row_id is correct. – willboudle Sep 20 '22 at 03:58
  • @willboudle yes, that is correct. Thank you for the updates. As you said the row_id is available on newer Adobe Commerce EE version. Mine is CE and bit older so I don't have the row_id. Does that mean the SKU is no longer the unique identifier for the entity product? – Mohammed Joraid Oct 26 '22 at 07:40
  • 1
    @MohammedJoraid, because Adobe Commerce has scheduling capabilities, there are two identifiers. entity_id and row_id. entity_id will never actually change on a sku, they are a 1:1. But row_id is 1:many. A sku could have scheduled changes, and row_id reflects that. https://magento.stackexchange.com/questions/278051/row-id-vs-entity-id-in-magento-2-3-ee – willboudle Oct 27 '22 at 15:41
  • @willboudle thank you sir for the valuable info. Appreciated. – Mohammed Joraid Nov 10 '22 at 07:20