4

I have created a custom column for associated products (basically every simple product will have that column under "associated products" tab of configurable product). I store information of this field/column into an attribute which belong to configurable product instead of each simple product.

Below is the code for adding a column;

config.xml

<core_block_abstract_to_html_before>
     <observers>
        <something>
          <class>namespace_JsonProductInfo_Model_Observer</class>
          <method>addPositionColumn</method>
        </something>
     </observers>
</core_block_abstract_to_html_before>

Oberver.php

public function addPositionColumn(Varien_Event_Observer $observer)
{
    $block = $observer->getEvent()->getBlock();
    if ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Super_Config_Grid) {
        if (!$block->isReadonly()) {
            $block->addColumnAfter(
                'position_simple',
                array(
                    'header'     => Mage::helper('jsonproductinfo')->__('Position'),
                    'type'      => 'input',
                    'name'      => 'position_simple',
                    'index'     => 'position_simple',
                    'sortable'  => false,
                ),
                'name'
            );
        }
    }
}

With above code I am able to add the column into a require grid. And I am also able to save the information which I put inside that column (which is basically an attribute). The only problem I am facing is, I am unable to display the stored information into that column again (If I edit the product). I have also created another observer of type eav_collection_abstract_load_befor but no luck so far. Below is the code for it:

config.xml

<eav_collection_abstract_load_before>
    <observers>
        <jsonproductinfo>
            <class>namespace_JsonProductInfo_Model_Observer</class>
            <method>addPositionToCatalogProductCollection</method>
        </jsonproductinfo>
    </observers>
</eav_collection_abstract_load_before>

Observer.php

public function addPositionToCatalogProductCollection($observer)
{
    $collection = $observer->getEvent()->getCollection();
    if (!isset($collection)) {
        return;
    }
    $collection->addAttributeToSelect('position_simple');
}

I would be grateful if someone can point to the mistake or guide me how can I display back that information again into that column.


EDIT

And here is the code which creates the "postion_simple".

$this->addAttribute(
    'catalog_product',
    'position_simple',
    array(
        'group'             => 'General',
        'type'              => 'varchar',
        'input'             => 'hidden',
        'backend'           => '',
        'frontend'          => '',
        'label'             => 'Simple Position',
        'class'             => '',
        'source'            => '',
        'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'           => true,
        'required'          => false,
        'user_defined'      => true,
        'default'           => '',
        'searchable'        => false,
        'filterable'        => false,
        'comparable'        => false,
        'visible_on_front'   => false,
        'visible_in_advanced_search'   => false,
        'unique'            => false,
        'apply_to'          => Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE,
        'is_configurable'   => false,
    )
);
  • have you checked that your observer is working ? check by printing query in your observer.echo $collection->getSelect(); – Minesh Patel Dec 7 '15 at 5:17
  • @MineshPatel, Yes, the observer is working. I was already sure about it but still checked it by printing the query. – Capri82 Dec 7 '15 at 12:44
0
  1. change 'index' => 'position_simple' to 'index' => 'entity_id' in your Observer.php (function addPositionColumn)
  2. you need to get value of your position_simple attribute from configurable product and use it in added column - one way to do that:

create function in Observer.php

protected function _getDefaultConfigurationId() {
    /** @var Mage_Catalog_Model_Product $product */
    $product = Mage::registry('current_product');
    if ($product) {
        return array($product->getData('position_simple'));
    }
    return '';
}

and use that in your addPositionColumn function like that:

...    
'values' => $this->_getDefaultConfigurationId(),
...

After that, there is no need of observer type eav_collection_abstract_load_befor at all.

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.