Jump to content

Country restriction not working for a specific country


Freddykhoury

Recommended Posts

Hello, 

I need to restrict some payment methods from the website. 

When I select only "Lebanon" it allow any country to see this specific payment, if i select any other country ex: sweeden only, only sweeden can see the payment methods

ex: I unchecked all countires from COD and slected Lebanon, the result is that all countries can see COD. if I select any country it is only seen by this country 

any idea?

 

thank you    

Link to comment
Share on other sites

For restricting the country from the credit card or COD payment, kindly try by adding the below code in the main file of the credit card/COD payment module, 

 

Function : hookPaymentOptions($params)

=>First Fetch the customer country from the saved address using the below code

 $address = new Address(context::getContext()->customer->id_address_delivery);

$selected_add_country_id = $address ->id_country;


=>Then check whether the current country is US or not, if not then use the below code

return;


The above code will check whether the customer country is US or not, if not then that payment method is not available on the front for that customer, otherwise the payment method is available.

Link to comment
Share on other sites

I am not sure I did the right update. I am not a professional programer. 

but it did not work when I tried to add it in the COD . the settings is set to be displayed only in Lebanon and it shows from any country 

the problem is only when I select Lebanon 

can you tell me where should it be added in the ps_cashondelivery.php

Thanks

Link to comment
Share on other sites

Kindly use the below code for reference

public function hookPaymentOptions($params)
    {
        if (!$this->active) {
            return;
        }
        $address = new Address(context::getContext()->cart->id_address_delivery);
        $selected_add_country_id = $address ->id_country;
        if($selected_add_country_id != "Your-Country-Code"){
            return;
        }
        
        
        if (!$this->checkCurrency($params['cart'])) {
            return;
        }

 

Link to comment
Share on other sites

as I said I am not a programer nd dont know where to add this funciton 
There is already a function for hook payment 

 

where should it be added?

public function hookPaymentOptions(array $params)

    {

        if (empty($params['cart'])) {

            return [];

        }

 

        /** @var Cart $cart */

        $cart = $params['cart'];

 

        if ($cart->isVirtualCart()) {

            return [];

        }

 

        $cashOnDeliveryOption = new PaymentOption();

        $cashOnDeliveryOption->setModuleName($this->name);

        $cashOnDeliveryOption->setCallToActionText($this->trans('Pay by Cash on Delivery', [], 'Modules.Cashondelivery.Shop'));

        $cashOnDeliveryOption->setAction($this->context->link->getModuleLink($this->name, 'validation', [], true));

        $cashOnDeliveryOption->setAdditionalInformation($this->fetch('module:ps_cashondelivery/views/templates/hook/paymentOptions-additionalInformation.tpl'));

 

        return [$cashOnDeliveryOption];

    }

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