Rohit Goel
Rohit Goel

Reputation: 3554

How to add new columns in group price section in magento

I need to add 2 new columns in magento admin in the group price section.

I have checked the template file.

app/design/adminhtml/default/default/template/catalog/product/edit/price/group.phtml

I have added new columns there, but..

how can i add these two columns in database and save values.

Any suggestions?

Thanks.

Upvotes: 2

Views: 1762

Answers (2)

Gwidryj
Gwidryj

Reputation: 1035

I've had the same issue. Answer by Samuel Coman was great help for me. To be more precise, you have to modify three files. I have added integer column 'popularity' (to be used by xml-rpc methods), but rules are the same. Here is my example.

The first file

magento/app/design/adminhtml/default/default/template/catalog/product/edit/price/group.phtml

in table around line 50 add new <th>:

<th><?php echo $this->getPriceColumnHeader(Mage::helper('catalog')->__('Popularity')); ?></th>

in var groupPriceRowTemplate around line 67 add new <td>:

+ '<td><input class="<?php echo $_htmlClass; ?>" type="text" name="<?php echo $_htmlName; ?>[{{index}}][popularity]" value="{{popularity}}" id="group_price_row_{{index}}_popularity"/></td>'

in var groupPriceControl line 95 modify default arguments etc.:

    var data = {
        website_id: '<?php echo $this->getDefaultWebsite(); ?>',
        group: '<?php echo $this->getDefaultCustomerGroup(); ?>',
        price: '',
        popularity: '',
        readOnly: false,
        index: this.itemsCount++
    };

    if(arguments.length >= 3) {
        data.website_id = arguments[0];
        data.group = arguments[1];
        data.price = arguments[2];
        data.popularity = arguments[3];
    }
    if (arguments.length == 5) {
        data.readOnly = arguments[4];
    }

add your column in addItem call, line 165:

groupPriceControl.addItem('<?php echo $_item['website_id']; ?>', '<?php echo $_item['cust_group']; ?>', '<?php echo sprintf('%.2f', $_item['price']); ?>', '<?php echo sprintf('%.0f', $_item['popularity']); ?>', <?php echo (int)!empty($_item['readonly']); ?>);

The second file
magento/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Groupprice/Abstract.php

here you have to modify afterSave method, firstly add your column to the array $new, line 316:

        $new[$key] = array_merge(array(
            'website_id'        => $data['website_id'],
            'all_groups'        => $useForAllGroups ? 1 : 0,
            'customer_group_id' => $customerGroupId,
            'value'             => $data['price'],
            'popularity'        => $data['popularity'],
        ), $this->_getAdditionalUniqueFields($data));

then you have to handle a case when you only change your new column and save it, so on line 349, after if (!empty($update)) statement, goes:

    if (!empty($update)) {
        foreach ($update as $k => $v) {
            if ($old[$k]['price'] != $v['value'] || $old[$k]['popularity'] != $v['popularity']) {
                $price = new Varien_Object(array(
                    'value_id'  => $old[$k]['price_id'],
                    'value'     => $v['value'],
                    'popularity'=> $v['popularity'],
                ));
                $this->_getResource()->savePriceData($price);

                $isChanged = true;
            }
        }
    }

The third file

magento/app/code/core/Mage/Catalog/Model/Resource/Product/Attribute/Backend/Groupprice/Abstract.php  

here modify loadPriceData method to properly show values from database; you have to just add your column to $colums variable, line 49:

    $columns = array(
        'price_id'      => $this->getIdFieldName(),
        'website_id'    => 'website_id',
        'all_groups'    => 'all_groups',
        'cust_group'    => 'customer_group_id',
        'price'         => 'value',
        'popularity'    => 'popularity',
    );

There is no need to change savePriceData method, as _prepareDataForTable inside it will find your new columns in database.

Than you have to add your columns to group prices table and also to tier price table, because it uses the same save methods, so run in mysql on your database commands similar to the following:

alter table catalog_product_entity_group_price add popularity integer;
alter table catalog_product_entity_tier_price add popularity integer;

And last, but not least remember to refresh the cache - without doing this system won't find new columns.

Hope it will help someone.

Upvotes: 4

Samuel Coman
Samuel Coman

Reputation: 265

To add the column create a new module and inside the installer alter the table catalog_product_entity_group_price and add your desired columns.

To save & display the data...well, I hope you are pretty advanced of Magento overriding.

The file you mentioned takes some data from Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Price_Group_Abstract but the real data is taken using the loadPriceData from Mage_Catalog_Model_Resource_Product_Attribute_Backend_Groupprice_Abstract

The prices are saved in method afterSave from Mage_Catalog_Model_Product_Attribute_Backend_Groupprice_Abstract which calls savePriceData from Mage_Catalog_Model_Resource_Product_Attribute_Backend_Groupprice_Abstract

You could try to override those methods but you might break some Magento stuff. Those prices are tied with the Indexers also. Another option would be to create a separate module, to add another observer on product save, save your data into a separate table and get it back using an extension of Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Price_Group_Abstract

Upvotes: 2

Related Questions