cancel
Showing results for 
Search instead for 
Did you mean: 

Distance based Shipping price in magento 2

SOLVED

Distance based Shipping price in magento 2

0 down vote favorite
I created a custom shipping module. My custom shipping module is working fine. It is showing the price on cart or checkout page that I filled in the admin. Now I need to change a shipping price based on the pin code that user fill or choose in available shipping.

 

I need to integrate a courier API to get the dynamic price based on the customer's pin code. So I can't use the Table rate Method

 

Here is my code for Carrier Model:

namespace Custom\Shipping\Model\Carrier;

use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Rate\Result;

class Example extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
\Magento\Shipping\Model\Carrier\CarrierInterface
{
/**
 * @var string
 */
protected $_code = 'example';

/**
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
 * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
 * @param \Psr\Log\LoggerInterface $logger
 * @param \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory
 * @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
 * @param array $data
 */

public function __construct(
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
    \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
    array $data = []
) {
    $this->_rateResultFactory = $rateResultFactory;
    $this->_rateMethodFactory = $rateMethodFactory;
    parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}

/**
 * @return array
 */
public function getAllowedMethods()
{
    return [$this->_code => $this->getConfigData('name')];
}

/**
 * @param RateRequest $request
 * @return bool|Result
 */
public function collectRates(RateRequest $request)
{
    if (!$this->getConfigFlag('active')) {
        return false;
    }

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

    /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
    $method = $this->_rateMethodFactory->create();

    $method->setCarrier('example');
    $method->setCarrierTitle($this->getConfigData('title'));

    $method->setMethod('example');
    $method->setMethodTitle($this->getConfigData('name'));

    /*you can fetch shipping price from different sources over some APIs, we used price from config.xml - xml node price*/

    $amount = $this->getConfigData('price');

    $method->setPrice($amount);
    $method->setCost($amount);

    $result->append($method);

    return $result;
}
}

$amount should be dynamic based on the customer's pin code. How can I get the customer's pin code here to run a courier API or any one have some other method to do this.

 
I need to get a pin code which finds distance, based on the product's pin code and customer's pin code via API.

 

If anyone know about this, Please reply.

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Distance based Shipping price in magento 2

Hi @Neeraj02381,

 

I guess the $amount shouldn't be set by the 

 

You should use another method to set the amount.

You can use the $request params to calculate.


For example using something like $request->getDestPostcode() into that method you can choose or set different prices based on destination post code.

 

Maybe you could try to explore the data on $request to see which information you can apply to the calculation.

View solution in original post

2 REPLIES 2

Re: Distance based Shipping price in magento 2

Hi @Neeraj02381,

 

I guess the $amount shouldn't be set by the 

 

You should use another method to set the amount.

You can use the $request params to calculate.


For example using something like $request->getDestPostcode() into that method you can choose or set different prices based on destination post code.

 

Maybe you could try to explore the data on $request to see which information you can apply to the calculation.

Re: Distance based Shipping price in magento 2

Thanks @Damian Culotta,

 

You are amazing. It works fine.