0

where we need to change the 'checkoutProvider' name for passing custom countries list in magento2 checkout page. I tried to change in checkout_index_index.xml file but not working. how to display in frontend checkout page.

$jsLayout['components']['checkoutProviderShip']['dictionaries'] = [
                    'country_id' => $this->getShipCountryOptions(),
                    'region_id' => $this->getRegionOptions(),
                ];

Thanks in Advance.

  • 1
    Share here that you have tried. – Sohel Rana Apr 18 at 13:11
  • yes. shared the code I modified the country data in code – venkata prasad Apr 18 at 13:14
0

You can use plugin over Magento\Checkout\Block\Checkout\DirectoryDataProcessor

And create after plugin on method process() and modify this function output.

Plugin class

<?php
namespace Stackexchange\Magento\Plugin;

use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\ObjectManager;

class DirectoryDataProcessorPlugin {
   /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var array
     */
    private $regionOptions;
    /**
     * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
     */
    private $regionCollection;

    public function __construct(
    \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollection,
    StoreManagerInterface $storeManager = null
    ) {
        $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
        $this->regionCollection = $regionCollection;
    }
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\DirectoryDataProcessor $subject,
        array $jsLayout
    ) {
        if (isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
            $jsLayout['components']['checkoutProvider']['dictionaries']['country_id'] = $this->getShipCountryOptions();
            $jsLayout['components']['checkoutProvider']['dictionaries']['region_id'] = $this->getRegionOptions();
        }
        return $jsLayout;
    }
    public function getShipCountryOptions()
    {
        // Write your code
    }
    private function getRegionOptions()
    {
        if (!isset($this->regionOptions)) {
            $this->regionOptions = $this->regionCollectionFactory->create()->addAllowedCountriesFilter(
                $this->storeManager->getStore()->getId()
            )->toOptionArray();
        }

        return $this->regionOptions;
    }    
}
  • have using plugin or class rewrite?? – Amit Bera Apr 18 at 13:24
  • use pluging is best solution – Amit Bera Apr 18 at 13:26
  • Only checkout.right> – Amit Bera Apr 18 at 13:27
  • if I use in the checkoutProvider data displaying fine but this is replacing the data so I'm using tha checkoutProviderShip but is not displaying in frontend – venkata prasad Apr 18 at 13:33
  • okay under your requirment – Amit Bera Apr 18 at 13:34

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.