Magento – Magento 2 Get Shipping Rates of All active shipping methods

magento2shippingshipping-methods

I have a special case where I want to show all active shipping methods with applied estimated shipping cost to login customer if customer has default shipping address set in address book.

The following link describe how to show all active shipping methods in a custom module.

Magento 2 How to get all active shipping methods?

But I did not find any code that also display shipping rate of each method. So I need to show shipping rate of each shipping method to login customer. These customer already has some shipping address.

Best Answer

I just create some code hopefully its help u.

create test.php on your project root directory.

require __DIR__. '/app/bootstrap.php'; 

require __DIR__.'/TestApp.php';

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager(); /** @var
\Magento\Framework\App\State $state */ $state =
$obj->get(\Magento\Framework\App\State::class);
$state->setAreaCode('frontend');

/** @var \Magento\Framework\App\Http $app */ $app =
$bootstrap->createApplication(TestApp::class);

$bootstrap->run($app);

Then create TestApp.php on your project root directory.

use Magento\Shipping\Model\Config\Source\Allmethods;

class TestApp extends \Magento\Framework\App\Http implements
\Magento\Framework\AppInterface {

    public function launch() {
        /** @var \Magento\Shipping\Model\Config\Source\Allmethods $allmethods */
        $allmethods = $this->_objectManager->get(Allmethods::class);

        echo"<pre>";
        print_r($allmethods->toOptionArray());

        return $this->_response;
    }

}