Clear Cart, then add a product automatically

Frank asks;

I’m trying to create a page that when someone visits; automatically empties the shopping cart and then adds one specific product, so the cart will only have that one item whenever the customer loads that page.

I thought this was an intelligent and interesting question – and as no-one else was helping, I decided to post it here in the blog rather than in the osCommerce Forum.

The Solution

My idea for this is to create a standalone page which you can advise your customers of – this page will simply do the following;

  • Clear the Cart Contents
  • Add a product to the Cart
  • Redirect to the Shopping Cart Page

That’s fairly straightforward and should just about cover what is needed.

How To Do It

1. Create your product – this will simply be the product that you want added to the Cart. Easy enough. Make a note of the Product ID – as we’ll need this in our “adding” code!

2. Create a new blank file called “addproduct.php” and save it into the same directory as your normal base files (index.php, product_info.php and all the rest). You can name it whatever you want really.

3. Add the following code to the file you created in Step 2:

[php]remove_all();
$cart->add_cart(28);
tep_redirect(tep_href_link(FILENAME_SHOPPING_CART, ”, ‘SSL’));
?>[/php]

In this example, the Product ID is “28” – you can see this in the second $cart line. Amend this number to whatever Product ID you are going to use.

4. Save the file. Visit in your browser and you will end up at your site Shopping Cart, with nothing other than Product ID 28 (or whatever ID you used!) in the Cart.

Easy as 123. Can I get a “hell yeah”?

What if your product has “options” (aka attributes)?

That’s a bit more problematic, but not overly so. What you will need to do is create your Product, with all it’s options. Then visit the product and add it to your Cart (making sure you selected the options that you wish your customers to automatically add) and view the resulting URL to get the options values… sounds complicated, so;

Let’s illustrate this with an example

In a standard install of osCommerce, Product ID #1 is a Matrox G200 MMS with a couple of options. Let’s say that that you wanted the following to be added to the cart automatically:

A “Matrox G200 MMS” with “Memory of 16Mb” and “Model Premium”. Select these options and press “add to cart” button. This will now show your Shopping Cart with the product and it’s options. Copy the link back to the product, and paste it somewhere – you will see something like this:

http://www.yoursite.com/product_info.php?products_id=1{4}3{3}6

Those numbers in brackets are the options that we selected! So…now you have this, you need to go back to the addproduct.php code and amend it slightly, like this:

[php]remove_all();
$cart->add_cart(1, 1, array(4 => 3, 3 => 6));
tep_redirect(tep_href_link(FILENAME_SHOPPING_CART, ”, ‘SSL’));
?>[/php]

Look at the difference in the 2nd $cart line – you can see a couple more parameters – they work like this: add_cart(PRODUCT ID, NUMBER OF PRODUCTS TO ADD, OPTIONS)

In the options part, you can plainly see a correlation between the URL we copied: product_info.php?products_id=1{4}3{3}6 and the option value: array(4 => 3, 3 => 6)

So, adding options make things slightly more complicated, but not overly so.

I have no doubt that in practice, this will need tweaking, but at least you now have a start in a way (maybe not even the best way!) to do this. Go to it!

9 Replies to “Clear Cart, then add a product automatically”

  1. I just wanted to say you are a star. I wrote a similar function to this but messed around for ages trying to get the attributes to work. After much frustration I came across your site and the answer that had been eluding me. So thanks very much!

  2. I get this errors:

    Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:\xampp\htdocs\shop2\catalog\test.php:2) in C:\xampp\htdocs\shop2\catalog\includes\functions\sessions.php on line 102

    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at C:\xampp\htdocs\shop2\catalog\test.php:2) in C:\xampp\htdocs\shop2\catalog\includes\functions\sessions.php on line 102

    Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\shop2\catalog\test.php:2) in C:\xampp\htdocs\shop2\catalog\includes\functions\general.php on line 33

    whats the problem?

  3. Gary,
    Thanks for this simple tweak. Starting a new business I have no reviews on our customer service. I created a no cost item in the store called “customer service” and wanted it added to the cart automatically. I only needed to use
    $cart->add_cart(2620);
    in the index.php up at the top to accomplish this.

    Many thanks.

  4. Hi Gary.

    Thank You for posting this. It’s very useful, I even found it is possible to add multiple products.

    I would like to send a customer a link which would add a product to the cart at a discounted price. The discount should not be available to any other customers.

    Is it possible to do this with code similar to this, and could you suggest the best discount contribution to achieve this?

    Thanks

    Ken

  5. Hi Gary, this was a quick fix for me in a time of need, thank you. is it possible to add an inactive item by using the same code, as at the moment i can only get it to work with an active item. can i pass another param?

    thanks in advance,

    Ben

  6. Ben. Mark the product out of stock. Skip stock checking for that product. Or use an addon to “hide” the product from general view…

Leave a Reply

Your email address will not be published. Required fields are marked *