About Club osCommerce

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

StumbleUpon It! DIGG It!

Only 1 product at a time in osCommerce

Written By Gary on Jan 25 2010 · Comments (0)

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

PHP:
  1. 'text' => tep_draw_input_field('cart_quantity[]', $products[$i]['quantity'], 'size="4"') . tep_draw_hidden_field('products_id[]', $products[$i]['id']));

Change it to:

PHP:
  1. '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;

PHP:
  1. #clubosc {
  2. border: none;
  3. text-align: center;
  4. background: transparent;
  5. }

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:
  1. $this->update_quantity($products_id_string, $qty, $attributes);
  2.           } else {
  3.             $this->contents[$products_id_string] = array('qty' => (int)$qty);

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

PHP:
  1. $this->update_quantity($products_id_string, (int)1, $attributes);
  2.           } else {
  3.             $this->contents[$products_id_string] = array('qty' => (int)1);

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

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!