Only 1 product at a time in osCommerce
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!
Over at the official osCommerce forum, someone asked about the possibility of only allowing a buyer to select 1 of each product. My answer was to amend the display of the shopping cart and the shopping cart class...
It's fairly easy. What I am going to show you is amending the display using HTML and CSS, then a bit of PHP code to only add 1 of a product in the class file.
Step 1: Make the quantity box readonly and look nicer.
As someone who has done a lot of HTML over the years I know that the attribute called "readonly" makes an input "read only"! So I add this to the shopping cart page. Open up shopping_cart.php and find
-
'text' => tep_draw_input_field('cart_quantity[]', $products[$i]['quantity'], 'size="4"') . tep_draw_hidden_field('products_id[]', $products[$i]['id']));
Change it to:
-
'text' => tep_draw_input_field('cart_quantity[]', $products[$i]['quantity'], 'size="4" id="clubosc" readonly="readonly"') . tep_draw_hidden_field('products_id[]', $products[$i]['id']));
You can see that I have added the readonly attribute and given it a css ID called "clubosc".
Step 2: Amend the look of the box using .css
Open up stylesheet.css and add this code;
-
#clubosc {
-
border: none;
-
text-align: center;
-
background: transparent;
-
}
Add something to the cart and your quantity box now looks like this:

Whereas the usual look is like this:

Now, because we have made the contents of the quantity field readonly, we have to ensure that not more than 1 product can be added. At the moment a buyer could return to the product page, and click "buy" again, ending up with 2 of the same item in his cart.
Step 3: Making only 1 buyable...
Open up /includes/classes/shopping_cart.php and find this line of code:
-
$this->update_quantity($products_id_string, $qty, $attributes);
-
} else {
Simply change both $qty to (int)1. So you end up with;
-
$this->update_quantity($products_id_string, (int)1, $attributes);
-
} else {
And that is it. 3 easy steps to allow customers to only buy a maximum of 1 of each product.


