2

How i can get current shipping address details in custom shipping module,

here my code

   <?php
public function collectRates(RateRequest $request)
{
    if (!$this->getConfigFlag('active')) {
        return false;
    }

    /** @var \Magento\Shipping\Model\Rate\Result $result */
    $result = $this->_rateResultFactory->create();

    //$shippingPrice = $this->getConfigData('price');
    $shippingPrice = "121";
    $method = $this->_rateMethodFactory->create();
    $method->setCarrier($this->_code);
    $method->setCarrierTitle($this->getConfigData('title'));
    $method->setMethod($this->_code);
    $method->setMethodTitle($this->getConfigData('name'));
    $method->setPrice($shippingPrice);
    $method->setCost($shippingPrice);
    $result->append($method);

    return $result;
} 

?>

i want get the current shipping address and need to display shipping price based in shipping address.

3 Answers 3

7

If you open this class you can able to see some getter/setter for address.

So inside shipping method you have RateRequest $request object. You can access this by following way:


$request->getDestCountryId();
$request->getDestStreet();

etc.

7
  • @sonal, i have little issue with multiple prices. how can i display different price with same shipping methods with variants. i want return multiple variations in above method
    – Ramesh
    Commented Sep 19, 2017 at 17:44
  • 1
    Check usps module, how they handle multiple price variant. github.com/magento/magento2/blob/2.1/app/code/Magento/Usps/…
    – Sohel Rana
    Commented Sep 20, 2017 at 1:21
  • @SohelRana I've created one plugin and called method afterCollectRates. I'm getting RateRequest object empty in the plugin method. Can you please advice?
    – Deeps
    Commented Apr 30, 2018 at 10:17
  • @Deeps show your class.
    – Sohel Rana
    Commented Apr 30, 2018 at 10:43
  • @SohelRanaI'm able to get the info using CheckoutSession i.e. $this->_checkoutSession->getQuote()->getShippingAddress()->getStreet(). But it's not working for visitor's, do you have any idea?
    – Deeps
    Commented Apr 30, 2018 at 13:37
0

To get the current user selected shipping address in shipping method use RateRequest Object request in collectRates method as,

Country Id          -> $request->getDestCountryId()
Region Id           -> $request->getDestRegionId()
Region code(String) -> $request->getDestRegionCode()
Zipcode (Postal)    -> $request->getDestPostcode()
City                -> $request->getDestCity()
Street              -> $request->getDestStreet()
All quote items     -> $request->getAllItems()
-2

Try $request->getDestCity()
To get the shipping destination city

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.