Configuring Special Shipping Prices for Magento Products
In this post, we’ll address a common requirement in Magento where you need to apply special fixed shipping prices for specific products when they are shipped to certain countries. To achieve this, we’ll create a custom module that adds a new shipping method called “flat rate per product”.
Setting Up the Custom Shipping Method
Firstly, let’s walk through how to set up the flat rate per product shipping method:
-
Create a new module using Magento’s standard module structure under
app/code/local. Name your module something likeLaravelFlatRatePerProduct. -
Define the shipping method model. In your module, create a file named
Model/Flatrate.phpand define the class extending\Enterprise_Shipping_Model_Carrier_Abstract:
class Laravel_Flatrate extends Mage_Shipping_Model_Carrier_Abstract
{
protected $_code = 'flatrate';
}
- Configure the shipping method. In
config.xml, add the following configuration:
<global>
<models>
<laravel_flatrate>
<class>Laravel_Flatrate_Model</class>
</laravel_flatrate>
</models>
</global>
<default>
<carriers>
<flatrate>
<active>1</active>
<shipping_model>laravel_flatrate/flatrate</shipping_model>
<!-- Additional configuration properties -->
</flatrate>
</carriers>
</default>
- Create the admin form field. In
Form/Flatrate.php, define a new text box for special shipping price:
class Laravel_Flatrate_Form_Flatrate extends Mage_Shipping_Model_Config_Data
{
public function getHtml()
{
$html = parent::getHtml();
$field = new Varien_Data_Form_Element_Text(
'special_price',
array(
'label' => Mage::helper('laravel_flatrate')->__('Special Price per Product'),
'title' => Mage::helper('laravel_flatrate')->__('Special Price per Product')
)
);
$html .= $field->getHtml();
return $html;
}
}
Checking Availability of the Flat Rate Method
The primary question from our reader centers around checking for the availability of this custom shipping method and its price value for a specific product before rendering the available shipping methods in availble.phtml.
To achieve this, you’ll need to follow these steps:
-
Include the necessary models at the beginning of your template file.
-
Create a new block extending
\Mage_Core_Block_Templateand override the_beforeToHtml()method:
class Laravel_Flatrate_Block_Available extends Mage_Core_Block_Template
{
protected function _beforeToHtml()
{
$flatRateModel = Mage::getSingleton('laravel_flatrate/flatrate');
// Check if the flat rate model is active and has a special price for the current product
// Implement your logic here
return parent::_beforeToHtml();
}
}
- Update the template file to use this new block:
<block type="laravel_flatrate/available" name="available_shipping_methods" as="shipping_methods">
<!-- Your existing shipping methods code -->
</block>
Conclusion
In this post, we’ve walked through how to create a custom flat rate per product shipping method in Magento and how to check for its availability before rendering the available shipping methods.
Remember, the key is to create a new module following Magento’s standard structure, define your shipping model, configure it properly, and then extend existing blocks and templates as needed. This approach ensures you can implement complex functionality like special pricing for specific products while maintaining Magento’s core integrity.
For more in-depth guides on creating custom modules and extending core functionality, I recommend exploring the Magento Developer Documentation and our own tutorials at Laravel Company.
Happy coding!