Solving the Magento Price Attribute Saving Issue
You’ve encountered an issue where adding price attributes programmatically in Magento fails to save decimal values correctly. The problem lies in the default data type set for the attribute when it’s added through code. Let me guide you through fixing this and ensuring accurate savings of prices with decimals.
Understanding the Problem
When you add a new price attribute using the existing setup script, Magento sets the input type to ‘price’ by default. However, this isn’t sufficient for saving decimal values when data is entered in the backend. To resolve this, we need to adjust the data type used for the attribute.
Modifying the Attribute Configuration
To allow for decimal prices, you should change the type from ‘int’ to ‘decimal’. Here’s how your modified setup script should look:
$installer->startSetup();
// NJ add product attributes
$attributeId = $installer->getAttributeId('catalog_product','my_filialpreis');
if($attributeId){
$installer->removeAttribute('catalog_product',$attributeId);
}
$installer->addAttribute('catalog_product', 'my_filialpreis', array(
'input' => 'price',
'type' => 'decimal', // Change this line
'user_defined' => true,
'label' => 'Filialpreis',
'scope' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => false,
'is_required' => '0',
'is_comparable' => '0',
'is_searchable' => '0',
'is_unique' => '0',
'is_configurable' => '0',
'unique' => false,
'used_in_product_listing' => false,
'required' => false,
'searchable' => false,
'comparable' => false,
'visible_on_front' => false,
)
);
$installer->updateAttribute('catalog_product', 'my_filialpreis', 'apply_to', 'configurable,simple,grouped,bundle,virtual,downloadable');
// NJ add attribute Id to Attribute Set
$installer->addAttributeToSet('catalog_product','Default','Price','my_filialpreis');
// NJ add attribute Id to Attribute Group (the tab in the backen Produktpage)
$installer->addAttributeToGroup('catalog_product','Default','Prices','my_filialpreis');
$installer->endSetup();
Why This Works
Changing the type to ‘decimal’ tells Magento to treat this attribute as a floating-point number, allowing it to handle decimal values. When you enter a price like 99.99 in the backend after making this change, Magento will save it correctly as 99.99 instead of truncating it to 99.
Conclusion
Changing the data type of your programmatically added price attribute from ‘int’ to ‘decimal’ is the key to resolving the issue with saving decimal values. This simple modification ensures that prices entered in the backend are saved accurately, even when attributes are added through code.
Remember, this change will only affect new installations or installations where you haven’t yet added the my_filialpreis attribute. If you’ve already added it and need to update existing data, you’ll need to use Magento’s built-in functionality for updating product prices.
As always, don’t hesitate to reach out if you encounter additional issues or have questions about other technical aspects of your Magento setup! The team at LaravelCompany is ready to help with any development needs.