1

I need to add $5 Handling Fee if Free Shipping method is selected.

enter image description here

3

1 Answer 1

1

Di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\OfflineShipping\Model\Carrier\Freeshipping">
        <plugin name="{Your_Plugin_Name}" type="{Vendor}\{Module}\Plugin\Freeshipping" />
    </type>
</config>

Plugin File

Freeshipping.php

class Freeshipping
{
    protected $rateResultFactory;
    protected $rateMethodFactory;

    public function __construct(
        \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
        \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
    ) {
        $this->_rateResultFactory = $rateResultFactory;
        $this->_rateMethodFactory = $rateMethodFactory;
    }

    public function afterCollectRates(\Magento\OfflineShipping\Model\Carrier\Freeshipping $subject, $result)
    {
        $result = $this->_rateResultFactory->create();
        $shippingPrice = "5.00"; //custom price

        $method = $this->_rateMethodFactory->create();
        $method->setCarrier('freeshipping');
        $method->setCarrierTitle($subject->getConfigData('title'));

        $method->setMethod('freeshipping');
        $method->setMethodTitle($subject->getConfigData('name'));

        $method->setPrice($shippingPrice);
        $method->setCost($shippingPrice);

        $result->append($method);
        return $result;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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