Skip to content

Instantly share code, notes, and snippets.

@Bonno
Last active September 15, 2016 09:36
Show Gist options
  • Save Bonno/b4aa850c9d22b4786d009697cc1b3440 to your computer and use it in GitHub Desktop.
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
/** @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