Jump to content

Enforce certain tax rule if user select Store pick up


Recommended Posts

Hello,

I would like to apply tax on product if user select "Store Pickup" as a shipping carrier. Here is my scenario:

State A: Tax  5%

State B: Tax 3%

Rest of the state: 0%.

Now, if someone's delivery address is within "State A" then 5% tax will be applied. Its working fine. Similarly, its working fine for other states too. 

The issue is, if someone's address is within State C, D or E (where tax is 0%) and he/she choose  "Store Pickup" as shipping carrier then I would like to apply tax of 5% because this store is within "State A" (where tax is 5%). Is it possible to enforce 5% tax if user select  "Store Pickup"? Or forcefully change delivery address to Store address if customer select  "Store Pickup"? 

Any help will be appreciated. I am using Prestashop version 1.7 (1.7.8.7)

Thank you!

 

Edited by atiquratik
Prestashop version added (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

You can achieve this by modifying your tax rule and creating a custom shipping carrier module.

Here's a step-by-step guide to create the custom shipping carrier module, but please review and properly test to adapt the code to your need, I haven't tested it:

1. Create a new folder named "storepickup" in the "modules" directory of your PrestaShop installation.

2. Create a new file named "storepickup.php" inside the "storepickup" folder and add the following code:

<?php
if (!defined('_PS_VERSION_')) {
    exit;
}

class StorePickup extends CarrierModule
{
    public function __construct()
    {
        $this->name = 'storepickup';
        $this->tab = 'shipping_logistics';
        $this->version = '1.0.0';
        $this->author = 'Your Name';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Store Pickup');
        $this->description = $this->l('Add a store pickup shipping carrier with custom tax rules.');

        $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
    }

    public function install()
    {
        if (!parent::install() || !$this->registerHook('updateCarrier')) {
            return false;
        }

        $carrier = $this->addCarrier();
        $this->updateCarrier($carrier, (int)Configuration::get('PS_LANG_DEFAULT'));

        return true;
    }

    public function uninstall()
    {
        $carrier = new Carrier((int)Configuration::get('STOREPICKUP_CARRIER_ID'));
        $carrier->delete();

        return parent::uninstall();
    }

    protected function addCarrier()
    {
        $carrier = new Carrier();
        $carrier->name = 'Store Pickup';
        $carrier->is_module = true;
        $carrier->deleted = 0;
        $carrier->shipping_handling = false;
        $carrier->range_behavior = 0;
        $carrier->shipping_external = true;
        $carrier->external_module_name = $this->name;
        $carrier->need_range = false;
        $carrier->position = Carrier::getHigherPosition() + 1;

        foreach (Language::getLanguages(true) as $language) {
            $carrier->delay[(int)$language['id_lang']] = 'Pick up at store';
        }

        if ($carrier->add()) {
            $groups = Group::getGroups(true);

            foreach ($groups as $group) {
                $carrier->addGroup((int)$group['id_group']);
            }

            return $carrier;
        }

        return false;
    }

    public function getOrderShippingCost($params, $shipping_cost)
    {
        return 0;
    }

    public function getOrderShippingCostExternal($params)
    {
        return 0;
    }

    public function hookupdateCarrier($params)
    {
        $id_carrier_old = (int)$params['id_carrier'];
        $id_carrier_new = (int)$params['carrier']->id;

        if ((int)Configuration::get('STOREPICKUP_CARRIER_ID') == $id_carrier_old) {
            Configuration::updateValue('STOREPICKUP_CARRIER_ID', $id_carrier_new);
        }
    }

    public function hookActionCarrierProcess($params)
    {
        $cart = $params['cart'];
        $address = new Address($cart->id_address_delivery);

        if ($params['carrier']->id == Configuration::get('STOREPICKUP_CARRIER_ID')) {
            // Set tax rules based on the store location (State A)
            $id_tax_rules_group = // your tax rules group id for State A (5%)
            $cart->id_address_delivery = // your store address id in State A;
            $cart->update();

            $address->id_country = // your store country id in State A;
            $address->id_state = // your store state id in State A;
            $address->save();

            // Assign the tax rules group to the carrier
            $params['carrier']->id_tax_rules_group = $id_tax_rules_group;
            $params['carrier']->save();
        }
    }
}

In this code, replace the placeholders (e.g., // your tax rules group id for State A (5%)) with the actual values from your store configuration.

After adding the code to "storepickup.php", follow these steps:

1. Zip the "storepickup" folder and install the module via the "Modules" tab in your PrestaShop back office.

2. Once the module is installed and enabled, a new shipping carrier named "Store Pickup" should be available in your shipping carriers list.

3. Edit the "Store Pickup" carrier and set the desired tax rules group for State A (5% tax).

Now, when a customer selects "Store Pickup" as the shipping carrier, the 5% tax for State A will be applied, and the delivery address will be changed to the store address in State A.

Remember to test the functionality in a staging environment before deploying it to a live store.

  • Like 1
Link to comment
Share on other sites

Hi @Eutanasio,

Thanks a lot for taking the time to provide a well written solution.
I took an alternative approach to solve the issue, but I will definitely test your code and let you know the outcome.  

Thanks again.

Regards,

Atiqur

Link to comment
Share on other sites

3 minutes ago, atiquratik said:

Hi @Eutanasio,

Thanks a lot for taking the time to provide a well written solution.
I took an alternative approach to solve the issue, but I will definitely test your code and let you know the outcome.  

Thanks again.

Regards,

Atiqur

good! if it works and you feel like, you can send me 500€ otherwise it's free 🤣

PS: just kidding

Link to comment
Share on other sites

  • 11 months later...

Eutanasio - I was looking for code to solve exactly this problem so I'm hoping you can help. I implemented your code as explained (thank you!) but no matter what I've tried, the sales tax is still computed based on the customer's delivery address, not the store's delivery address, even though it is specified in the code. The sales tax definitely changes when I select "Store Pickup" as my delivery method but the sales tax is still not being computed correctly. It appears that it's still referring to the general Sales Tax rules instead of the new rule. Any ideas on what to tweak to get this working properly? Happy to hire you for the job if that's easier! Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...