1

I have a 3rd party extension which provides a grid. Now I want to extend the grid with a simple Magento attribute. It looks so easy but I can't get it to work! I modified the function _prepareCollection() and wanted to add an attribute to the selection. Unfortunately all attempts failed.

That is my most promising solution (added before "setCollection") but it doesn't work either:

    $collection->getSelect()->join( array( '_atr'=>'eav_attribute'), "_atr.attribute_code = 'second_sku'", array());
    // $collection->addAttributeToSelect('second_sku');

Thats the (original) full function:

    protected function _prepareCollection() {
    $orderId = $this->getOrder()->getid();

    $collection = mage::getResourceModel('Purchase/OrderProduct_collection')
                    ->addFieldToFilter('pop_order_num', $orderId);

    //only products not supplied
    $collection->getSelect()->where('(pop_qty - pop_supplied_qty)  >0');

    //add image picture
    $smallImageTableName = mage::getModel('Purchase/Constant')->getTablePrefix() . 'catalog_product_entity_varchar';
    $collection->getSelect()->joinLeft($smallImageTableName,
            'pop_product_id=`' . $smallImageTableName . '`.entity_id and `' . $smallImageTableName . '`.store_id = 0 and `' . $smallImageTableName . '`.attribute_id = ' . mage::getModel('Purchase/Constant')->GetProductSmallImageAttributeId(),
            array('small_image' => 'value'));

    //join with product
    $productTableName = mage::getModel('Purchase/Constant')->getTablePrefix() . 'catalog_product_entity';
    $collection->getSelect()->joinLeft($productTableName,
            'pop_product_id=`' . $productTableName . '`.entity_id',
            array('sku' => 'sku'));

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

Any help will be much appreciated! Thank you!

EDIT: If I add my code to the original function I'll get following select:

SELECT `main_table`.*, `catalog_product_entity_varchar`.`value` AS `small_image`, `catalog_product_entity`.`sku` FROM `purchase_order_product` AS `main_table` LEFT JOIN `catalog_product_entity_varchar` ON pop_product_id=`catalog_product_entity_varchar`.entity_id and `catalog_product_entity_varchar`.store_id = 0 and `catalog_product_entity_varchar`.attribute_id = 109 LEFT JOIN `catalog_product_entity` ON pop_product_id=`catalog_product_entity`.entity_id INNER JOIN `eav_attribute` AS `_atr` ON _atr.attribute_code = 'second_sku' WHERE (pop_order_num = '1') AND ((pop_qty - pop_supplied_qty) >0);

As you see the select "works" but the attribute is not part of the selection. I already tried addAttributeToSelect and addFieldToSelect but both throw an error.

4
  • 1
    Have you debugged the full query produced to see that it actually works? Try doing something like Mage::log($collection->getSelectSql(true), null, 'query.log', true); to see the SQL produced for this collection. Jan 6, 2015 at 10:47
  • Thanks for your help. I edit my previous post and added the select.
    – Stefan
    Jan 6, 2015 at 10:53
  • Can't quite remember the syntax, but can you do something like the following? $collection->getSelect()->columns(array('second_sku'=>'_atr.attribute_value')); Jan 6, 2015 at 11:14
  • That doesn't work. But I finally got it done by myself. Thank you so much for your help!
    – Stefan
    Jan 6, 2015 at 13:51

2 Answers 2

4

I finally got it done!

I added this in my _prepareCollection() function

        //ADD THE ATTRIBUTE 
    $this->_addSecondSku($collection);

Then I added a new function:

    protected function _addSecondSku($collection)
{
    $entityProduct = Mage::getSingleton("core/resource")->getTableName('catalog_product_entity');
    $entityValuesVarchar = Mage::getSingleton("core/resource")->getTableName('catalog_product_entity_varchar');
    $entityAtribute = Mage::getSingleton("core/resource")->getTableName('eav_attribute');

    $collection->getSelect()
        ->joinLeft( array( '_manAttr'=>$entityAtribute ), "_manAttr.attribute_code = 'second_sku'", array() )
        ->joinLeft( array( '_manValVarchar'=>$entityValuesVarchar ), "_manValVarchar.attribute_id = _manAttr.attribute_id AND _manValVarchar.entity_id = {$entityProduct}.entity_id", array('second_sku'=>'value'))
    ;
}
0
1
 protected function _prepareCollection()
    { 

        $collection = Mage::getResourceModel($this->_getCollectionClass());

        $collection->getSelect()->join('sales_flat_order', 'main_table.increment_id = sales_flat_order.increment_id',array('fee_amount'));

        $collection->join('invoice', 'main_table.entity_id = invoice.entity_id', 'subtotal');

        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

Your Answer

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

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