0

I want to display a custom notice on cart page "if customer's order total above $299, then he is qualified for free shipping". For that I have imported following table rates:

Country | Region/State | Zip/Postal Code | Order Subtotal (and above) | Shipping Price

US | * | * | 0 | 15

US | * | * | 299 | 0

I can get single table rate using collectRatesByAddress() function of \Magento\Shipping\Model\Shipping class, but I can't get multiple table rates. Please see sample code as follows:

$quote = $this->_checkoutSession->getQuote();
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCountryId("US") 
            ->setCity("New York") 
            ->setPostcode("11209") 
            ->setRegionId("") 
            ->setRegion("") 
            ->setCollectShippingRates(true)->collectShippingRates(); 

$shippingCarrier = 'tablerate';
$carriers[$shippingCarrier] = '';

$result = $this->_shippingModel->collectRatesByAddress($shippingAddress, array_keys($carriers))->getResult();

foreach ($result->getAllRates() as $rate) {
    echo $rate->getPrice();
}

Please give me any solution/suggestion/hint to resolve this problem.

Thanks in appreciation.

1

Have you tried using the method \Magento\Shipping\Model\Shipping::collectRates ?

Or, Alternatively you may query the database table shipping_tablerate which has the table rates stored in it.

  • After investigation, I found that collectRatesByAddress() returns applicable table rate data based on subtotal. So finally I used suggested alternate solution. @nikin Thank you for the guide lines. – Dhara Bhatti Dec 7 '18 at 6:11

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.