Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Free Shipping for Match Items Only - Using Table Rates Based on Order Total

Can I achieve the following with Magento:

I'm using a table rate system of price vs destination. And I've got a shopping cart price rule of Free Shipping for products with the attribute free_shipping -> to set yes.

This works fine if you have normal products in the basket OR free shipping products in the basket. However if you have both a normal product AND a free shipping product in the basket. It calculates the shipping based on the order total including the product with free shipping.

How can I correct this, so the shipping is applied only to the order total of products not including free shipping, when both type of products exist in the basket?

Is there a Magento Plugin for this? Thanks in advance.

like image 731
mattpark22 Avatar asked Jul 01 '11 09:07

mattpark22


People also ask

What is Table rate shipping in Magento?

Magento 2 Table Rate Shipping extension helps Magento 2 stores to create unlimited custom shipping methods and shipping rates based on various condition combinations. Especially, shipping rate calculation is upgraded with volumetric weight & destination, rate-setting types and more.


1 Answers

I had the same problem and implemented a small code change to make it work.

Basically forget the promotion rule. Disable it. It doesn't seem to work properly with the shipping rates table when applying free shipping to individual items. The shipping rules seem to take precedence and calculate based on cart total.

The trick is to subtract the free shipping items from the cart total at the point the Shipping Rates module is making the calculation.

In my case everything inside a particular category (id:15) was to get free shippping. But you can amend the logic to suit your needs.

You will need to amend the following file (or copy it to your local codebase to do things properly).

app\code\core\Mage\Shipping\Model\Carrier\Tablerate.php

CHANGE THIS:

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());
                }
            }
        }

TO THIS:

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
    //And Exclude items which should have Free Shipping
    if (!$this->getConfigFlag('include_virtual_price') && $request->getAllItems()) {

       $freeshipping_category_id = 15;

        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());
                    }
                        //If it's in the free shipping, remove it's value from the basket
                        $arr_category_ids = $child->getProduct()->getCategoryIds();
                        if ( in_array($freeshipping_category_id, $arr_category_ids) ) {
                            $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal());
                        }
                }
            } elseif ($item->getProduct()->isVirtual()) {
                $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal());
            }
                //If it's in the free shipping category, remove it's value from the basket
                $arr_category_ids = $item->getProduct()->getCategoryIds();
                if ( in_array($freeshipping_category_id, $arr_category_ids) ) {
                    $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal());
                }
        }
    }
like image 139
elMarquis Avatar answered Sep 26 '22 08:09

elMarquis