0

I'm develop a module for shipping method. I need order's address to calculate freight.

use Magento\Quote\Model\Quote\Address\RateRequest;

$destCountryId = $request->getDestCountryId();
$destState = $request->getDestRegionCode();
$destCity = $request->getDestCity();

If customer not login ,they need to input address like picture enter image description here I can use my code to get address. But if customer have logined and select address. enter image description here My code can get country and city ,but can't get State with getDestRegionCode(). What's wrong with my code.Or may be someone can tell me how to get address is right.

0

In your Model Carrier, add the following code after "$destPostcode = $request->getDestPostcode();"

if ($request->getDestRegionId()) {
        $regionCollection = $this->_regionCollectionFactory->create();
        $regionCollection->addFilter('main_table.region_id', $request->getDestRegionId());
        $region = $regionCollection->getFirstItem();

        $request->setDestRegionCode($region->getCode());
}
$destRegionCode = $request->getDestRegionCode();

You will also need to add the regionCollecttionFactory to your Constructor:

\Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
| improve this answer | |
  • Thanks for your answer.But ,there isn't function setRequest(RateRequest $request) in my Model,what should I do.And ,if customer's state is not select from list is input by heself like 'zhejiang' ,Your code still can help me ? Please help me agin,thanks. – keke996 Apr 20 at 7:05
  • @keke996 I updated my answer, please take a look now. – Andresa Martins Apr 20 at 7:12
  • thanks,and what's function _getQuotes(),i create it before your answer. – keke996 Apr 20 at 7:23
  • _getQuotes() is the function that will call the API to retrieve the shipping rates, You probably gave a different name to it. – Andresa Martins Apr 20 at 7:25
  • sorry , I can't find _getQuotes() function in my code,I just $result->append($method) and return in collectRates(). now I add your setRequest() into my code and $this->setRequest($request) in collectRates(), now my page is worng .can't find _countryFactory – keke996 Apr 20 at 7:38

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.