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?
In this way getDestRegionCode() return null not 'zhejiang '
In this way entered zhejiagn getDestRegionCode() can get right value 'zhejiang'
Best Answer
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 useMagento\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()