Skip to content

Magento2 Allow all country in billing address but restrict few countries in shipping address

I want people can choose any country for billing but few countries allowed for shipping. I followed this solution it works great for the sipping address but, I need it for the billing country I tried following but it’s not working:

<?php

namespace TrecksCheckoutcountryPlugin;
use MagentoDirectoryHelperData as DirectoryHelper;

class LayoutProcessor
{

    protected $countryOptions;

    public function __construct(
        MagentoStoreModelStoreManagerInterface $storeManager,
        MagentoDirectoryModelResourceModelCountryCollectionFactory $countryCollection,
        DirectoryHelper $directoryHelper
    ) {
        $this->_storeManager = $storeManager;
        $this->countryCollectionFactory = $countryCollection;
        $this->directoryHelper = $directoryHelper;
    }

   
    public function afterProcess(
        MagentoCheckoutBlockCheckoutLayoutProcessor $subject,
        $result
    ) {

        $result['components']['checkout']['children']['steps']['children']['billing-step']['children']
        ['billingAddress']['children']['billing-address-fieldset']['children']['country_id'] = [
            'component' => 'Magento_Ui/js/form/element/select',
            'config' => [
                'customScope' => 'billingAddress',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/select',
                'id' => 'drop-down',
            ],
            'dataScope' => 'billingAddress.country_id',
            'label' => 'Country',
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => [],
            'sortOrder' => 70,
            'id' => 'drop-down',
            'options' => $this->getCountryOptions()
        ];
        return $result;
    }

    public function getStoreId()
    {
        return $this->_storeManager->getStore()->getId();
    }

    //We have refernce this function from MagentoCheckoutBlockCheckoutDirectoryDataProcessor
    public function getCountryOptions()
    {
            $countryIds = array("US");
            //$countryIds = array("US","CA");
           
            $countryselection = $this->countryCollectionFactory->create()->loadByStore(
                $this->_storeManager->getStore()->getId());
            $countryselection->addFieldToFilter('country_id', ['in' => $countryIds]);
            $countryselection = $countryselection->toOptionArray();
            $countryarray = $this->orderCountryOptions($countryselection);

        return $countryarray;
    }


    public function orderCountryOptions(array $countryOptions)
    {
        $topCountryCodes = $this->directoryHelper->getTopCountryCodes();
        if (empty($topCountryCodes)) {
            return $countryOptions;
        }

        $headOptions = [];
        $tailOptions = [[
            'value' => 'delimiter',
            'label' => '──────────',
            'disabled' => true,
        ]];
        foreach ($countryOptions as $countryOption) {
            if (empty($countryOption['value']) || in_array($countryOption['value'], $topCountryCodes)) {
                $headOptions[] = $countryOption;
            } else {
                $tailOptions[] = $countryOption;
            }
        }
        return array_merge($headOptions, $tailOptions);
    }
}