To achieve the functionality of calculating order tax based on the store origin address when the “Store Pickup” shipping method is selected in Magento 2, you can follow these steps:
1. Configure Store Pickup Shipping Method
Magento 2 doesn’t have a built-in “Store Pickup” method by default, so you’ll need to:
- Install a third-party extension for Store Pickup (or build a custom one).
- If using a custom module, ensure the method is set up as a shipping method in your system.
2. Define Tax Calculation Settings
Magento allows you to configure tax calculation based on the shipping origin:
- Go to Stores > Configuration > Sales > Tax.
- Set the following:
- Tax Calculation Based On: Set this to “Shipping Origin.”
- Default Tax Destination Calculation: Ensure this is configured for fallback values.
3. Add Store Origin Address for Store Pickup
You need to configure the store’s origin address:
- Navigate to Stores > Configuration > Shipping Settings > Origin.
- Set the Country, Region, and Postal Code for the store.
If you have multiple store locations, a Store Pickup extension may allow configuring different origins for each store.
4. Modify Tax Calculation for Store Pickup
To automatically use the store’s origin address for tax calculation when Store Pickup is selected, you’ll need custom logic. Follow these steps:
a. Create/Override a Plugin
You need to override the Magento\Tax\Model\Calculation or similar relevant classes using a plugin or observer.
For instance:
namespace Vendor\Module\Plugin;
use Magento\Quote\Model\Quote\Address;
class TaxCalculation
{
public function beforeCollect(\Magento\Quote\Model\Quote $quote)
{
$shippingMethod = $quote->getShippingAddress()->getShippingMethod();
// Check if Store Pickup method is selected
if ($shippingMethod === 'storepickup') {
// Override shipping address with store origin address
$storeOrigin = [
'country_id' => 'US', // Set your store's origin country
'region_id' => '12', // Set your store's origin region
'postcode' => '10001', // Set your store's origin postcode
];
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->addData($storeOrigin);
$shippingAddress->setCollectShippingRates(false); // Prevent re-calculating rates
}
}
}
b. Dispatch an Event Observer
You can use Magento’s event system to listen for changes in the shipping method during checkout:
- Event:
sales_quote_address_collect_totals_before - Observer Class:
namespace Vendor\Module\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Event\Observer; class ChangeTaxForStorePickup implements ObserverInterface { public function execute(Observer $observer) { $address = $observer->getEvent()->getQuoteAddress(); $shippingMethod = $address->getShippingMethod(); if ($shippingMethod === 'storepickup') { // Update tax calculation address to store origin $address->setCountryId('US'); $address->setRegionId('12'); $address->setPostcode('10001'); $address->setCollectShippingRates(false); } } }5. Test the Changes
- Go to the checkout page and select the Store Pickup shipping method.
- Verify that the tax is calculated based on the store’s origin address.
6. Optional Enhancements
- Multiple Store Origins: If you have multiple pickup locations, modify the logic to select the correct origin address dynamically based on the chosen store location.
- Frontend Notifications: Display a message to the user explaining that tax is calculated based on the store’s address.
Summary
By using custom plugins or observers, you can dynamically switch the tax calculation address based on the selected shipping method (Store Pickup). Additionally, ensure your Store Pickup method is properly integrated with Magento’s tax calculation logic. Let me know if you’d like a detailed module implementation for this!
