Magento Add Custom Attribute Programmatically in Specific Fieldset

magento-1.8

I've created a custom attribute through an upgrade script and I saw that I can only add it to specific groups. If I for example try to add it to 'Inventory' it will create a new fieldset and not add it to the main inventory fieldset.

I've discovered the file where I need to put the code to load the custom var: /app/design/adminhtml/default//template/catalog/product/tab/inventory.phtml but I don't know how to load the custom var and make it savable at submit.

My upgrade script (this part works):

$installer->addAttribute(
Mage_Catalog_Model_Product::ENTITY,
'warehouseStock',
array(
    'label'                      => Mage::helper('My_HELPER')->__('Warehouse Stock'),
    'group'                      => 'General',
    'type'                       => 'text',
    'input'                      => 'text',
    'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'user_defined'               => true,
    'required'                   => false,
    'visible'                    => true,
    'source'                     => 'eav/entity_attribute_source_table',
    'backend'                    => null,
    'searchable'                 => false,
    'visible_in_advanced_search' => false,
    'visible_on_front'           => false,
    'is_configurable'            => false,
    'is_html_allowed_on_front'   => false,
    'sort_order'                 => '50',
)

This is what I did in /app/design/adminhtml/default//template/catalog/product/tab/inventory.phtml

<tr>
        <?php
            $_item = $this->getProduct()->getId();
            $_resource = $this->getProduct()->getResource();
            $optionValue = $_resource->getAttributeRawValue($_item, 'warehouseStock', Mage::app()->getStore());
        ?>
        <td class="label"><label for="inventory_warehouse_stock"><?php echo Mage::helper('catalog')->__('Warehouse Availability');?></label></td>
        <td class="value">
            <?php if (!$_readonly):?>
                <input type="hidden" id="original_warehouse_qty" name="<?php echo $this->getFieldSuffix() ?>[stock_data][original_warehouse_qty]" value="<?php echo $optionValue*1 ?>"/>
            <?php endif;?>
            <input type="text" class="input-text required-entry validate-number" id="warehouse_qty" name="<?php echo $this->getFieldSuffix() ?>[stock_data][qty]" value="<?php echo $optionValue*1 ?>" <?php echo $_readonly;?>/>
        </td>
        <td class="value scope-label"><?php echo Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
    </tr>

but of course it's not savable, it only shows the current value that has been set through the custom attribute in general field.

Best Answer

You can declare your attribute at creation not to be visible.

'visible' => false,

This means that you won't be able to edit it later from Catalog->Attributes->Manage attributes but also no input will be created automatically for it.
But the attribute will be automatically picked up on save or load.
So you have to manually add it to one of the templates like the one you mentioned in the question (inventory.phtml).

But I think your case is a bit different.
If you need a new field related to the stock functionality, you shouldn't add it as a product attribute. You should add it as a column in the stock table (cataloginventory_stock_item) and you will have that value available from $product->getStockItem()

Related Topic