Question

use Magento\Quote\Model\Quote\Address\RateRequest;
    $destCountryId = $request->getDestCountryId();
    $destState = $request->getDestRegionCode();
    $destCity = $request->getDestCity();

use this code I can get the address in my custom shipping method. but if people select address no entered into input,sometime I can't get RegionCode,getDestRegionCode() return null.

After observation, I found if State/Province is entered into input not select from Magento after checkout the address will be saved.Next time to select this address in Shipping Address page, getDestRegionCode() return null.

What's wrong with my code? How to get State/Province reliably? enter image description here

In this way getDestRegionCode() return null not 'zhejiang '

enter image description here

In this way entered zhejiagn getDestRegionCode() can get right value 'zhejiang'

Was it helpful?

Solution 2

Finnal I Find no way to get region If region is entered not select from form when customer checkout select address.

But We can use di.xml to rewirete

Magento\Quote\Model\Quote\Address

public function getRegionCode()
{
    $regionId = $this->getData('region_id');
    $region = $this->getData('region');

    if (!$regionId && is_numeric($region)) {
        if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
            $this->setData('region_code', $this->getRegionModel($region)->getCode());
        }
    } elseif ($regionId) {
        if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) {
            $this->setData('region_code', $this->getRegionModel($regionId)->getCode());
        }
    } elseif (is_string($region)) {
        $this->setData('region_code', $region);
    } elseif (is_array($region)) {
        $this->setData('region_code', $region['region']);
    }
    return $this->getData('region_code');
}

Fist you need to noee which data is you need , then do like this code.

elseif (is_array($region)) {
        $this->setData('region_code', $region['region']);
    }

Then , we can use

$request->getDestRegionCode();

to get value.

OTHER TIPS

Magento\Quote\Model\Quote\Address\RateRequest is work fine when we calculating shipping method.

Understand how the Shipping address is saved & shipping methods are calculated at checkout.

At checkout steps, when a user enters shipping country Or Shipping region or Selecting an existing then an ajax request happen which is take address fields data from Shipping Address form and set address data to Magento\Quote\Model\Quote\Address\RateRequest object and which used all shipping methods for calculating the shipping cost and it's availability . But request does not save the address to the Database table quote_addrss.

So, in that request, if you use Magento\Quote\Model\Quote\Address\RateRequest then you will get right $request->getDestCountryId() etc.

But Shipping Address is saved when we select a shipping method at the checkout by click on Next button. that time's ajax does not request shipping cost calculation method, So if use Magento\Quote\Model\Quote\Address\RateRequest on that time , on your code that does not give right address details.

Magento\Quote\Model\Quote\Address\RateRequest is only working fine when you shipping method is calculated.

Update

use $request->getDestRegionCode() getting Region field data. Take look on method \Magento\Customer\Model\Address\AbstractAddress::getRegionCode()

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top