5

The shipping method may be flatrate or FedEx or freeshipping, but I want to override this price with $1. How can I do this?

1 Answer 1

7

1. Register Plugin

app/code/Acme/StackExchange/etc/di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Address\Rate">
        <plugin name="Acme_StackExchange_Plugin_Quote_Model_Quote_Address_RatePlugin"
                type="Acme\StackExchange\Plugin\Quote\Model\Quote\Address\RatePlugin" sortOrder="10"/>
    </type>
</config>

2. Implement Plugin logic

app/code/Acme/StackExchange/Plugin/Quote/Model/Quote/Address/RatePlugin.php

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Plugin\Quote\Model\Quote\Address;

use Magento\Quote\Model\Quote\Address\Rate;
use Magento\Quote\Model\Quote\Address\RateResult\AbstractResult;
use Magento\Quote\Model\Quote\Address\RateResult\Method;

class RatePlugin
{
    public function afterImportShippingRate(
        Rate $subject,
        Rate $result,
        AbstractResult $rate
    ): Rate {
        if ($rate instanceof Method) {
            // some logic to checks
            // set custom price to method
            $result->setPrice(1);
        }

        return $result;
    }
}
1

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.