Question

I am simply looking to get the shipping rates from the current quote and override them via a plugin/interceptor but have no idea where I'm going wrong here:

<?php

namespace Vendor\Module\Plugin\Model\Quote\Address\RateResult;

use \Psr\Log\LoggerInterface;

class MethodPlugin
{
    protected $_customerSession;
    protected $_logger;

    public function __construct(
        LoggerInterface $logger,
        \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->_logger = $logger;
        $this->_customerSession = $customerSession;
    }

    public function beforeSetPrice($subject, $price)
    {
        $this->_logger->debug("Current price is " . $price);

        return $price;
    }

    public function afterSetPrice($subject, $price)
    {
        $price = 3.33;
        $this->_logger->debug("New price is " . $price);

        return $price;
    }
}

My thinking was that the beforeSetPrice() method would log the current price (which it does) and the afterSetPrice() method would log the new price (which it does) AND update the price shown in the shipping rates on checkout (which it doesn't)

Was it helpful?

Solution

Solution worked for me:

<?php

namespace Vendor\Model\Plugin\Model\Quote\Address\RateResult;

class MethodPlugin
{

    public function afterSetPrice($subject)
    {
        $new_price = "2.00";

        $subject->setData("price", $new_price);

        return $subject;
    }
}

OTHER TIPS

To set shipping rates on checkout just add custom price in beforeSetPrice function. It will update the price.

Note: if you have multiple shipping methods then it will update price for all shipping method.

public function beforeSetPrice($subject, $price)
    {
        $this->_logger->debug("Current price is " . $price);
        $price = 3.33;
        return $price;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top