Last active
September 15, 2016 09:36
-
-
Save Bonno/b4aa850c9d22b4786d009697cc1b3440 to your computer and use it in GitHub Desktop.
Simple and light way to build an array with the values of an collection withoud loading the complete objects in Magento
This file contains 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
/** @var array $_productSkus */ | |
protected $_productSkus; | |
/** | |
* Return the sku for a given productId | |
* | |
* @param $productId | |
* @return bool | |
*/ | |
public function getProductSku($productId) | |
{ | |
if (empty($this->_productSkus)) { | |
$collection = Mage::getModel('catalog/product')->getCollection() | |
->addAttributeToSelect(array('sku'), 'inner') | |
->addAttributeToFilter('type_id', array(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)); | |
$select = $collection->getConnection() | |
->query($collection->getSelect()); | |
while ($row = $select->fetch()) { | |
$this->_productSkus[$row['entity_id']] = $row['sku']; | |
} | |
unset($select, $collection); | |
} | |
if (isset($this->_productSkus[$productId])) { | |
return $this->_productSkus[$productId]; | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment