Jump to content

limit certain postal codes


Charles 01

Recommended Posts

Good morning,

work on a prestashop, I got a module which allows you to limit certain postal codes (I would like to exclude certain areas like Corsica or overseas territories for delivery in France) The module works really well and limits Dom Tom postal codes (all postal codes between 96000 and 98000). I would like to edit the module so that it also limits the postal codes of Corsica (postal codes between 20000 and 25000). Here is the code present in the module PHP file, could you tell me how to edit this file so that they also limit postal codes between 20000 and 25000 please?



  

 /**
     * Check Post code for France and DOM-TOM
     *
     * @param array $params
     * @return bool|void
     */
    public function hookActionValidateCustomerAddressForm($params)
    {
        if ( Configuration::get(strtoupper($this->name) . '_ENABLE_POSTCODE_RESTRICTION') &&
            Configuration::get(strtoupper($this->name) . '_COUNTRY_ID_FR') ) {
            $is_valid = true;

            $form = $params['form'];
            $country = $form->getField("id_country");
            $id_country = $country->getValue();
            if ($id_country == (int) Configuration::get(strtoupper($this->name) . '_COUNTRY_ID_FR')) {
                $zip = $form->getField("postcode");
                $zipcode = (int)$zip->getValue();
                if ($zipcode > self::DOM_TOM_POSTCODE_RANGE_START && $zipcode != 98000) {
                    $zip->addError($this->l('Invalid country according to zip code - should be a DOM TOM one'));
                    $is_valid = false;
                }
            }
            return $is_valid;
        }
    }

Sincerely.

Edited by endriu107 (see edit history)
Link to comment
Share on other sites

On 10/25/2023 at 2:55 PM, Charles 01 said:

Good morning,

work on a prestashop, I got a module which allows you to limit certain postal codes (I would like to exclude certain areas like Corsica or overseas territories for delivery in France) The module works really well and limits Dom Tom postal codes (all postal codes between 96000 and 98000). I would like to edit the module so that it also limits the postal codes of Corsica (postal codes between 20000 and 25000). Here is the code present in the module PHP file, could you tell me how to edit this file so that they also limit postal codes between 20000 and 25000 please?

Hello,

To limit the postal codes for Corsica (between 20000 and 25000) along with the existing restriction for DOM-TOM postal codes (between 96000 and 98000), you can modify the hookActionValidateCustomerAddressForm function as follows:

  1. Define constants for the Corsica postal code range at the top of your class.
  2. Modify the condition in the if statement to check if the postal code lies in the Corsica range.

Here's the modified code:

// ...
const CORSICA_POSTCODE_RANGE_START = 20000;
const CORSICA_POSTCODE_RANGE_END = 25000;

// ...

public function hookActionValidateCustomerAddressForm($params)
{
    if ( Configuration::get(strtoupper($this->name) . '_ENABLE_POSTCODE_RESTRICTION') &&
         Configuration::get(strtoupper($this->name) . '_COUNTRY_ID_FR') ) {
        $is_valid = true;

        $form = $params['form'];
        $country = $form->getField("id_country");
        $id_country = $country->getValue();

        if ($id_country == (int) Configuration::get(strtoupper($this->name) . '_COUNTRY_ID_FR')) {
            $zip = $form->getField("postcode");
            $zipcode = (int)$zip->getValue();

            if (
                ($zipcode >= self::DOM_TOM_POSTCODE_RANGE_START && $zipcode <= self::DOM_TOM_POSTCODE_RANGE_END && $zipcode != 98000) || 
                ($zipcode >= self::CORSICA_POSTCODE_RANGE_START && $zipcode <= self::CORSICA_POSTCODE_RANGE_END)
            ) {
                $zip->addError($this->l('Invalid postal code for delivery.'));
                $is_valid = false;
            }
        }
        return $is_valid;
    }
}

This will ensure that any postal codes from both the Corsica and DOM-TOM ranges are limited as per your requirements.

We hope these suggestions help simplify the process for you. Let us know if you have any more questions or need further assistance. We're here to help!

Link to comment
Share on other sites

On 10/30/2023 at 3:49 PM, Charles 01 said:

Good morning,

Thank you for the solution, however, I'm not sorry but where should I integrate this piece of code into which file?

Sincerely

Good morning,

I apologize for any confusion. To integrate the provided code, you should follow these steps:

1. Open your PrestaShop module's main PHP file. This file typically has the same name as your module and is located in the root folder of your module.

2. Locate the `hookActionValidateCustomerAddressForm` function within this file. This function handles the validation of customer addresses.

3. Insert the code provided at the top of the module's main PHP file to define the constants for the Corsica postal code range. Place it just above the `hookActionValidateCustomerAddressForm` function.

4. Modify the `hookActionValidateCustomerAddressForm` function as shown in the provided code, adjusting the conditions as needed.

5. Save the changes and upload the updated module to your PrestaShop installation.

This should ensure that the code is correctly integrated into your module, and it will restrict postal codes according to your specified requirements for Corsica and DOM-TOM.

If you encounter any issues or have further questions, please don't hesitate to reach out. We're here to assist you.

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...