0

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

3

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);
| improve this answer | |
  • I can confirm this works. Except I was not using a custom shipping method, but standard Tablerate, for which I had to override Magento\OfflineShipping\Model\Carrier\Tablerate and add that line on 108. Tablerate is also what was selected in Cart by default when only one shipping method was available. For clarification: I am on Magento 2.1.10 – Jacques Feb 14 '18 at 13:24
0

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.

| improve this answer | |
0

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.

| improve this answer | |

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.