2

Is it possible to display a message in a pop-up box or alert on the cart page when a customer is close to the free delivery threshold i.e.

"Spend another £5 and you qualify for free delivery"

2
  • You could probably set up an observer on catalog_product_load_after like is suggested here. You would have to get the cart total and compare it to your free delivery threshold, but it shouldn't be too hard to set up. You can get the cart totals with something like Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()
    – travisw
    Jul 28 '15 at 14:30
  • @travisw. Why the event catalog_product_load_after? Also you should not use getGrandTotal. The free shipping conditions are based on getSubtotal.
    – Marius
    Jul 28 '15 at 14:49
4

Create your own module. Let's call it StackExchange_FreeShipping
you will need the following files:

app/etc/modules/StackExchange_FreeShipping.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <StackExchange_FreeShipping>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Shipping />
            </depends>
        </StackExchange_FreeShipping>
    </modules>
</config>

app/code/local/StackExchange/FreeShipping/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <StackExchange_FreeShipping>
            <version>0.0.1</version>
        </StackExchange_FreeShipping>
    </modules>
    <global>
        <helpers>
            <stackexchange_freeshipping>
                <class>StackExchange_FreeShipping_Helper</class>
            </stackexchange_freeshipping>
        </helpers>
        <models>
            <stackexchange_freeshipping>
                <class>StackExchange_FreeShipping_Model</class>
            </stackexchange_freeshipping>
        </models>
    </global>
    <frontend>
        <translate>
            <modules>
                <StackExchange_FreeShipping>
                    <files>
                        <default>StackExchange_FreeShipping.csv</default>
                    </files>
                </StackExchange_FreeShipping>
            </modules>
        </translate>
        <events>
            <controller_action_predispatch_checkout_cart_index>
                <observers>
                    <stackexchange_freeshipping>
                        <class>stackexchange_freeshipping/observer</class>
                        <method>checkFreeShipping</method>
                    </stackexchange_freeshipping>
                </observers>
            </controller_action_predispatch_checkout_cart_index>
        </events>
    </frontend>
</config>

app/code/local/StackExchange/FreeShipping/Model/Observer.php - the observer that checks if you are close to free shipping

<?php
class StackExchange_FreeShipping_Model_Observer 
{
    public function checkFreeShipping($observer) 
    {
        //if free shipping is not enabled, do nothing
        if (!Mage::getStoreConfig('carriers/freeshipping/active')) {
            return $this;
        }
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        //if there are no products in the cart, again, do nothing
        if (count($quote->getAllItems()) == 0 ){
            return $this;
        }
        //get the quote subtotal
        $subtotal = $quote->getSubtotal();
        //get the free shipping
        $freeShippingValue = Mage::getStoreConfig('carriers/freeshipping/free_shipping_subtotal');
        //check if the cart is below the free shipping amount
        if ($freeShippingValue > $subtotal) {
            Mage::getSingleton('checkout/session')->addNotice(
                Mage::helper('stackexchange_freeshipping')->__(
                    'Your cart has the has the value of %s. In order to get free shipping you need to order for %s more',
                    '<span style="color:green">'.Mage::helper('core')->currency($subtotal, true, false).'</span>',
                    '<span style="color:red">'.Mage::helper('core')->currency($freeShippingValue - $subtotal, true, false).'</span>'
                )
            );
        }

    }
}

app/locale/en_US/StackExchange_FreeShipping.csv - the translation file. You can add this for every language you have:

"Your cart has the has the value of %s. In order to get free shipping you need to order for %s more","Your cart has the has the value of %s. In order to get free shipping you need to order for %s more"

Of course change the text if you want it different.
Clean the cache and give it a go.
You should see now in the cart a notice message above the cart table with the message

Your cart has the has the value of 34$. In order to get free shipping you need to order for 15$ more.
3
  • Thanks for this @Marius. I've implement your suggestion but I am getting a white screen. I've checked the event log nothing in there.
    – user9252
    Jul 28 '15 at 15:47
  • Try to debug step by step. I haven't tested the code. I just addapted it from an extension i built a long time ago. It might have syntax errors.
    – Marius
    Jul 28 '15 at 16:24
  • I got it working. I forgot to create the helper.
    – user9252
    Aug 13 '15 at 12:59
0

On completing the answer of @Marius, you have to add else the helper, otherwise you will have an error without

app/code/local/StackExchange/FreeShipping/Helper/Data.php

<?php
class StackExchange_FreeShipping_Helper_Data extends Mage_Core_Helper_Abstract{

}
1
  • Works great for me! Only thing I'd like to add (@Prince) is to clarify the information on the "helper" step, for those that aren't soaring with the eagles (like me). The "helper" file name that he forgot to add is Data.php. As in: app/code/local/StackExchange/FreeShipping/Helper/Data.php His helper code works fine. Aug 10 '17 at 6:25

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.