0

How to apply tablerate shipping on discounted subtotal ? In default magento it apply shipping on actual subtotal. If any coupon applied then shipping should be calculated on discounted amount.

For example,

These are shipping rates-

Country | Region/State | Zip/Postal Code | Order Subtotal (and above) | Shipping Price

USA | * | * | 0 | 4.5

USA | * | * | 25.01 | 10

USA | * | * | 50.01 | 11

Your subtotal is : $55, and coupon discount is: $15

By default magento will pick shipping as $11 on subtotal=$55

But after making all above changes it should pick shipping as $10 on discounted subtotal i.e. $55-$15 = $40

  • Have you got any solution? – Yogita Jul 27 '18 at 6:25
1

To do this we need to override some magento core files.

Override Mage_Shipping_Model_Carrier_Tablerate

In your config.xml file add below code -

<models>
    <shipping>
        <rewrite>
            <carrier_tablerate>Yourpackage_Yourmodule_Model_Carrier_Tablerate</carrier_tablerate>
         </rewrite>
    </shipping>
</models>

Create Tablerate.php file in your module of below path- Yourpackage/Yourmodule/Model/Carrier/Tablerate.php and rewrite function collectRates() as follows -

class Yourpackage_Yourmodule_Model_Carrier_Tablerate extends Mage_Shipping_Model_Carrier_Tablerate
{
    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        .............

        $rate = $this->getRate($request); //after this line of function add below code.......

        if ($request->getPackageValueWithDiscount() < 0) {
            $request->setPackageValueWithDiscount(0);
        }
        $originalPackageValue = $request->getPackageValue();
        $request->setPackageValue($request->getPackageValueWithDiscount());
        $rate = $this->getRate($request);
        $request->setPackageValue($originalPackageValue);

        .............

    }
}

Then we need to make some changes for discount calculation of default magento -

Override Mage_Sales_Model_Quote_Address

In your config.xml file add below code -

<models>
    <sales>
        <rewrite>
            <quote_address>Yourpackage_Yourmodule_Model_Sales_Quote_Address</quote_address>
        </rewrite>
     </sales>
</models>

Then Create Address.php file in your module of below path- Yourpackage/Yourmodule/Model/Sales/Quote/Address.php and rewrite function getBaseSubtotalWithDiscount() as follows -

class Yourpackage_Yourmodule_Model_Sales_Quote_Address extends Mage_Sales_Model_Quote_Address
{
    public function getBaseSubtotalWithDiscount()
    {
        $baseDiscountAmount = 0;
        $quote = Mage::getModel('sales/quote_address')->getCollection()
           ->addFieldToFilter('quote_id', $this->getQuoteId())
           ->addFieldToFilter('customer_address_id', $this->getCustomerAddressId())
           ->getFirstItem();
        $quoteData = $quote->getData();
        if ($this->getBaseDiscountAmount() != "") {
            return $this->getBaseSubtotal()+$this->getBaseDiscountAmount();
        } else {
            if (isset($quoteData['base_discount_amount'])) {
                $baseDiscountAmount = $quoteData['base_discount_amount'];
            } 
            return $this->getBaseSubtotal()+$baseDiscountAmount;
        }
    }
}

Now configure Tablerate shipping from admin. Create a coupon. Apply coupon code on frontend and do checkout and check shipping rates over there.

For example these are shipping rates-

Country | Region/State | Zip/Postal Code | Order Subtotal (and above) | Shipping Price

USA | * | * | 0 | 4.5

USA | * | * | 25.01 | 10

USA | * | * | 50.01 | 11

Your subtotal is : $55, and coupon discount is: $15

By default magento will pick shipping as $11 on subtotal=$55

But after making all above changes it will pick shipping as $10 on discounted subtotal i.e. $55-$15 = $40

  • Trying to get this to work on magento 1.9.x but I am getting below errors, any idea? Fatal error: Uncaught Error: Call to a member function getCollection() on boolean in /home/mg/domains/x.nl/public_html/devi/includes/src/Mage_Sales_Model_Quote.php:518 Stack trace: #0 – Sander van Zuidam Apr 10 '17 at 7:09

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.