About Club osCommerce

Showcasing osCommerce…the good, the bad and the ugly!

Follow osc_pro on Twitter

How Much To Spend For Free Shipping

Written By Gary on Feb 13 2009 · Comments (7) Follow osc_pro on Twitter

On the osCommerce Forum, Jim asks;

I notice that oscommerce has the default ability to have all orders over a certain amount to qualify for free shipping. Is there a contribution that would add text to the effect of

"You are only [X amount of money] away from qualifying for free shipping."

With the amount of money being based on the cart contents of the time. It would be a nice device to have to push up the average order amount, anyone know of such a contribution?

There probably is a contribution, but by the time you've found it, and checked to make sure the code is OK, it's quicker to simply come up with some code that does the same thing.

Step 1. make the language definitions

Open up includes/languages/english.php and add the following lines of code directly before the end ?> in the file

PHP:
  1. define('SHIPPING_CHARGE', 'Only another %s until you qualify for free shipping.');
  2. define('SHIPPING_FREE', 'You qualified for FREE shipping!');

These lines have to be in the english.php as one or the other of these will show in ALL osCommerce pages in your store. Did you notice the %s rather than the actual price? We're going to do something special with that, read on :)

Step 2: Amend the Shopping Cart infoBox

In this file we are going to paste code which shows one or other of the language definitions based upon the value of the cart contents!

So, open up /includes/boxes/shopping_cart.php and paste the following code directly before this line of code <!-- shopping_cart_eof //-->

PHP:
  1. <tr>
  2.             <td class="main"><?php echo ($cart->show_total() <MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER) ? sprintf(SHIPPING_CHARGE, tep_output_string_protected($currencies->format(MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER - $cart->show_total()))) : SHIPPING_FREE; ?>
  3.           </tr>

Save both files and upload. Voila. Easy as 123.

Deconstructing the PHP

1. We know that the value of the cart contents is assigned to $cart->show_total()
2. We know that the value of the free shipping in the order_total module is assigned to MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER

So this piece of code: $cart->show_total() < MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER compares the two. Depending upon which is true (which is greater than the other), this piece of code: sprintf(SHIPPING_CHARGE, tep_output_string_protected($currencies->format(MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER - $cart->show_total()))) : SHIPPING_FREE; shows either the SHIPPING_CHARGE language, or the SHIPPING_FREE language.

How about that %s ?

Well, that is used by the sprintf() function to format data that is passed to it. You can see that part of our code looks like this: sprintf(SHIPPING_CHARGE, tep_output_string_protected($currencies->format(MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER - $cart->show_total()))) - the 2nd part of this code is the value %s (in other words the total of the free shipping amount LESS the cart total). Make sense?

Images, images, images

When the value of the cart is LESS than the Free Ship Amount;

And when the value of the cart is equal to or greater than the Free Ship Amount;

Easy as 123!


Buy Gary A Beer?
Buying me a "beer" helps me to keep my contributions updated and keep this blog alive - and you get a link from my homepage to your site. Cheers!


7 Comments

  1. Super cool, and thank you, I was looking for something like this a few days ago when we enabled free shipping in our shop.

    Comment by Tomee — February 24, 2009 @ 8:11 pm


  2. Is there any way to get this working inside header.php (I have a mini-cart up the top of my site) on 2.3.1? Thanks

    Comment by Trent — April 24, 2011 @ 3:21 am


  3. Trent – instructions would be no different for 2.3.1

    Comment by Gary — April 24, 2011 @ 8:52 am


  4. Hi Gary. Not sure what I was doing wrong before, but I gave it another go and sure enough, it works perfect :) Also added it to the actual shopping cart next to the checkout button.

    Thanks!

    Comment by Trent — April 24, 2011 @ 10:13 am


  5. Good work :)

    Comment by Gary — April 26, 2011 @ 4:18 pm


  6. Hi Gary,

    Can you please advice where to add it in OScommerce 2.3.1 shopping cart?

    Thank you!

    Comment by rub — December 27, 2011 @ 5:11 pm


  7. You can add it anywhere. Try right after the line which includes;

    echo $currencies->format($cart->show_total());

    Comment by Gary — January 19, 2012 @ 12:55 pm


Leave a comment

RSS feed for comments on this post · TrackBack URL

Hot 100 osCommerce Shops

View the osCommerce HOT 100These are the best looking, most exceptional osCommerce Stores as voted for by you.

New to osCommerce - get inspiration from these beautiful shops. Reckon your site has what it takes to become a member of the HOT 100? Submit it!