1

I am working in Magento-2, In my requirement, I want to show particular country on checkout page shipping address country drop-down, this only applicable for the shipping address form section, in billing address it has to show all the country.

We have option in backend (Go to -> Store -> Configuration -> General -> Allow countries -> Select "United States"), if i select this, it's applicable for shipping & billing address area.

I've also tried with layoutprocessing after plugin, but there is no luck, do you have any other way to achieve the requirment.

Also, I've added the custom Jquery script on onepage.html, but this is also not worked

require(['jquery'], function($) {       
        $('select[name=country_id]')
            .find('option')
            .remove()
            .end()
            .append('<option value="whatever">text</option>')
    });

If you know any other good way please suggest, thanks in advance for your support.

1 Answer 1

1

app/code/Vendor/Checkout/Plugin/Block/DirectoryDataProcessor

Override DirectoryDataProcessor to add a new directory for the shipping step country_id component:

<?php

namespace Vendor\Checkout\Plugin\Block;

use Magento\Checkout\Block\Checkout\DirectoryDataProcessor as BaseDirectoryDataProcessor;

class DirectoryDataProcessor
{
    /**
     * @param BaseDirectoryDataProcessor $subject
     * @param $jsLayout
     * @return mixed
     */
    public function afterProcess(
        BaseDirectoryDataProcessor $subject,
        $jsLayout
    ) {

        if (!isset($jsLayout['components']['checkoutProvider']['dictionaries']['shipping_country'])) {
            $jsLayout['components']['checkoutProvider']['dictionaries']
                ['shipping_country'] = $this->getShippingCountryOptions();
        }

        if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children']['country_id'])) {

            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
            ['shippingAddress']['children']['shipping-address-fieldset']['children']['country_id']
            ['imports']['initialOptions'] = 'index = checkoutProvider:dictionaries.shipping_country';
              //do the same for ['imports']['setOptions']
       }
        return $jsLayout;
    }

    /**
     * Create the list of the custom countries you want
     *
     * @return array
     */
    private function getShippingCountryOptions()
    {
        $options = [
            [
                'value' => 'US',
                'label' =>  __('United States'),
                'is_region_visible' => 1,
                'is_default' => 1
            ]
        ];

        if (count($options) > 0) {
            array_unshift(
                $options,
                ['title' => '', 'value' => '', 'label' => __('Please select a country.')]
            );
        }

        return $options;
    }
}

Don't forget to add the plugin to your di.xml

app/code/Vendor/Checkout/etc/frontend/di.xml

 <type name="Magento\Checkout\Block\Checkout\DirectoryDataProcessor">
    <plugin name="shipping_address_custom_country_list" type="Vendor\Checkout\Plugin\Block\DirectoryDataProcessor" sortOrder="1"/>
 </type>

That's it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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