0

It seems in Magento 2, we can only configure Allowed Countries per WEBSITE or STORE. but not STOREVIEWS.

enter image description here

I found this solution https://magento.stackexchange.com/a/245401/26668, where we can add Allowed Countries to STOREVIEW directly to the table core_config_data

enter image description here

The problem is, it doesn't seem that I can get the Allowed Countries programmatically.

I have tried following,

use Magento\Directory\Model\AllowedCountries;

public function __construct(
    AllowedCountries $allowedCountries
)
{
    $this->allowedCountries = $allowedCountries;
}

 $allowedCountries = $this->allowedCountries->getAllowedCountries("default",0);

Which works fine, and it returns allowed countried for default website to me.

but if I want to get allowed countries of a specific storeview that has store ID 12, for example,

$allowedCountries = $this->allowedCountries->getAllowedCountries("stores",12);

it gives me an error.

"Warning: Invalid argument supplied for foreach() in /home/websites/public/vendor/magento/module-directory/Model/AllowedCountries.php on line 66"

Any ideas how to get allowed countries by storeview?

  • 1
    do you have a column in table core_config_data with scope "stores" and "scope_id" 12 and "path" gener/country/allow as in this solution magento.stackexchange.com/a/245401/26668 ? – Altravista Aug 20 '19 at 14:58
  • yes, i have those columns. I have add a screenshot to the question. please have a look. – Magento Learner Aug 20 '19 at 15:05
  • I found the problem. I must write ScopeCode with [ ], so it has to be, $allowedCountries = $this->allowedCountries->getAllowedCountries("stores",[12]); – Magento Learner Aug 20 '19 at 15:26
  • 1
    great write your answer :) – Altravista Aug 20 '19 at 15:28
0

I found it out, I must use array for store Ids, so I need to write Store Ids in [], as following

$allowedCountries = $this->allowedCountries->getAllowedCountries("stores",[12]);
0

Can you please check this link: https://www.dckap.com/blog/how-to-restrict-specific-us-states-in-checkout-magento-2/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.