Sunday, July 26, 2009

Prevent customer checkout - Oscmax

I've seen contributions that attempt to prevent checkout by adding some code to the "Checkout" button in the shopping cart page. This is good, but a customer can bypass this simply by clicking on a "Checkout" link anywhere else on the site - for example in the header if they are logged in.

Rather than modifying every single checkout link, a more effective way is to add some code in checkout_shipping.php - since this page is the first checkout stage for all users, signed in or not. Another proof of reliability: oscommerce coders themselves use this page to verify that a customer is logged in, sending them to the login page otherwise.

So how to add a condition? Say that using the distinct product count function, you want to prevent users from checking out if they added less than 5 distinct products to their cart...

Open catalog/checkout_shipping.php

Around line 35, find:
"if (!tep_session_is_registered('customer_id'))"

This redirects a customer to the login page and so we want our function, which will redirect to the shopping cart page, to be processed before that (i.e. before checking even if the customer is logged in, make sure they added enough items to the cart - otherwise don't bring them to login).

Before this add the following:

// check that required number of products has been met
if ($cart->count_contents_distinct() > 5) {
tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
}

Of course you should have the function in place or you'll get errors all over. For a how-to on that, check out this post.

Alternatively, you can substitute a variety of other conditions in there: show_total, count_contents - basically any function from catalog/includes/classes/shopping_cart.php and others.

The numbers are easily configurable as well - just substitute a variable like MIN_PRODUCTS and then define it in the corresponding language file (a more detailed tutorial to follow...)

1 comment: