2

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)

2 Answers 2

1

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;
    }
}
1
  • This did'nt work for me , I am using Magento 2.4.2
    – Ajith
    Jul 9, 2021 at 7:38
0

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;
    }
1
  • You were on the right lines with this Poojan so thank you :) However I have edited my post with the solution I found to be more suitable to my purpose. Thanks again for your help though!
    – SNAFU
    May 18, 2019 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.