Table rates shipping based on price AFTER DISCOUNT – Magento

Magento has quite a big bug which enables the shipping module to calculate shipping costs without discount or shipping rules (if they are applied).

It cost me hours to find a workaround, so if you use it, I’d really appreciate it if you refer to this blog.

How I fixed it

Go to:

/app/code/local/Mage/Shipping/Model/Carrier/Tablerate.php

Add the following code at around line 77 (in the collectRates function):

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

Now, the shipping rates are calculated on the subtotal minus the discount. I hope this solves the problem (it does in 1.7)

24 thoughts on “Table rates shipping based on price AFTER DISCOUNT – Magento”

  1. Thank you, this solved our problem with calculating shipping costs when using discount coupons in Magento 1.6.1.

  2. Hello, sorry, I am having some problems to apply those lines. I am using a free extension for shipping rates “MATRIXRATE”. May it be different because this extension?

  3. Hello,

    finally it worked for me using a matrixshipping rates when changing the “PackageValue” for “PackageValueWithDiscount” and when I notice you have to have the \’tablerates\’ shipping method active (in sytem – shipping methods) , even if the spreadsheet is empty, then the \’matrixrates\’ plugin also gets the correct subtotal.

    thanks, best regards.

  4. Where to add ?????/
    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
    if (!$this->getConfigFlag(‘active’)) {
    return false;
    }

    // exclude Virtual products price from Package value if pre-configured
    if (!$this->getConfigFlag(‘include_virtual_price’) && $request->getAllItems()) {
    foreach ($request->getAllItems() as $item) {
    if ($item->getParentItem()) {
    continue;
    }
    if ($item->getHasChildren() && $item->isShipSeparately()) {
    foreach ($item->getChildren() as $child) {
    if ($child->getProduct()->isVirtual()) {
    $request->setPackageValue($request->getPackageValue() – $child->getBaseRowTotal());
    }
    }
    } elseif ($item->getProduct()->isVirtual()) {
    $request->setPackageValue($request->getPackageValue() – $item->getBaseRowTotal());
    }

  5. Did not work for me in 1.9. Maybe I’m missing somthing? If someone has it working in 1.9 can you post like to file and code? Thank

  6. Hi.
    I’m using a promotion cart rule:
    – Free Shipping when the country is Spain
    – Subtotal is equal or greater than 49.59 Euros.

    When the people have 49.59 Euros in the cart, this works fine.

    I have promotion cart rule:
    – Discount code 10%

    But when is applied a discount coupon of 10% then still the free shipping and the subtotal is minus than 49.59 with the coupon applied.

    How i can solve it?
    Thank you very much.

    PD: I’m using 2 modules with tablerates.csv configured.

  7. Hi ,

    Great Thanks, saved my lots of time. It worked for me in magento 1.9.0.1. Thanks mate.

    I had replaced getPackageValue with getPackageValueWithDiscount in Tablerate.php file.

    1. Hi,

      When i submit the order getting error message after chnaged the code, “There was an error processing your order… please specify shipping method error in magento with table rate”. Please help me to solve this.

  8. Getting crazy, doing exactly like above but it will not work in 1.9.2.4

    public function collectRates(Mage_Shipping_Model_Rate_Request $request->getPackageValueWithDiscount())
    {
    if (!$this->getConfigFlag(‘active’)) {
    return false;
    }

    // exclude Virtual products price from Package value if pre-configured
    if (!$this->getConfigFlag(‘include_virtual_price’) && $request->getAllItems()) {
    foreach ($request->getAllItems() as $item) {
    if ($item->getParentItem()) {
    continue;
    }
    if ($item->getHasChildren() && $item->isShipSeparately()) {
    foreach ($item->getChildren() as $child) {
    if ($child->getProduct()->isVirtual()) {
    $request->setPackageValue($request->getPackageValueWithDiscount() – $child->getBaseRowTotal());
    }
    }
    } elseif ($item->getProduct()->isVirtual()) {
    $request->setPackageValue($request->getPackageValueWithDiscount() – $item->getBaseRowTotal());
    }
    }
    }

  9. Just to clarify what Raul said for Matrixrate.php, this is a webshopapps plugin. This hack works perfect on Magento 1.9.2.4

    add this code at the top of the collectRates function

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

    Then change all the “getPackageValue” code to “getPackageValueWithDiscount”

    Thank you for this thread.
    -J

Leave a Reply

Your email address will not be published. Required fields are marked *