1
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'

2 Answers 2

1

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()

11
  • Sorry, may be you can't understand my problem .I need getDestRegionCode() 同get region for calculated shipping method . When I choose address not input address, getDestRegionCode() return null.
    – keke996
    Commented Apr 22, 2020 at 8:16
  • I use this function for calulate shipping method in my shipping carrier model collectRates()
    – keke996
    Commented Apr 22, 2020 at 8:34
  • then check ajax request which estimating the shipping method list on your browser. i Check address field values are passing properly or not.
    – Amit Bera
    Commented Apr 22, 2020 at 8:40
  • magento.com/default/rest/default/V1/carts/mine/estimate-shipping-methods-by-address-id select address ajax send request to this url ,param is entity_id in table customer_address_entity. use entity_id can find address and region has right value but region_id is 0. In this mode of operation getDestRegionCode() return null.
    – keke996
    Commented Apr 22, 2020 at 8:46
  • 1
    yes ,i use $request->getDestRegionCode(),but if region field was input type field before select address in this time,this time $request->getDestRegionCode() return null.
    – keke996
    Commented Apr 22, 2020 at 9:20
1

Finaly 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 rewrite

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.