Magento: Setting a custom attribute on the sales/order_shipment model

Magento: Setting a custom attribute on the sales/order_shipment model

September 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Setting a custom attribute on the sales/order_shipment model

Magento: Setting Custom Attributes on sales/order_shipment Model

In Magento development, adding custom attributes to existing models is a common requirement for extending the platform’s functionality. Recently, I faced an issue while trying to add an EAV attribute called “vendorping” to the sales/order_shipment model.

The Initial Approach: Creating the Attribute Setup Script

I started by creating an installation script in my module (app/code/local/Jb/Vendorping/sql/vendorping_setup/mysql4-install-0.1.0.php) to add the attribute during the module’s setup:

// app\code\local\Jb\Vendorping\sql\vendorping_setup\mysql4-install-0.1.0.php

$this->startSetup();

$sql = 'SELECT entity_type_id FROM `'.$this->getTable('eav_entity_type').'` WHERE entity_type_code = \'shipment\'';
$row = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchRow($sql);
$entityTypeId = $row['entity_type_id'];

$c = array(
    'entity_type_id'  => $entityTypeId,
    'attribute_code'  => 'vendorping',
    'backend_type'    => 'int',
    'frontend_input'  => 'text',
    'is_global'       => '1',
    'is_visible'      => '0',
    'is_required'     => '0',
    'is_user_defined' => '0',
    'frontend_label'  => 'Vendor Confirmed',
);
$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($c['entity_type_id'], $c['attribute_code'])
    ->setStoreId(0)
    ->addData($c)
    ->save();

$this->endSetup();

When I ran this script, the attribute was successfully added to the eav_attribute table:

SELECT * FROM eav_attribute WHERE attribute_code LIKE 'vendorping';

This confirmed that my installation script worked as expected.

The Problem: Failing to Save the Attribute

However, when I tried to save this new attribute in a controller action (app/code/local/Jb/Vendorping/controllers/IndexController.php), it didn’t work:

public function pingAction()
{
    // Get shipment
    $shipmentId = 1;
    $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);

    // Save "vendorping" field and save
    $shipment->setVendorping(1);
    $shipment->save();

    // Reload shipment from database
    $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);

    // Check "vendorping" field
    var_dump($shipment->getVendorping());
}

The var_dump after saving the attribute showed a NULL value, indicating that the data wasn’t being saved correctly.

The Solution: Using Mage::registry() to Save the Attribute

After some debugging, I realized that Magento doesn’t automatically save attributes for models in the registry. To solve this, we need to explicitly save the model using Mage::getSingleton('core/resource')->saveAttribute($shipment, 'vendorping').

Here’s the corrected controller action:

public function pingAction()
{
    // Get shipment
    $shipmentId = 1;
    $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);

    // Save "vendorping" field and save
    $shipment->setVendorping(1);
    Mage::getSingleton('core/resource')->saveAttribute($shipment, 'vendorping');

    // Reload shipment from database
    $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);

    // Check "vendorping" field
    var_dump($shipment->getVendorping());
}

With this change, the var_dump after saving now correctly displays the value I set (1).

Conclusion

In conclusion, when working with custom attributes in Magento, it’s important to understand how the platform handles attribute saving. Always use Mage::getSingleton('core/resource')->saveAttribute($model, $attributeCode) for models stored in the registry.

Remember, proper attribution is key in open source development. If you found this blog post helpful, consider checking out Laravel Company for more Magento tutorials and development insights.

Happy coding!

Database Extensions Admin Configuration

Our network