About Club osCommerce

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

Follow osc_pro on Twitter

Show Shipping Method elsewhere on invoice.php

Written By Gary on Mar 17 2009 · Comments (0) Follow osc_pro on Twitter

In the standard osCommerce invoice page, the shipping method and cost is shown in the order_totals, as below;

You can see that this order used "Flat Rate (Best Way)"...

On the official osCommerce forum, Tony asked;

I wish to add the shipping/postal method selected during checkout as part of the delivery label. Does anybody know the code to do this?

I am using integrated label paper (the paper with a sticker attached which I peel off with the delivery address) and have got everything else I want to display on the label (OID, Shipping Address, Return Address).

My answer was;

Stored in order_totals table of database, so easiest way would be to create a function that gets the value of text based on order_id and ot_shipping. Sounds complicated but should be straightforward once you dig in.

The asked found a solution, but it's a horrible way;

PHP:
  1. for ($i = 0, $n = sizeof($order->totals); $i <$n; $i++) {
  2.    if ($order->totals[$i]['code'] == 'ot_shipping') {
  3.       $myshipping = $order->totals[$i]['title'];
  4.    }
  5. }
  6. echo $myshipping;

Which is iterating through all the order_totals in order to match "ot_shipping" and then display it. OK, so, it works fine and is not causing much overhead. But so ugly!

Here's a piece of code I just cooked up which gets the shipping method;

Step 1. Add this to /admin/includes/functions/general.php

PHP:
  1. function clubosc_get_shipping_method($oID) {
  2.     $shipping_query = tep_db_query("select title from " . TABLE_ORDERS_TOTAL . " where class='ot_shipping' AND orders_id = '" . (int)$oID . "'");
  3.     $shipping_value = tep_db_fetch_array($shipping_query);   
  4.     return substr($shipping_value['title'], 0, -1);   
  5. }

Step 2. Add this whereever you want in admin/invoice.php

PHP:
  1. <?php echo clubosc_get_shipping_method($oID); ?>

For this blog post, let's add it near the "Payment Method" line of text, so do this:

PHP:
  1. <tr>
  2.         <td class="main"><b><?php echo ENTRY_PAYMENT_METHOD; ?></b></td>
  3.         <td class="main"><?php echo $order->info['payment_method']; ?></td>
  4.       </tr>
  5.       <tr>
  6.         <td class="main"><b><?php echo ENTRY_SHIPPING_METHOD; ?></b></td>
  7.         <td class="main"><?php echo clubosc_get_shipping_method($oID); ?></td>
  8.       </tr>

Step 3. Add this to admin/includes/languages/english/invoice.php

PHP:
  1. define('ENTRY_SHIPPING_METHOD', 'Shipping Method:');

Save all files and upload. This are of your invoice should now look like this:

Easy as 123. Or is it?

Deconstructing the code

PHP:
  1. function clubosc_get_shipping_method($oID)

Simply a name, and a variable to pass through.

PHP:
  1. $shipping_query = tep_db_query("select title from " . TABLE_ORDERS_TOTAL . " where class='ot_shipping' AND orders_id = '" . (int)$oID . "'");
  2.     $shipping_value = tep_db_fetch_array($shipping_query);

Grabbing the correct data from the database. In this case scanning the orders_total table for the text column that has column values of the "order_id" and where the class text is "ot_shipping".

PHP:
  1. return substr($shipping_value['title'], 0, -1);

Showing the data on the page, without the ending ":" - this is necessary as the actual textual methos of shipping is "Flat Rate (Best Way):" - obviously we don't want to show the end colon!

Yes, it is as 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!


No Comments

No comments yet.

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!