Changing Shipping Price in Magento Checkout
In this post, we’ll explore how to change the final shipping price during checkout in Magento using a custom checkbox added via design. This scenario involves manipulating the shipping cost based on specific conditions.
Understanding the Scenario
We have three shipping methods with varying costs:
- Method 1: $33
- Method 2: $23
- Method 3: $10 (plus an additional $10 for special packaging)
The goal is to increment the final price of Method 3 based on a checkbox value added manually in the design. The total shipment cost should be $20.
Step-by-Step Solution
1. Create a Custom Checkout Block
First, let’s create a new checkout block file:
/app/code/Vendor/Module/view/frontend/templates/checkout/cart/item/default.phtml
Inside this file, add your custom checkbox and label:
<div class="checkout-item-special-packaging">
<input type="checkbox" name="special_packaging" id="special_packaging" value="1"
<?php if ($block->getQuote()->getSpecialPackaging()): ?>checked="checked"<?php endif; ?>>
<label for="special_packaging">Add Special Packaging (+$10)</label>
</div>
2. Add Custom Checkout Price Calculation
To manipulate the shipping price, we need to override Magento’s checkout cart price calculation.
Create a plugin configuration file:
/app/code/Vendor/Module/etc/di.xml
Add the following XML content:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\Quote\Item">
<plugin name="vendor_module_change_shipping_price" type="Vendor\Module\Plugin\CheckoutCartPrice"/>
</type>
</config>
3. Create the Checkout Cart Price Plugin
Create the Plugin class:
/app/code/Vendor/Module/Plugin/CheckoutCartPrice.php
Implement the plugin logic to update the shipping price based on the checkbox value:
namespace Vendor\Module\Plugin;
use Magento\Framework\App\Request\Http;
use Magento\Quote\Model\Quote\Item;
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
class CheckoutCartPrice
{
protected $specialPackaging = 10; // Set the additional cost for special packaging
public function afterCollectTotals(
AbstractTotal $subject,
Item $item,
array $totalAmounts
) {
if ($this->getRequest()->getParam('special_packaging')) {
$shippingPrice = $item->getProduct()->getShippingPrice();
$specialPackagingCost = $this->specialPackaging;
$newTotal = $shippingPrice + $specialPackagingCost;
$subject->setTotalAmount('shipping_tax_amount', 0);
$subject->setTotalAmount('shipping_including_tax', $newTotal);
$subject->setTotalAmount('shipping_amount', $newTotal);
return $totalAmounts;
}
return parent::afterCollectTotals($subject, $item, $totalAmounts);
}
}
4. Update the Quote Model
To persist the special packaging value across pages and sessions, update your quote model:
/app/code/Vendor/Module/etc/di.xml
Add this configuration:
<type name="Magento\Quote\Model\Quote">
<plugin name="vendor_module_update_quote_special_packaging" type="Vendor\Module\Plugin\UpdateQuoteSpecialPackaging"/>
</type>
Implement the plugin logic in /app/code/Vendor/Module/Plugin/UpdateQuoteSpecialPackaging.php:
namespace Vendor\Module\Plugin;
use Magento\Framework\App\Request\Http;
use Magento\Quote\Model\Quote;
class UpdateQuoteSpecialPackaging
{
protected $request;
public function __construct(Http $request)
{
$this->request = $request;
}
public function afterSetOrigData(Quote $subject, array $data)
{
if ($this->request->getParam('special_packaging')) {
$subject->setSpecialPackaging($this->request->getParam('special_packaging'));
}
return parent::afterSetOrigData($subject, $data);
}
}
Conclusion
In this post, we’ve demonstrated how to modify the shipping price during checkout in Magento using a custom checkbox and plugin. We covered creating a custom block, implementing a cart price calculation plugin, and persisting the special packaging value across sessions.
Remember to replace Vendor with your actual vendor name and Module with your module name throughout the code examples.
Key Takeaways:
- Use the Magento plugin architecture to override core behavior.
- Update the quote model to persist custom data across pages and sessions.
- Remember to clear your cache after making changes in development mode.
For more advanced Magento checkout customizations, consider exploring our dedicated resources on LaravelCompany.