mattpark22
mattpark22

Reputation: 751

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?

Upvotes: 5

Views: 5817

Answers (4)

Kender
Kender

Reputation: 1254

I know that this question has been answered and accepted, but I would like to offer a way to do this without programming, or changing core files if at all possible

If you want to create your item as a "Virtual" item, you can exclude virtual items from shipping fees

If item is created already

Go to your database with a program like phpMyAdmin and navigate to catalog_product_entity, find your product SKU and change type_id to virtual

If item is not created

Create item as Virtual Product during the first step of product creation (instead of Simple Product or Bundled Product, etc.)

To exclude Virtual Products from shipping fees

Navigate to System > Configuration > Shipping Methods and under the Table Rates header set Include Virtual Products in Price Calculation to No

  • Please note, this will only work for, and is only necessary for, the Table Rates shipping method
  • UPS, USPS, FedEx and DHL determine by weight, so since Virtual Product types have a product weight of 0 it will not affect shipping costs for these methods

Upvotes: 0

elMarquis
elMarquis

Reputation: 7730

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

Upvotes: 2

Matteo
Matteo

Reputation: 1219

If I understood your question correctly, the solution is really easy and quick, here is how I've solved it:

In the file: Validator.php (/app/code/core/Mage/SalesRule/Model/) search for the string "case Mage_SalesRule_Model_Rule::FREE_SHIPPING_ITEM:" and just before the "break;" add this simple thing "$item->setWeight(0);"

This will force the weight of the item to be 0 so no shipping price will be calculated on it, and if you disable the free shipping option for hat item, the weight is considered and everything works fine.

Upvotes: 1

ʍǝɥʇɐɯ
ʍǝɥʇɐɯ

Reputation: 4022

In your sales rule set the Free Shipping to 'FOR MATCHING ITEMS ONLY'.

Upvotes: 2

Related Questions