Magento Asked on December 2, 2021
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.
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="MagentoDirectoryModelResourceModelRegionCollection">
<plugin name="product_Limit_State_Filter_Plugin" type="VendorModulePluginStateFilter"/>
</type>
</config>
Vendor/Module/Plugin/StateFilter.php
<?php
namespace VendorModulePlugin;
class StateFilter
{
protected $disallowed = [
'Guam',
'Armed Forces Pacific',
'Hawaii',
'Alaska'
];
protected $scopeConfig;
protected $allowedUsStates;
public function __construct(
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoCheckoutModelSession $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;
}
}
Answered by akashmalik on December 2, 2021
Answered by Teja Bhagavan Kollepara on December 2, 2021
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 ModelCheckoutLayoutProcessorPlugin.php
<?php
namespace VendorModuleModelCheckout;
use MagentoFrameworkStdlibArrayManager;
class LayoutProcessorPlugin
{
/**
* @param MagentoCheckoutBlockCheckoutLayoutProcessor $subject
* @param array $jsLayout
* @return array
*/
protected $arrayManager;
/**
* LayoutProcessor constructor.
* @param ArrayManager $arrayManager
*/
public function __construct(
ArrayManager $arrayManager
) {
$this->arrayManager = $arrayManager;
}
public function afterProcess(
MagentoCheckoutBlockCheckoutLayoutProcessor $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;
}
}
Answered by vlade on December 2, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP