2

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.

  • Can you show us the relevant parts of the upgrade script? – user3154108 Oct 13 '14 at 8:34
2

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()

  • I was thinking of this approach but I thought there was an easier and better way, that keeps me from changing a core table format. I've did as you said but now it's not saving as it should. This is the attribute used by submit: name="<?php echo $this->getFieldSuffix() ?>[stock_data][qty]"... but If I change qty to my column name which is warehouse_stock, it isn't saving. If I let qty it will save the data in the Qty field – Magarusu Oct 13 '14 at 10:40
  • Did you clear the cache after adding the table column? Even if your cache is off, ZF still caches the table schema and does not recognize your new column. – Marius Oct 13 '14 at 10:48
  • @Magarusu. Before I go jump buildings in one single leap...who was your previous super hero? :D – Marius Oct 13 '14 at 11:46
  • 3
    Batman, but you are way more powerful, you can jump from <block> to </block> :) – Magarusu Oct 13 '14 at 12:03

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.