Only 1 product at a time in osCommerce

By | January 25, 2010

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

[php]’text’ => tep_draw_input_field(‘cart_quantity[]’, $products[$i][‘quantity’], ‘size=”4″‘) . tep_draw_hidden_field(‘products_id[]’, $products[$i][‘id’]));[/php]

Change it to:

[php]’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’]));[/php]

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;

[php]#clubosc {
border: none;
text-align: center;
background: transparent;
}[/php]

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:

[php]$this->update_quantity($products_id_string, $qty, $attributes);
} else {
$this->contents[$products_id_string] = array(‘qty’ => (int)$qty);[/php]

Simply change both $qty to (int)1. So you end up with;

[php]$this->update_quantity($products_id_string, (int)1, $attributes);
} else {
$this->contents[$products_id_string] = array(‘qty’ => (int)1);[/php]

And that is it. 3 easy steps to allow customers to only buy a maximum of 1 of each product.

Leave a Reply

Your email address will not be published.