Magento2 – How to Get Current Address Data in Custom Shipping Method

magento2shipping-methods

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.

Best Answer

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.

Related Topic