cancel
Showing results for 
Search instead for 
Did you mean: 

Overriding countries error when submitting order

Overriding countries error when submitting order

Hello, 

I have created a plugin to override country options so that when you are in frontend it will allow the countries you have set in the configuration. And when you are in adminhtml/backend it will allow ALL countries.

 

But when I try to submit an order from the backend, I get the following error: "Please check the shipping address information. Invalid value of "DK" provided for the countryId field."

 

I get the same error no matter which country I select for shipping.

 

<?php


use Magento\Directory\Model\AllowedCountries as Countries;
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;

class AllowedCountriesPlugin
{
    protected $_countryCollectionFactory;

    public function __construct(
        CollectionFactory $countryCollectionFactory
    ) {
        $this->_countryCollectionFactory = $countryCollectionFactory;
    }

    /**
     * @param Countries $subject
     * @param array $result
     * @return array
     * @throws LocalizedException
     */
    public function afterGetAllowedCountries(Countries $subject, array $result)
    {
        $fullListCountries = $this->getCountryCollection();

        $objectManager = ObjectManager::getInstance();
        $state = $objectManager->get('Magento\Framework\App\State');

        if ($state->getAreaCode() == 'adminhtml') {
            return $fullListCountries;
        }

        else {
            return $result;
        }

    }

    public function getCountryCollection()
    {
        return $this->_countryCollectionFactory->create()->loadData()->toOptionArray();
    }

}

 

1 REPLY 1

Re: Overriding countries error when submitting order


@MyGiftCardSite wrote:

Hello, 

I have created a plugin to override country options so that when you are in frontend it will allow the countries you have set in the configuration. And when you are in adminhtml/backend it will allow ALL countries.

 

But when I try to submit an order from the backend, I get the following error: "Please check the shipping address information. Invalid value of "DK" provided for the countryId field."

 

I get the same error no matter which country I select for shipping.

 

<?php


use Magento\Directory\Model\AllowedCountries as Countries;
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;

class AllowedCountriesPlugin
{
    protected $_countryCollectionFactory;

    public function __construct(
        CollectionFactory $countryCollectionFactory
    ) {
        $this->_countryCollectionFactory = $countryCollectionFactory;
    }

    /**
     * @param Countries $subject
     * @param array $result
     * @return array
     * @throws LocalizedException
     */
    public function afterGetAllowedCountries(Countries $subject, array $result)
    {
        $fullListCountries = $this->getCountryCollection();

        $objectManager = ObjectManager::getInstance();
        $state = $objectManager->get('Magento\Framework\App\State');

        if ($state->getAreaCode() == 'adminhtml') {
            return $fullListCountries;
        }

        else {
            return $result;
        }

    }

    public function getCountryCollection()
    {
        return $this->_countryCollectionFactory->create()->loadData()->toOptionArray();
    }

}

 


You are trying to override final static void m method in subclass , which is not permitted by Java compiler.