2

I've searched all around and haven't been able to find a solution to this.

I'm on Magento 2.1.7 and need to remove the option for shipping to any province other than Ontario (Canada) from my check-out page. But at the same time I need billing addresses from other provinces to be accepted, just no shipments to anywhere other than Ontario.

Thanks in advance for the help!

4

It is possible to change it in the checkout for example create these module files

Block/Checkout/LayoutProcessor.php

<?php
namespace Vendor\Module\Block\Checkout;

use Magento\Directory\Helper\Data as DirectoryHelper;

/**
 * Class LayoutProcessor
 * @package Vendor\Module\Block\Checkout
 */
class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
{


    /**
     * @var DirectoryHelper
     */
    protected $directoryHelper;

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

    /**
     * @param array $result
     * @return array
     */
    public function process($result)
    {

        if ($result['components']['checkout']['children']['steps']
        ['children']['shipping-step']['children']['shippingAddress']) {



                    $shippingAddressFieldSet = $result['components']['checkout']['children']['steps']
                    ['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'];

                    $regionOptions = $shippingAddressFieldSet['region_id']['options'];

                    // update the options array according to your logic $regionOptions

                    $shippingAddressFieldSet['region_id']['options'] = $regionOptions;

                    $result['components']['checkout']['children']['steps']
                    ['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'] = $shippingAddressFieldSet;
                }
            }
        }


        return $result;
    }
}

and then your frontend di

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\Checkout\Block\Onepage">
        <arguments>
            <argument name="layoutProcessors" xsi:type="array">
                <item name="vendor_module_layoutprocessor" xsi:type="object">Vendor\Module\Block\Checkout\LayoutProcessor</item>
            </argument>
        </arguments>
    </type>
</config>
| improve this answer | |
1

If you want to remove from shipping and billing adress form, you can try this code

namespace Vendor\CheckoutScripts\Block\Checkout;

use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;

class LayoutProcessor implements LayoutProcessorInterface
{
    public function process($jsLayout)
    {
        $component = &$jsLayout['components']['checkout']['children']['steps']['children'];
        $componentBilling = &$component['billing-step']['children']['payment']['children']['payments-list']['children'];
        foreach ($componentBilling as $keyBilling => $value) {
            if (preg_match('/-form/', $keyBilling)) { 
                unset($componentBilling[$keyBilling]['children']['form-fields']['children']['region_id']);
            }
        }

        $componentShipping = &$component['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'];
        if(!empty($componentShipping['region_id'])) {
            unset($componentShipping['region_id']);
        }

        return $jsLayout;
    }
}

=> 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\Checkout\Block\Onepage">
        <arguments>
            <argument name="layoutProcessors" xsi:type="array">
                <item name="vendor_checkoutscripts_layoutprocessor" xsi:type="object">Vendor\CheckoutScripts\Block\Checkout\LayoutProcessor</item>
            </argument>
        </arguments>
    </type>
</config>
| improve this answer | |

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.