0

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)')
1

This module is somehow crappy, because it doesn't use the magento way of creating collections, therefore it is hard to add more fields, tables and attributes.

What you can try is to join catalog_product_entity where the sku is in. (Mage_Core_Model_Resource_Db_Collection_Abstract::join)

$collection->join(
     array('prod' => $collection->getTable('catalog/product_entity')), 
     'prod.entity_id=main_table.product_id',
     'sku'
);
  • Thank you so much! would you please tell me where I should insert your code? I have a join function in the abstract.php of the path you mentioned – Nickool May 20 '14 at 17:25
  • The best would be to insert into into the collection_load_before event of the collection, because touching third party codes means, you are not longer easy updateable. But if this doesnt matter you can just throw it into _prepareCollection – Fabian Blechschmidt May 20 '14 at 17:41
  • you mean which file? abstract.php? I don't have such thing collection_load_before I just have beforeload and when I inserted it gave me error and I inserted in my function preparecollection in grid.php too and i had error – Nickool May 20 '14 at 17:48
  • you didn't named your files in your post, I mean this: protected function _prepareCollection() { $store = $this->_getStore(); $collection = Mage::getModel('ds_productfaq/productfaq') ->getCollection() ->joinProducts() ->setOrder('answered_on', 'ASC') ->setOrder('created_on', 'ASC') ; – Fabian Blechschmidt May 20 '14 at 17:49
  • yea it is Grid.php as I told you it gave me error I inserted after this – Nickool May 20 '14 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.