Pizza Shop – Collection of Modules

Although osCommerce is usually used for selling products which are then sent to the buyer by post, it is possible to use osCommerce for establishments such as Pizza Shops or restaurants.

If we think about a Pizza Shop, which allows online ordering, we see a few features that most other shops don’t want or need;

1. ability to close down online ordering when the physical Pizza parlour is closed
– how about a Header Tag module that does this?

2. ability to charge the correct rate for deliveries
– how about a Shipping module that links into Google Maps and works out the exact distance from the Pizza Shop to the Customer in miles or kilometers, the shipping module then charges per km/mile.

3. ability to add in a Tip for the delivery driver
– how about a Order Total module that allows the buyer to add in a Tip.

Can you think of any more needs for a Restaurant?

Nice and easy modification for a beer

A person searching for a way to ensure unique data came across this post which I made for a client a couple of years ago.

Within 20 minutes, that new client had paypal’d me a couple of beers, I had installed the modification, tested it and tweaked it a little ready for him to use. And this is something that he had been trying to find for over 6 months!

Developers; that is the exact point of keeping every single piece of code you ever write. If someone comes to you in the future you can dig it out and re-use.

Challenges on the osCommerce Forum

Quite often, in the osCommerce forum, I state my idea about something and am usually challenged by someone or other to show that what I have said can be done, can be done.

I must admit that sometimes what I come up with in theory, cannot be done as easily in practice. Here’s a couple of recent examples…

1. Making osCommerce invisible (link)

I have a page filled with images (and nothing else). User clicks on as many images as he wants and that makes (with help of javascript) changes to an invisible form. The form then gets submitted and all the selected products are inserted to the osCommerce system. In other words, can I insert several products from a html form to osCommerce all at once?

My answer was “yes”, as I have recently coded up a system that allows this. With some extra changes and some .js trickery, we cna show just the image, and have the image clickable to update the “count” to be purchased.

2. Hidden Price Module (link)

I need a way to hide the pricing of specific products on my site. Rather than show pricing, it should read something like add to cart to see pricing. Once the item has been added to the cart, the pricing should be visible

I thought (with thinking nor readin the question properly) that this could be done in 1 line of code. My general idea was to amend the display_price function in the currency class file. Of course, this would work for removing pricing across the entire site, with prices shown in the shopping_cart.php listing. Just return “NULL” or such in the display_price function.

Then I was reminded that the original poster needs to do this for some products and not others. To enable this is a lot more difficult;

1. We need to be able to choose the products to not have the price displayed. This is similar to the admin function of my HAZMAT module. We’ll call this the “no display products”.
2. We need to be able to compare the product being displayed with the “no display products” & be able to remove the pricing for the “no display products” – this requires a change across multiple files to allow the passing of the product_id thru the display_price function. Then we can do some magic in the currencies class file based on the product id.

All in all, not straightforward. But still do-able, why not?

Making a simple “similar products” module

By “similar products” module, I mean s similar products to the product you are already looking at. What do we need to show;

1. products name (linked to it’s own product page).
2. products price

These need to be shown in the product info page. The “similar”ness feature will be simple list of the products in the same category as the product being looked at. We need to exclude the product being looked at from the list.

So, the SQL goes like Pastebin (including comments): http://pastebin.com/wn5zA2HF

Next up is to take the “upcoming_products.php” module and:

1. Find the line of code (it’s line 13) that starts:

$expected_query = tep_db_query(

Change that full line of code to the $similar_query that I pastebin’d.

2. Change EVERY instance of $expected to $similar throughout the file.

3. Find the line that starts (line 29):

‘ <td align=”right”>

In that line, change tep_date_short($expected[‘date_expected’]) to $currencies->format($expected[‘products_price’],tep_get_tax_rate($product_info[‘products_tax_class_id’]))

Save the file as “link_products.php” in the /includes/modules/ folder.

Open up “product_info.php” file and:

1. Find the line of code like this:

if ($product_info[‘manufacturers_id’] > 0) {

Right above that line of code add:

include(DIR_WS_MODULES . ‘like_products.php’);

Save the file.

Open up “includes/languages/english/product_info.php” file and:

1. Add the following line of code

define(‘TABLE_HEADING_LIKE_PRODUCTS’, ‘Other products you might like’);

Save the file.

Reload the page in your browser. If the product you are looking at has fellow products in its category, you will see a new box that looks like this:

20130123-1

If there is any demand I might package it up as a addon or more likely github it for easy reference.

Add a Special Offer banner to product listings…

In this tutorial I am going to show you how to add a “special” banner into the product listing in the “new products for…” module. At the end of the tutorial it will look like this:

As you can see, any products on special offer will have a banner overlay on top of the product image.

What you will need before beginning the tutorial

A. A transparent “special” image of the exact same dimensions as your “thumbnail images”. You can use the image below if you wish;

B. The “new products for…” module located at /includes/modules/new_products.php

Let’s get started

1. Upload the “special” image into the icons directory. This is located at /images/icons/

2. Open up new_products.php

2a. Find the two lines of code that start $new_products_query = tep_db_query
Add s.specials_new_products_price, before the first “if” in BOTH those lines.

This calls the specials price for the product.

2b. Find the following code:

$new_prods_content .= '<td width="33%" align="center" valign="top"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $new_products['products_image'], $new_products['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br /><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . $new_products['products_name'] . '</a><br />' . $currencies->display_price($new_products['products_price'], tep_get_tax_rate($new_products['products_tax_class_id'])) . '</td>';

Change to:

$new_prods_content .= '<td width="33%" align="center" valign="top">';
$new_prods_content .= '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">';
$new_prods_content .= tep_image(DIR_WS_IMAGES . $new_products['products_image'], $new_products['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
$new_prods_content .= '</a><br /><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . $new_products['products_name'] . '</a><br />' . $currencies->display_price($new_products['products_price'], tep_get_tax_rate($new_products['products_tax_class_id'])) . '</td>';

Here we have seperated the one long line of code into 4 separate lines of code. This allows us to easier manipulate one of those lines of code!

3. Find the following line of code in the 4 lines of code you just split

$new_prods_content .= tep_image(DIR_WS_IMAGES . $new_products['products_image'], $new_products['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);

Change to:

if ($new_products['specials_new_products_price'] > 0) {
$new_prods_content .= tep_image(DIR_WS_ICONS . 'special.png', $new_products['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT, 'style="background:url(' . DIR_WS_IMAGES . $new_products['products_image'] . ')"');
}
else {
$new_prods_content .= tep_image(DIR_WS_IMAGES . $new_products['products_image'], $new_products['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT, '');
}

Here we are setting up a logical choice based on the value of specials_new_products_price and saying; if the specials_price is greater than zero do “this” else do “that”. “That” is show the image normally, and “this” is show the newly made “special” image with the actual products image as a background.

Simple as 1-2-3.

How to show more categories of a linked product

In osCommerce, there is a mechanism by which a product can exist in multiple categories. It’s the “copy to” functionality on the product adding/editing page in admin. Obviously, the “copied” product needs to be LINKED (not DUPLICATED) as a linked product has the same ID as the original product whereas a duplicated product gets a new ID…

So, assuming we have 1 linked product across a number of categories we might want to show the customer the other categories in which it resides. There is no in-built way to do this, so we need to make some code to enable it.

Step 1. Add the code to get (and display) the other categories in which the product resides

Open up product_info.php, add:


$catQuery = tep_db_query("SELECT categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id='" . (int)$HTTP_GET_VARS['products_id'] . "' and categories_id NOT IN ('" . $current_category_id . "')");
while ($cat = tep_db_fetch_array($catQuery)) {
echo sprintf(ALSO_SEEN, $product_info['products_name'], tep_get_path($cat['categories_id']), clubosc_get_category_name($cat['categories_id']));
}

Here we are making a call to the database to get all the categories the product is in, other than the category we are presently looking at. There is no point in letting the customer know it exists “here” where he is looking.

Step 2: Add the language define

Open /includes/languages/english/product_info.php, add


define('ALSO_SEEN', '
%s can also be seen in < a href="' . tep_href_link(FILENAME_DEFAULT, '%s') . '" >%s< /a >');

Here we are passing 3 parameters to the text definition. These 3 parameters are; product name, category path, category name. These are passed by the code in Step 1.

Step 3: Add a new function to get and display the category name

Open /includes/functions/general.php, add


function clubosc_get_category_name($cat, $language = '') {
global $languages_id;

if (empty($language)) $language = $languages_id;

$categories_query = tep_db_query("select categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id = '" . (int)$cat . "' and language_id = '" . (int)$languages_id . "'");
$categories = tep_db_fetch_array($categories_query);

return $categories['categories_name'];
}

Here we pass through the category id (in Step 1 code), and use it to get the name of the other category/categories the product is in.

I have covered making functions in the past in this blog and I have also covered in quite some detail how “sprintf” works.

The result of this will be a line (per category the linked product is in) of text like this;

Of course, you can pretty up the text as you see fit, using .css. Enjoy.

Update all your prices in one go to 1 cent under the dollar

A customer of mine wanted to update all his prices to 1 cent under the dollar, eg go from 15.80 to 15.99. With 1000s of products that’s an utter waste of time, so here is one line of SQL to use in phpMyadmin;

UPDATE `products` SET `products_price` = (ceil(`products_price`)-0.01);

What this does is set the price to the next nearest whole number minus 1 cent.

Enjoy.

Add Math Protection to contact_us.php

This morning I was contacted to see about adding a question to osCommerce just like I have on this blog – where I ask a math question…I was going to suggest using Google recaptcha service, but the client did not want that.

And so…

Adding a question such as “what is 3+5” cuts down on automated bots spamming the heck out of you. A good place to implement this in contact_us.php, and the code changes are very simple.

The basis of the change is to create two random numbers, and ask the person contacting you to insert the total into an input field. The field is then checked to see if the answer is correct. If it is correct, then the contact email is sent to the shop owner. If the answer is incorrect, a “fail” message appears.

Step 1. Add the language defines to the language file.

We need two new defines, one for the “question” and one for the response if the question is incorrect.

/includes/languages/english.php
(and any other languages you operate in your shop)

http://pastebin.com/Wz5LmjJ9

Step 2. Add the logic to the contact file.

Here we need to create two numbers, add them up and ask the question to the user. http://pastebin.com/L7zaX7Tr

And that is all there is to it. Try it out in your store if you wish.

Notes

In line 21 of the pastebin I used base64_encode to create a hidden “answer” variable which creates something to check the users answer against. In line 6 I decode it, and then in line 8 I check one against the other. I use base64 to “hide” the value of $a + $b from prying eyes.

My use of base64_encode and base64_decode is no more than that…if you are unsure of what I am doing in the code, I suggest to ask question to help yourself to understand more fully, or not use the code. The base64 code might (assuming you use “site monitor”) ring alarm bells. I guarantee that if you use my “math protection” code as is, there is nothing harmful in it.

Summary

A useful way to cut down on the spam that is sent via contact forms. You could also add this to any other form in a similar way (eg, create_account.php, tell_a_friend.php and so on).

JavaScript Off? Check your site…

Some people, myself included, browse websites with .js turned off. If I need to access a sites javascript features I can easily make an exception on a site by site basis.

What this means is that 90% of sites I visit do not work, or work but look awful. For example, visiting any blogspot blog simply gives a blank page, nice usability Google!

In osCommerce with .js turned off, the vast majority is usable, but some parts appear broken. Two examples;

1. The buttons.

As the look/theme of the button is powered by .js, they react as simple links.

2. The images module.

The multiple images gallery and resizing is powered by .js, so these also ruin the design of the layout.

There are more examples of problem area, but more or less, osCommerce is usable without the need for javascript.

In Magento (shopping cart software), they show a message somewhat like this:

You can see a similar message by visiting any of the shops listed on the Magento Showcase, and turning .js off in your browser.

Adding a similar message to osCommerce…

Step 1. Create an icon.

The icon should be .png format and about 30 x 30 pixels or something like that. Fairly small. Save it into /images/icons/ as filename: no-script.png

Step 2. Add the text for the message

Open up /includes/languages/english.php. Add the following code (open the page, then copy/paste): http://pastebin.com/K6kzqwWV

Repeat for all languages in your shop.

Step 3. Add the code to the header of your site

Open up /includes/template_top.php. Add the following (open the page, then copy/paste);

http://pastebin.com/RYXAgf9e

Step 4. Applying style to the message.

Open up stylesheet.css. Add the following (open the page, then copy/paste): http://pastebin.com/9w0sY8Jb

That is all.

If done correctly, you should see a message like this;

But remember – the message only shows if javascript is turned off in the browser.

Open a different box by default in Admin

Really short post as this is so simple.

When you open the osCommerce admin, the “configuration” box is open by default. This is because it is at the top of the boxes list. If you place a different box on the top of the list, that one will be open by default instead.

Easy as 123. To do this, open up admin/includes/column_left.php and move the line of code that displays the box up to the top. Eg, move

include(DIR_WS_BOXES . ‘customers.php’);

to right underneath

$cl_box_groups = array();

But…what if you want to keep the box order as is (NOT move any lines of code), and just have the customers box open as default?

Again, easy as 123. Count from ZERO until you get to the box you want open. Eg, in a standard install of osCommerce, the customers box is number 3. Open up admin/includes/column_left.php and add the following code;

,
active: 3

right after;

icons: {
‘header’: ‘ui-icon-plus’,
‘headerSelected’: ‘ui-icon-minus’
}

Now the boxes are as they were, but the customers box will be open by default instead of the configuration box.