Site icon EcomPlugins Blog

Magento 2 Store pickup shipping method selection then calculate order tax based on store origin address automatically on checkout page

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:


2. Define Tax Calculation Settings

Magento allows you to configure tax calculation based on the shipping origin:

  1. Go to Stores > Configuration > Sales > Tax.
  2. 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:

  1. Navigate to Stores > Configuration > Shipping Settings > Origin.
  2. 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:

Exit mobile version