1

I'm using Magento CE 1.9.2.2 and Table Rates is my only shipping method.

The rate € 5,00 for all orders below € 99,95 and € 0,00 for all orders of € 99,95 or more.

This is what the Shipping Method step looks like when someone places an order of € 99,95 or more.

Magento Table Rates

Now I want to dynamically replace the "Best Way" carrier name with "Free Shipping" when the order is € 99,95 or more.

I've found out that in

app/design/frontend/base/default/template/checkout/onepage/shipping_method/available.phtml

the following code

<dt>
    <?php echo $this->escapeHtml($this->getCarrierName($code)) ?>
</dt>

is responsible for printing the carier name.

I have tried

<dt>
    <?php if ($this->getShippingPrice()== 0): ?>
        <?php echo $this->__('Free Shipping') ?>
    <?php else: ?>
        <?php echo $this->escapeHtml($this->getCarrierName($code)) ?>
    <?php endif; ?>
</dt>

But this doesn't work. This logic always prints "Free Shipping" even if the shipping price NOT 0,00.

What is the best way to build some logic into this?

0

First copy file from app/code/core/Mage/Shipping/Model/Carrier/Tablerate.php to

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

and replace bellow function from

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

        // Free shipping by qty
        $freeQty = 0;
        if ($request->getAllItems()) {
            $freePackageValue = 0;
            foreach ($request->getAllItems() as $item) {
                if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
                    continue;
                }

                if ($item->getHasChildren() && $item->isShipSeparately()) {
                    foreach ($item->getChildren() as $child) {
                        if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
                            $freeShipping = is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0;
                            $freeQty += $item->getQty() * ($child->getQty() - $freeShipping);
                        }
                    }
                } elseif ($item->getFreeShipping()) {
                    $freeShipping = is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0;
                    $freeQty += $item->getQty() - $freeShipping;
                    $freePackageValue += $item->getBaseRowTotal();
                }
            }
            $oldValue = $request->getPackageValue();
            $request->setPackageValue($oldValue - $freePackageValue);
        }

        if ($freePackageValue) {
            $request->setPackageValue($request->getPackageValue() - $freePackageValue);
        }
        if (!$request->getConditionName()) {
            $conditionName = $this->getConfigData('condition_name');
            $request->setConditionName($conditionName ? $conditionName : $this->_default_condition_name);
        }

        // Package weight and qty free shipping
        $oldWeight = $request->getPackageWeight();
        $oldQty = $request->getPackageQty();

        $request->setPackageWeight($request->getFreeMethodWeight());
        $request->setPackageQty($oldQty - $freeQty);

        $result = $this->_getModel('shipping/rate_result');
        $rate = $this->getRate($request);

        $request->setPackageWeight($oldWeight);
        $request->setPackageQty($oldQty);

        if (!empty($rate) && $rate['price'] >= 0) {
            $method = $this->_getModel('shipping/rate_result_method');

            $method->setCarrier('tablerate');
            $method->setCarrierTitle($this->getConfigData('title'));

            $method->setMethod('bestway');
            $method->setMethodTitle($this->getConfigData('name'));

            if ($request->getFreeShipping() === true || ($request->getPackageQty() == $freeQty)) {
                $shippingPrice = 0;
            } else {
                $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
            }

            $method->setPrice($shippingPrice);
            $method->setCost($rate['cost']);

            $result->append($method);
        } elseif (empty($rate) && $request->getFreeShipping() === true) {
            /**
             * was applied promotion rule for whole cart
             * other shipping methods could be switched off at all
             * we must show table rate method with 0$ price, if grand_total more, than min table condition_value
             * free setPackageWeight() has already was taken into account
             */
            $request->setPackageValue($freePackageValue);
            $request->setPackageQty($freeQty);
            $rate = $this->getRate($request);
            if (!empty($rate) && $rate['price'] >= 0) {
                $method = $this->_getModel('shipping/rate_result_method');

                $method->setCarrier('tablerate');
                $method->setCarrierTitle($this->getConfigData('title'));

                $method->setMethod('bestway');
                $method->setMethodTitle($this->getConfigData('name'));

                $method->setPrice(0);
                $method->setCost(0);

                $result->append($method);
            }
        } else {
            $error = $this->_getModel('shipping/rate_result_error');
            $error->setCarrier('tablerate');
            $error->setCarrierTitle($this->getConfigData('title'));
            $error->setErrorMessage($this->getConfigData('specificerrmsg'));
            $result->append($error);
        }

        return $result;
    }

**To**

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

    // Free shipping by qty
    $freeQty = 0;
    if ($request->getAllItems()) {
        $freePackageValue = 0;
        foreach ($request->getAllItems() as $item) {
            if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
                continue;
            }

            if ($item->getHasChildren() && $item->isShipSeparately()) {
                foreach ($item->getChildren() as $child) {
                    if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
                        $freeShipping = is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0;
                        $freeQty += $item->getQty() * ($child->getQty() - $freeShipping);
                    }
                }
            } elseif ($item->getFreeShipping()) {
                $freeShipping = is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0;
                $freeQty += $item->getQty() - $freeShipping;
                $freePackageValue += $item->getBaseRowTotal();
            }
        }
        $oldValue = $request->getPackageValue();
        $request->setPackageValue($oldValue - $freePackageValue);
    }

    if ($freePackageValue) {
        $request->setPackageValue($request->getPackageValue() - $freePackageValue);
    }
    if (!$request->getConditionName()) {
        $conditionName = $this->getConfigData('condition_name');
        $request->setConditionName($conditionName ? $conditionName : $this->_default_condition_name);
    }

    // Package weight and qty free shipping
    $oldWeight = $request->getPackageWeight();
    $oldQty = $request->getPackageQty();

    $request->setPackageWeight($request->getFreeMethodWeight());
    $request->setPackageQty($oldQty - $freeQty);

    $result = $this->_getModel('shipping/rate_result');
    $rate = $this->getRate($request);

    $request->setPackageWeight($oldWeight);
    $request->setPackageQty($oldQty);

    if (!empty($rate) && $rate['price'] >= 0) {
        $method = $this->_getModel('shipping/rate_result_method');

        $method->setCarrier('tablerate');
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod('bestway');
        $method->setMethodTitle($this->getConfigData('name'));

        if ($request->getFreeShipping() === true || ($request->getPackageQty() == $freeQty)) {
            $shippingPrice = 0;
        } else {
            $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
        }

        $method->setPrice($shippingPrice);
        $method->setCost($rate['cost']);

        $result->append($method);
    } elseif (empty($rate) && $request->getFreeShipping() === true) {
        /**
         * was applied promotion rule for whole cart
         * other shipping methods could be switched off at all
         * we must show table rate method with 0$ price, if grand_total more, than min table condition_value
         * free setPackageWeight() has already was taken into account
         */
        $request->setPackageValue($freePackageValue);
        $request->setPackageQty($freeQty);
        $rate = $this->getRate($request);
        if (!empty($rate) && $rate['price'] >= 0) {
            $method = $this->_getModel('shipping/rate_result_method');

            $method->setCarrier('tablerate');
            $method->setCarrierTitle('Free Shipping');

            $method->setMethod('bestway');
            $method->setMethodTitle($this->getConfigData('name'));

            $method->setPrice(0);
            $method->setCost(0);

            $result->append($method);
        }
    } else {
        $error = $this->_getModel('shipping/rate_result_error');
        $error->setCarrier('tablerate');
        $error->setCarrierTitle($this->getConfigData('title'));
        $error->setErrorMessage($this->getConfigData('specificerrmsg'));
        $result->append($error);
    }

    return $result;
}

To

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

    // Free shipping by qty
    $freeQty = 0;
    if ($request->getAllItems()) {
        $freePackageValue = 0;
        foreach ($request->getAllItems() as $item) {
            if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
                continue;
            }

            if ($item->getHasChildren() && $item->isShipSeparately()) {
                foreach ($item->getChildren() as $child) {
                    if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
                        $freeShipping = is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0;
                        $freeQty += $item->getQty() * ($child->getQty() - $freeShipping);
                    }
                }
            } elseif ($item->getFreeShipping()) {
                $freeShipping = is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0;
                $freeQty += $item->getQty() - $freeShipping;
                $freePackageValue += $item->getBaseRowTotal();
            }
        }
        $oldValue = $request->getPackageValue();
        $request->setPackageValue($oldValue - $freePackageValue);
    }

    if ($freePackageValue) {
        $request->setPackageValue($request->getPackageValue() - $freePackageValue);
    }
    if (!$request->getConditionName()) {
        $conditionName = $this->getConfigData('condition_name');
        $request->setConditionName($conditionName ? $conditionName : $this->_default_condition_name);
    }

    // Package weight and qty free shipping
    $oldWeight = $request->getPackageWeight();
    $oldQty = $request->getPackageQty();

    $request->setPackageWeight($request->getFreeMethodWeight());
    $request->setPackageQty($oldQty - $freeQty);

    $result = $this->_getModel('shipping/rate_result');
    $rate = $this->getRate($request);

    $request->setPackageWeight($oldWeight);
    $request->setPackageQty($oldQty);

    if (!empty($rate) && $rate['price'] >= 0) {
        $method = $this->_getModel('shipping/rate_result_method');

        $method->setCarrier('tablerate');
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod('bestway');
        $method->setMethodTitle($this->getConfigData('name'));

        if ($request->getFreeShipping() === true || ($request->getPackageQty() == $freeQty)) {
            $shippingPrice = 0;
        } else {
            $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
        }

        $method->setPrice($shippingPrice);
        $method->setCost($rate['cost']);

        $result->append($method);
    } elseif (empty($rate) && $request->getFreeShipping() === true) {
        /**
         * was applied promotion rule for whole cart
         * other shipping methods could be switched off at all
         * we must show table rate method with 0$ price, if grand_total more, than min table condition_value
         * free setPackageWeight() has already was taken into account
         */
        $request->setPackageValue($freePackageValue);
        $request->setPackageQty($freeQty);
        $rate = $this->getRate($request);
        if (!empty($rate) && $rate['price'] >= 0) {
            $method = $this->_getModel('shipping/rate_result_method');

            $method->setCarrier('tablerate');
            $method->setCarrierTitle('Free Shipping');

            $method->setMethod('bestway');
            $method->setMethodTitle($this->getConfigData('name'));

            $method->setPrice(0);
            $method->setCost(0);

            $result->append($method);
        }
    } else {
        $error = $this->_getModel('shipping/rate_result_error');
        $error->setCarrier('tablerate');
        $error->setCarrierTitle($this->getConfigData('title'));
        $error->setErrorMessage($this->getConfigData('specificerrmsg'));
        $result->append($error);
    }

    return $result;
}

I have added Free Shipping label at line no.184 like this $method->setCarrierTitle('Free Shipping');

  • I have just tried this but Free Shipping does not appear at all. Have you tested this yourself? Thanks! – ForMat Dec 18 '15 at 9:10

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.