6

In my webshop I have two shipping methods, free shipping (for orders above 200 euro) and flat rate shipping (5 euro). I do not have other shipping methods for other countries or something.

I want to show the flat rate shipping if the order is under 200, else the free shipping method. I want to show this in the cart (not the estimate function, but as soon as the user goes to the cart it will see the appropriate shipping).

I created a plugin for the file: vendor/magento/module-tax/Block/Checkout/Shipping.php. This class has a function called displayShipping. This is called from the cart page to check if shipping should be displayed.

I created a before method for this function, so before Magento does the check it executes my function:

$shippingAddress = $this->getQuote()->getShippingAddress();
if (!$shippingAddress->getCountryId()) {
    $this
        ->getQuote()
        ->getShippingAddress()
        ->setShippingMethod('flatrate_flatrate')
        ->setCountryId('NL')
        ->save();
}

Now, shipping cost does show up in the cart, but the shipping amount is set to 0 instead of the 5 euro.

How can I fix this?

1
  • Have you got solution to it, If not let me know in comment Commented Nov 18, 2016 at 10:15

1 Answer 1

1

I created an event :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch_checkout_cart_index">
        <observer name="set-default-shipping-method" instance="Vendor\Checkout\Observer\SetDefaultShippingMethod"/>
    </event>
</config>

And in my event :

<?php

namespace Vendor\Checkout\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\Quote\Address;

use \Magento\Checkout\Model\Session as CheckoutSession;

class SetDefaultShippingMethod implements ObserverInterface
{
    /** @var CheckoutSession */
    protected $checkoutSession;

    /**
     * @param CheckoutSession $checkoutSession
     */
    public function __construct(CheckoutSession $checkoutSession) {
        $this->checkoutSession = $checkoutSession;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $quote = $this->checkoutSession->getQuote();
        $shippingAddress = $quote->getShippingAddress();

        if(!$shippingAddress->getCountryId()){
            $shippingAddress->setCountryId('FR'); // Default country for calculate shipping rates if no one given
        }

        if(!$shippingAddress->getShippingMethod()){
            $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('mondialrelay_pickup'); // Default Shipping method
        }


    }
}

This event is called when the user call the checkout cart index action

1
  • No. Its not working. event called but shipping method not selected.
    – Indian
    Commented Jul 31, 2017 at 6:54

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.