Removing Special Price Date Requirements in Magento
Are you facing issues with the ‘From’ & ‘To’ date requirements for special prices in your Magento store? This can be particularly challenging when integrating with a Point of Sales system that doesn’t pass through these dates. In this post, we’ll guide you step-by-step through removing these date restrictions to allow seamless integration.
Understanding the Special Price Mechanism
Magento’s core functionality includes a complex pricing mechanism for handling special prices. The special price is applied only within specific date ranges defined by ‘From’ and ‘To’ fields at the product level in the backend.
The dates are stored in the catalog_product_entity_special_price table with fields special_price (the actual discount amount) and special_from_date & special_to_date. When a special price is applied, Magento checks if the current date falls within this range before applying the discount.
Locating the Relevant Files
You’ve already started by identifying some key files related to special price calculations:
app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.phpapp/code/core/Mage/Catalog/Model/Product/Type/Price.phpapp/code/core/Mage/Catalog/Model/Product.php
These are indeed the core files that handle product collection fetching and price calculations. However, editing these files directly is generally not recommended as it violates Magento’s coding standards and can lead to issues with future updates.
Instead, we should extend these models through a custom module to ensure our changes are preserved during updates.
Creating a Custom Module
-
Create the Module Directory: Navigate to
app/code/local(orcommunity) directory and create your new module folder following the standard Magento naming convention, e.g.,MyCompany_RemoveSpecialPriceDates. -
Configure the config.xml File:
- In
app/code/local/MyCompany/RemoveSpecialPriceDates/etc/config.xml, define your module:
<?xml version="1.0"?> <config> <modules> <MyCompany_RemoveSpecialPriceDates> <active>true</active> <codepool>local</codepool> <depends> <Mage_Catalog /> </depends> </MyCompany_RemoveSpecialPriceDates> </modules> </config> - In
-
Extend the Product Collection:
-
In
app/code/local/MyCompany/RemoveSpecialPriceDates/Model/Product/Collection.php, extend the core collection:class MyCompany_RemoveSpecialPriceDates_Model_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection { public function _afterLoad() { $this->removeFromSelect('special_from_date'); $this->removeFromSelect('special_to_date'); parent::_afterLoad(); } } -
This code removes the ‘special_from_date’ and ‘special_to_date’ columns from the product collection after loading, effectively disabling the date restrictions.
-
Conclusion
By following this approach, you’ve successfully created a custom module that extends the core Magento functionality to remove special price date restrictions without directly modifying core files. This ensures your changes are preserved during updates and maintains Magento’s coding standards.
Remember to clear your cache before testing the changes.
If you need further assistance or want to explore more advanced pricing strategies like tiered pricing, check out our detailed guide on Laravel Company.
Happy coding!