1

I need to remove some states/regions from the checkout dropdown in the billing step, and different states/regions from the shipping step on Magento 2.2. I have used this How to remove Guam, Palau, and other US Territories from address State dropdown - Magento 2

it works but it removes them from both shipping and billing.

3

3 Answers 3

1
  1. log in to admin
  2. Go to stores > configuration > General > General > States options (Choose Required Country from drop down) from here
  3. Save configuration
  4. Go to PHPMYADMIN > choose table directory_country_region_name and directory_country_region
  5. Delete the particular desired record from that table. (You have to do it from the database. Otherwise, you have to use all existing state options. Why because The states were added already right? To make any change, you need to access the database.)
  6. Clear cache and re-index
0

I have removed them from the shipping step and I know this is not the perfect solution but we only ship to the US so it works fine for me.

create a module and create Model\Checkout\LayoutProcessorPlugin.php

<?php

namespace Vendor\Module\Model\Checkout;
use Magento\Framework\Stdlib\ArrayManager;

class LayoutProcessorPlugin
{

    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */

    protected $arrayManager;

    /**
     * LayoutProcessor constructor.
     * @param ArrayManager $arrayManager
     */
    public function __construct(
        ArrayManager $arrayManager
    ) {
        $this->arrayManager = $arrayManager;
    }



    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array  $jsLayout
    ) {
    $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
                ['shippingAddress']['children']['shipping-address-fieldset']['children']['region_id'] =
                [
                'component' => 'Magento_Ui/js/form/element/select',
                'config' => [
                    'customScope' => 'shippingAddress',
                    'template' => 'ui/form/field',
                    'elementTmpl' => 'ui/form/element/select', 
                    'id' => 'region-state',
                ],
                'dataScope' => 'shippingAddress.region_id',
                'label' => 'State/Province',
                'provider' => 'checkoutProvider',
                'visible' => true,
                'validation' => ['required-entry' => true,],
                'sortOrder' => 80,
                'id' => 'region-state',
                'options' => [

                [
                    'value' => '',
                    'label' => 'Please select region state or province.',
                ],
                [
                    'value' => '12',
                    'label' => 'California',
                ],
                [
                    'value' => '13',
                    'label' => 'Colorado',
                ]
                ],
            ];  
    return $jsLayout;

    }
    }
0

Vendor/Module/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Directory\Model\ResourceModel\Region\Collection">
           <plugin name="product_Limit_State_Filter_Plugin" type="Vendor\Module\Plugin\StateFilter"/>
      </type>
</config>

Vendor/Module/Plugin/StateFilter.php

<?php
namespace Vendor\Module\Plugin;

class StateFilter
{
  protected $disallowed = [
    'Guam',
    'Armed Forces Pacific',
    'Hawaii',
    'Alaska'
];
  protected $scopeConfig;
  protected $allowedUsStates;

  public function __construct(
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Checkout\Model\Session $session
  ) {
      $this->scopeConfig = $scopeConfig;
      $this->session = $session;
  }

  public function afterToOptionArray($subject, $options)
  {
      $result = array_filter($options, function ($option) {
          if (isset($option['label'])){
            // if($this->needHide()) { //remove this condition to hide them by default
              return !in_array($option['label'], $this->disallowed);
            // }
          }
          return true;
      });
      return $result;
  }

  public function needHide() {
    $blacklistSkus = array('sku1','sku2');
    $needToHide = false;
    $quote = $this->session->getQuote();
    if($quote->getAllItems()) {
      $items = $quote->getAllItems();
      foreach($items as $item) {
            if(in_array($item->getSku(),$blacklistSkus)) {
            $needToHide = true;         
        }
      }
    }
    return $needToHide;
  }
}

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.