1

here is my prepareCollection from this how to get product name and price and status

protected function _prepareCollection()
{
    $store = $this->_getStore(); 
    $collection = Mage::getModel('catalog/product_link')    
                       ->getCollection() 
                       ->addFieldToFilter('product_id',$this->_getProductId())
                       ->addFieldToFilter('link_type_id','1');
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
3
  • For what purpose do you need 'product name and price and status'? To display in grid? Commented Feb 17, 2014 at 9:44
  • @oleksii.svarychevskyi i have a grid in admin page. in the grid i am showing product_id , related_product_id and i want to show the product name and and price status. show that user can understand which product have related products.
    – Mani Dev
    Commented Feb 17, 2014 at 9:51
  • @oleksii.svarychevskyi help me man.
    – Mani Dev
    Commented Feb 17, 2014 at 9:59

1 Answer 1

1

Go to your product link collection class and add a method

public function addProductData($attributesCodes)
{
    foreach ($attributesCodes as $attributeCode) {
        $attributeTableAlias = $attributeCode . '_table';
        $attribute = Mage::getSingleton('eav/config')
            ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);

        $this->getSelect()->join(
           array($attributeTableAlias => $attribute->getBackendTable()),
             "main_table.product_id = {$attributeTableAlias}.entity_id AND {$attributeTableAlias}.attribute_id={$attribute->getId()}",
        array($attributeCode => 'value')
    );
        $this->_map['fields'][$attributeCode] = 'value';
    }
    return $this;
}

Then in your block's _prepareCollection() you can call this method

protected function _prepareCollection()
{
    $store = $this->_getStore(); 
    $collection = Mage::getModel('catalog/product_link')    
                   ->getCollection() 
                   ->addFieldToFilter('product_id',$this->_getProductId())
                   ->addFieldToFilter('link_type_id','1');
    $collection->addProductData(array('name', 'price', 'status'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

Now you can add 'name', 'price', 'status' columns in _prepareColumns().

1
  • i am getting this below error Fatal error: Call to a member function addProductData() on a non-object in C:\xampp\htdocs\magento\app\code\local\Mk\Myproducts\Block\Adminhtml\Myproduct\Grid.php on line 59
    – Mani Dev
    Commented Feb 18, 2014 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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