Example #1
0
 /**
  * Retrieve product attribute
  *
  * @param string $attributeCode
  * @param int|array $productIds
  * @param string $storeId
  * @return array
  */
 public function _getProductAttribute($attributeCode, $productIds, $storeId)
 {
     $adapter = $this->_getReadAdapter();
     if (!isset($this->_productAttributes[$attributeCode])) {
         $attribute = $this->productResource->getAttribute($attributeCode);
         $this->_productAttributes[$attributeCode] = array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getId(), 'table' => $attribute->getBackend()->getTable(), 'is_global' => $attribute->getIsGlobal());
         unset($attribute);
     }
     if (!is_array($productIds)) {
         $productIds = array($productIds);
     }
     $bind = array('attribute_id' => $this->_productAttributes[$attributeCode]['attribute_id']);
     $select = $adapter->select();
     $attributeTable = $this->_productAttributes[$attributeCode]['table'];
     if ($this->_productAttributes[$attributeCode]['is_global'] || $storeId == 0) {
         $select->from($attributeTable, array('entity_id', 'value'))->where('attribute_id = :attribute_id')->where('store_id = ?', 0)->where('entity_id IN(?)', $productIds);
     } else {
         $valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
         $select->from(array('t1' => $attributeTable), array('entity_id', 'value' => $valueExpr))->joinLeft(array('t2' => $attributeTable), 't1.entity_id = t2.entity_id AND t1.attribute_id = t2.attribute_id AND t2.store_id=:store_id', array())->where('t1.store_id = ?', 0)->where('t1.attribute_id = :attribute_id')->where('t1.entity_id IN(?)', $productIds);
         $bind['store_id'] = $storeId;
     }
     $rowSet = $adapter->fetchAll($select, $bind);
     $attributes = array();
     foreach ($rowSet as $row) {
         $attributes[$row['entity_id']] = $row['value'];
     }
     unset($rowSet);
     foreach ($productIds as $productId) {
         if (!isset($attributes[$productId])) {
             $attributes[$productId] = null;
         }
     }
     return $attributes;
 }