How to find out the master product of a simple product?

How can I find out if a simple product is part of a custom product and then get a master product? I need this for a product listing.

Just found out:

$_product->loadParentProductIds(); $parentIds = $_product->getParentProductIds(); 
+4
source share
3 answers

Say you have a simple Product Product identifier.

To get all the parent custom product identifiers of this simple product, use the following code: -

 <?php $_product = Mage::getModel('catalog/product')->load(YOUR_SIMPLE_PRODUCT_ID); $parentIdArray = $_product->loadParentProductIds() ->getData('parent_product_ids'); if(!empty($parentIdArray)) { // currently in the master configurable product print_r($parentIdArray); // this prints all the parent product IDs using your simple product. } ?> 

I suppose that should make you.

+5
source

After version 1.4.2.0, the methods loadParentProductIds() and getParentProductIds() are deprecated. Do not ask me why. Personally, I liked these methods. Therefore, I returned them to my local Mage classes. Here's how to do it: Copy

 app/code/core/Mage/Catalog/Model/Product.php 

to

 app/code/local/Mage/Catalog/Model/Product.php 

and change the loadParentProductIds() method found around line 1349 to:

 public function loadParentProductIds() { return $this->_getResource()->getParentProductIds($this); } 

This piece of code will request its resource for its parent product identifiers. To do this, we will need to rewrite the getParentProductIds() method in the resource class.

So copy:

 app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product.php 

to

 app/code/local/Mage/Catalog/Model/Resource/Eav/Mysql4/Product.php 

Find the deprecated getParentProductIds() method. It should be somewhere near line 535. Overwrite it with code up to 1.4.2.0:

 public function getParentProductIds($object){ $childId = $object->getId(); $groupedProductsTable = $this->getTable('catalog/product_link'); $groupedLinkTypeId = Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED; $configurableProductsTable = $this->getTable('catalog/product_super_link'); $groupedSelect = $this->_getReadAdapter()->select() ->from(array('g'=>$groupedProductsTable), 'g.product_id') ->where("g.linked_product_id = ?", $childId) ->where("link_type_id = ?", $groupedLinkTypeId); $groupedIds = $this->_getReadAdapter()->fetchCol($groupedSelect); $configurableSelect = $this->_getReadAdapter()->select() ->from(array('c'=>$configurableProductsTable), 'c.parent_id') ->where("c.product_id = ?", $childId); $configurableIds = $this->_getReadAdapter()->fetchCol($configurableSelect); return array_merge($groupedIds, $configurableIds); } 

Now you can do it again:

 $_product->loadParentProductIds()->getData('parent_product_ids'); 

Hope this helps you!

+4
source

For Magento 1.4.2 and later, use the following method instead:

 $configurable_product_model = Mage::getModel('catalog/product_type_configurable'); $parentIdArray = $configurable_product_model->getParentIdsByChild($simple_product_id); 
+4
source

Source: https://habr.com/ru/post/1309950/