I have a ds_product module where for each product we can have questions and in the backend we can see the questions I want to add the sku to the grid so in app/code/local/DS/ProductFAQ/Block/Adminhtml/productfaq/Grid.php
here is what I did:
$this->addColumn('sku', array(
'header' => Mage::helper('ds_productfaq')->__('SKU'),
'align' =>'left',
'index' => 'sku',
));
I have a column with name sku now but the column is empty I wanted to know where and what I should search to make it work, as I searched Mage::helper('ds_productfaq')->__('SKU'), this line return a class, which inherits Mage_Core_Helper_Abstract. but I don't know what to do next
It seems that I should change the collection in a way so that the sku would be selected like name and email too here is the function of prepare collection
protected function _prepareCollection()
{
$store = $this->_getStore();
$collection = Mage::getModel('ds_productfaq/productfaq')
->getCollection()
->joinProducts()
->setOrder('answered_on', 'ASC')
->setOrder('created_on', 'ASC')
;
if ($store->getId()) {
$collection->addFieldToFilter('main_table.store_id', $store->getId());
}
$this->setCollection($collection);
$columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
// sort by product_name
if ($columnId == 'product_name' ) {
$this->_preparePage();
$columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
$dir = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) {
$dir = (strtolower($dir)=='desc') ? 'desc' : 'asc';
$this->_columns[$columnId]->setDir($dir);
$collection->getSelect()->order('IFNULL(_table_product_name.value,_table_product_name2.value) '.$dir);
}
}
parent::_prepareCollection();
return $this;
}
if I add this line to the collection:
->addAttributeToSelect('sku') I'll have this error:
Fatal error: Call to undefined method DS_ProductFAQ_Model_Mysql4_Productfaq_Collection::addAttributeToSelect() in /var/www/app/code/local/DS/ProductFAQ/Block/Adminhtml/Productfaq/Grid.php on line 29
here is the joinProducts function, maybe the change is needed here:
public function joinProducts()
{
$resource = Mage::getSingleton('core/resource');
$product_table = $resource->getTableName('catalog/product');
$productResource = Mage::getResourceSingleton('catalog/product');
$nameAttr = $productResource->getAttribute('name');
$nameAttrId = $nameAttr->getAttributeId();
$nameAttrTable = $nameAttr->getBackend()->getTable();
$this->getSelect()->joinLeft(
array('_table_product_name' => $nameAttrTable),
'_table_product_name.entity_id=main_table.product_id
AND (_table_product_name.store_id = main_table.store_id)
AND _table_product_name.attribute_id = '.(int)$nameAttrId,
array('product_store_id'=>'_table_product_name.store_id')
)->joinLeft(
array('_table_product_name2' => $nameAttrTable),
'_table_product_name2.entity_id = main_table.product_id
AND (_table_product_name2.store_id = 0)
AND _table_product_name2.attribute_id = '.(int)$nameAttrId,
array('')
)
->from("",array(
'product_name' => new Zend_Db_Expr('IFNULL(_table_product_name.value,_table_product_name2.value)')
)
);
return $this;
}
I did this and now the sku is not empty anymore! and full with name properties because the join tables are for product name not sku but I don't know how to join for sku
here is what I did!
'sku' => new Zend_Db_Expr('IFNULL(_table_product_name.value,_table_product_name2.value)')