Created
August 16, 2016 12:43
-
-
Save shakhmehedi/77d4ea601fafa2ded40767693b39e0f5 to your computer and use it in GitHub Desktop.
Magento 1: Get Configurable Product ids/skus from simple product
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Return parent product id(s) array. | |
* | |
* @param Mage_Catalog_Model_Product $product | |
* @return array|bool|null | |
*/ | |
public function getParentIds(Mage_Catalog_Model_Product $product) | |
{ | |
$id = $product->getId(); | |
$type = $product->getTypeId(); | |
$parentIds = null; | |
if ($type == 'simple') { | |
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable') | |
->getParentIdsByChild($id); | |
} | |
if (count($parentIds)) { | |
return $parentIds; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Returns parent skus | |
* | |
* @param Mage_Catalog_Model_Product $product | |
* @param bool $asString if set to 'true', skus as comma separated string. | |
*/ | |
public function getParentSkus(Mage_Catalog_Model_Product $product, $asString = false){ | |
$parentIds = $this->getParentIds($product); | |
$products = Mage::getModel('catalog/product')->getCollection() | |
->addAttributeToSelect(array('sku')) | |
->addIdFilter($parentIds); | |
$parentSkus = null; | |
foreach ($products as $product) { | |
$parentSkus[]=$product->getSku(); | |
} | |
if($asString){ | |
return implode('|',$parentSkus); | |
}else{ | |
return $parentSkus; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment