Magento: Calculating Duties/Taxes During Checkout
As an experienced e-commerce developer, I understand the complexity of international shipping and the need to integrate accurate duty and tax calculations into your checkout process. In this post, we’ll explore how to add duties/taxes to a quote during review in Magento.
Understanding Magento’s Quote Structure
Magento uses the quote object to store information about an order while itâs being built or reviewed by a customer. The $quote->addCostComponent() method you mentioned is indeed available, but it doesn’t directly support adding duties/taxes.
$quote->addCostComponent('Duties', 5.0);
This method allows you to add additional costs (like shipping fees) to the quote, but it doesn’t handle taxes or duties specifically.
Using Magento’s Tax Calculation API
Magento provides a robust tax calculation system that can be extended to include third-party APIs like those used for international duty calculations. To achieve this, we’ll create a custom tax model and use Magento’s event observers.
Step 1: Create a Custom Tax Model
Create a new class in your custom module:
namespace LaravelCompany\Duties\Model;
class DutiesTaxModel extends \Magento\Tax\Model\Sales\Order\TaxManagement
{
public function calculate($items, $address)
{
// Implement your logic to call the 3rd party API and fetch duties/taxes.
// Return an array of tax items that can be added to the quote.
return [];
}
}
Step 2: Create a Tax Collector Model
Create another class:
namespace LaravelCompany\Duties\Model;
class DutiesTaxCollector extends \Magento\Tax\Model\Sales\Order\TaxManagement
{
public function fetch($taxModel, $items, $address)
{
// Implement logic to use the custom tax model and add duties/taxes to the quote.
// Use the $quote->addTax() method or adjust the total taxes accordingly.
return [];
}
}
Step 3: Set Up Event Observers
Create an event observer in your module’s etc/events.xml file:
<event name="sales_model_service_quote_submit_before">
<observer name="laravelcompany_duties_calculate_taxes" instance="LaravelCompany\Duties\Observer\CalculateTaxesObserver"/>
</event>
In the observer class, you can call your custom models:
namespace LaravelCompany\Duties\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\Http;
class CalculateTaxesObserver implements ObserverInterface
{
protected $dutiesTaxModel;
protected $dutiesTaxCollector;
// Inject the necessary dependencies
public function execute(\Magento\Framework\Event\Observer $observer)
{
if ($this->isInternationalShipping($observer)) {
$items = $observer->getQuote()->getAllItems();
$address = $observer->getQuote()->getShippingAddress();
$taxes = $this->dutiesTaxModel->calculate($items, $address);
// Add the calculated duties/taxes to the quote
}
}
private function isInternationalShipping($observer)
{
// Implement logic to check if the shipping destination is international.
// Return true or false based on your criteria.
}
}
Conclusion
In this post, we explored how to extend Magento’s tax calculation system to include duties/taxes from a third-party API during the checkout review process. By creating custom models and using event observers, you can seamlessly integrate international duty calculations into your e-commerce store.
At Laravel Company, we specialize in helping businesses like yours unlock the full potential of Magento for complex e-commerce challenges. Contact us today to learn more about our services and how we can assist you with your custom development needs.