Question

I need to calculate shipping charge after discount is applied on cart subtotal

for example
cart subtotal: $170.00
Discount: -$15.00
Shipping: $17.00

Here i want it to calculate shipping charges after discount is added to cart subtotal,

for example
cart subtotal: $170.00
    Discount: -$15.00
    Shipping: $15.50

because it should first remove $15 from $170 and then it should calculate shipping charges from the remaining one.

Can you anyone please tell me the way to do this or any workaround for it

thanks in advance

Was it helpful?

Solution

I have added this in my custom shipping method, and it works for me. This should be added on your collectrates function inside your module.

$s_price = $request->getPackageValueWithDiscount();
$request->setPackageValue($s_price);

OTHER TIPS

Anyone who wants to use a plugin to implement this should use something like this

public function beforeGetRate(
    \Acme\Shipping\Model\ResourceModel\Carrier\Matrixrate $subject,
    $request
) {
    // Shipping package value should be calculated
    // with discount applied
    // 
    $getPackageValueWithDiscount = $request->getPackageValueWithDiscount();
    $request->setPackageValue($getPackageValueWithDiscount);

    return [$request];
}

Where \Acme\Shipping\Model\ResourceModel\Carrier\Matrixrate is whatever shipping method you want to change.

If others solutions doesn't work for you, try to use mine:
Every default methods you can find is in Magento\OfflineShipping\Model\Carrier folder.
Then find collectRates function and change the logic as you want.
Here is function of Free Shipping:

public function collectRates(RateRequest $request)
{
    if (!$this->getConfigFlag('active')) {
        return false;
    }

    /** @var \Magento\Shipping\Model\Rate\Result $result */
    $result = $this->_rateResultFactory->create();

    $this->_updateFreeMethodQuote($request);

    if ($request->getFreeShipping() || $request->getBaseSubtotalInclTax() >= $this->getConfigData('free_shipping_subtotal')
    ) {
        // custom calculate shipping method using subtotal and discount.
        // old line was used subtotal only, not included discount.
        // Get subtotal include discount.
        $subtotalInclDiscount = $request->getPackageValueWithDiscount();
        if ($subtotalInclDiscount >= $this->getConfigData('free_shipping_subtotal')) {
            /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
            $method = $this->_rateMethodFactory->create();

            $method->setCarrier('freeshipping');
            $method->setCarrierTitle($this->getConfigData('title'));

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

            $method->setPrice('0.00');
            $method->setCost('0.00');

            $result->append($method);
        }

    }

    return $result;
}

I've add this:

        // custom calculate shipping method using subtotal and discount.
        // old line was used subtotal only, not included discount.
        // Get subtotal include discount.
        $subtotalInclDiscount = $request->getPackageValueWithDiscount();
        if ($subtotalInclDiscount >= $this->getConfigData('free_shipping_subtotal')) {
              // old code
        }

Hope this help.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top