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)

Était-ce utile?

La 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;
    }
}

Autres conseils

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;
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top