Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Add to cart/buy now button


kittycat01

Recommended Posts

Hi there!

 

I've added some HTML to my checkout payment page with images of products I have on offer, and an "add to cart" image button underneath.

 

Now, all I want is the html link that would add the desired product to the cart. I've tried using the buy_now function so many different ways and I just get taken to an empty cart :( (Or should I say, any items I already put in there manually show, but the desired product doesn't get added)

 

I've tried this:

www.webstore.com/apples-p-28.html?products_id=28&action=buy_now

 

and this

www.webstore.com/product_info.php?products_id=28&action=buy_now

 

this

www.webstore.com/shopping_cart.php?action=buy_now&products_id=28&id=&cart_quantity=1

 

and finally this

www.webstore.com/index.php?action=buy_now&products_id=28

 

None of those work, they just take me to the empty cart and it's driving me crazy! I would just simply like to know the way to add a product to the cart via a button I've created. Could somebody please explain to me what I'm doing wrong? I've been reading forum posts all day and I've tried everything I've seen, but it just seems like the buy_now function doesn't... function!?

 

Am I being daft in presuming I can have a html link that will add an item to the cart? The product I want to add doesn't have any attributes to choose from, so I know it's not that.

 

Thanks in advance,

Kitty :D

Link to comment
Share on other sites

You can not use an HTML link to add to the cart. You must use the PHP tep_href_link() function to preserve the session.

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

It would look something like this:

 

tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now&products_id=' . $products_id]))

 

Note that $products_id has to be defined somewhere above this line.

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

Hi Jim!

 

Hmm, think I might be in a little over my head now! No idea how to define a product as you mentioned... I've been looking around other files to see how it might work, but I'm lost...

 

Thank you anyway, Kitty :)

Link to comment
Share on other sites

Hi, maybe i'm being a little simple here, but if its just you want your button to look different then can you not just rename YOUR button pic the same as the default one, then overwrite it it with your own (or delete the file and re-upload yours with the same name. My button is here:

includes/languages/english/images/buttons/button_buy_now.gif

Running a botched up version of  osCommerce Online Merchant v2.3.4 bootstrap with the dresscode theme installed, numerous add-ons, terrible coding, terrible website, but will have to make do until I have made up for my losses and can risk shutting down for a couple of weeks while I start all over again. - I did not install my program but am endeavouring to fix it with your help.

Link to comment
Share on other sites

Hi Kellie!

 

It's not that I want the buy now button to look different, but the buy_now code doesn't seem to work on the site... I don't understand why :(

I'd just like the button I've created to add an item to the cart, and if the buy_now function works like that normally, I don't understand why I need extra script to make it happen?

I'm confused! D:

 

I've basically added some html to the checkout page, that includes a picture of the item I want to sell at checkout and a button - I just want the customer to be able to press this button then the item be added to the cart and then they can continue to checkout from the cart...

 

I'm very interested in learning how everything works on oscommerce, and I'm willing to implement things myself, I just need help in understanding exactly what it is that I'm doing!

Thanks for replying,

Kitty :)

 

 

Hi, maybe i'm being a little simple here, but if its just you want your button to look different then can you not just rename YOUR button pic the same as the default one, then overwrite it it with your own (or delete the file and re-upload yours with the same name. My button is here:

includes/languages/english/images/buttons/button_buy_now.gif

Link to comment
Share on other sites

HTML code is stateless. That means that every page exists by itself, independent of all other pages. The page neither knows nor cares who accesses it.

 

This doesn't work for a shopping cart. The cart needs to deliver its contents to the same person every time. To do this, the cart needs to maintain state all the way through the checkout process. This means that we need to add session controls on top of the normal stateless HTML protocols. This can be accomplished either through the use of a cookie or by adding a Session ID to the URL string.

 

osCommerce accomplishes this session control via a set of PHP scripts. Using the tep_href_link() function allows these scripts to pass the session information along to the next page, insuring that the customer retains their cart throughout their visit. Using a plain HTML link anywhere in the middle of that process breaks the session and causes the customer to lose their cart.

 

To make the code I posted above work, you need to give it the products_id that defines that particular product. You can find that ID in the URL when you visit the product page. Then just replace $products_id with that number, or add

 

$products_id = 123;

 

somewhere above that code (if 123 is the products_id). Of course osCommerce is designed to pull the product information from a database, so you'll have to maintain this mess of HTML manually. But that's your choice.

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

If the 'checkout payment' page is shopping_cart.php then we can add this button

 

<form action="shopping_cart.php?action=add_product" method="post">
<input type="hidden" name="products_id" value="16">
<input type="submit" value="Add To Cart">
</form>

 

It goes immediately after the main form, a dozen lines from the end of the file

 

<?php
 reset($initialize_checkout_methods);
 while (list(, $value) = each($initialize_checkout_methods)) {
?>
<p align="right"><?php echo $value; ?></p>
<?php
 }
}
?>
</div>
</form>
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< IT GOES IN HERE
<?php
} else {
?>
<div class="contentContainer">
<div class="contentText">
<?php echo TEXT_CART_EMPTY; ?>
<p align="right"><?php echo tep_draw_button(IMAGE_BUTTON_CONTINUE, 'triangle-1-e', tep_href_link(FILENAME_DEFAULT)); ?></p>
</div>
</div>
<?php
}
require(DIR_WS_INCLUDES . 'template_bottom.php');
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>

 

Product 16 is a DVD called Courage Under Fire, in the case of the dummy products that come with osC.

Link to comment
Share on other sites

@@Chris H Thanks you for getting back to me!

 

OK, I know I must be trying everyone's patience right now - I added the button where you suggested, and it seemed to refresh the cart yet not add anything to it (I changed the id to the product id I want to sell).

 

I actually want the button on the checkout_payment page, so I put it there also, and this just sends me to the checkout_confirmation page when I click it, without adding the extra item to the cart? D: I am using v2.3.1 - I wonder if this is something to do with it not working?

 

@@kymation Thank you also for getting back to me!

Thank you for explaining the process to me further, I've been using Oscommerce for a long time, and I basically do what proper coders would hate, in that I look at code on other pages and twiddle with it until I get it to do what I want it to (!)... but I can't write this stuff from scratch, because I don't quite understand what it all means, I don't understand the structure of PHP.

With the tep_href_link code and the product_id code - how do I "attach" this to a button? I've tried looking at the product_info page to see how it might all fit together but I just can't figure it out.

Is it to do with CSS - like the button is created using CSS?

 

 

For example, this is what the code for the add to cart button looks like on the product_info page:

<div>
<?php echo '<span style="font-size:14px; color:#141414; margin-right:5px;">'.TEXT_QTY.'</span>'.tep_draw_input_field('products_quantity', '1', 'size=2 style="border:1px solid #ccc; padding: 3px 5px;"'); ?> <?php echo tep_draw_hidden_field('products_id', $product_info['products_id']); ?>
<div style="margin-top:10px; margin-bottom:50px"><input type="submit" class="orangebutton" value="Add to basket"></div>
</div>

 

With my meagre brain, I can see this code says add a box to put item quantity in, explains the colours/design used, then it is asking the php to look for the products id, I think, judging by the code @@Chris H gave me, it then creates a button that says "add to basket" on it. The class bit "orangebutton" will access the CSS to "design" the look of the button. The input type bit - "submit" - I don't understand how that is defined, does it look to the CSS or another external file to determine what "submit" is?

 

Can you see how I try and figure everything out now?! Daft, I know!!!

 

If everyone has had enough of trying to explain this to me, don't worry.

 

Thank you again everyone for your time and patience!

Kitty :)

Link to comment
Share on other sites

There are two different button types in osCommerce. They may look the same, and even act the same, but they do it in very different ways.

 

The first type is a form submit button. That is what you have in your example. It sends the contents of an HTML form. In this case, it sends the contents of the products_quantity field (which is a text box that the customer can fill in) and the products_id field (which is hidden from the customer.) This information is sent to the location given in the form tag (which you did not show.) The form tag uses the tep_href_link() function to specify the location, and that function preserves the session so we don't lose the cart. Adding a second button of this type inside the same form will just act a a second submit button, resulting in a duplicate of the existing button. This is not what you say you want.

 

The second type is a link button. It doesn't submit any form data, so the customer can't fill in any boxes etc. It acts just like an HTML link. The code for that looks like this:

 

<?php echo tep_draw_button(IMAGE_BUTTON_CONTINUE, 'triangle-1-e', tep_href_link( $PHP_SELF, tep_get_all_get_params(array('action')) . 'action=buy_now&products_id=' . 123])
)); ?>

 

This is using the osCommerce 2.3x functions to draw the button. You seem to be using old HTML code for that, so your code may look a bit different. Note the use of the tep_href_link() function to generate the link, and the lack of any form tags. This is just a link disguised as a button.

 

osCommerce draws the buttons using Javascript and CSS to control the appearance. You can do this in other ways, including using an image for the button, but it's usually best to keep all of the buttons looking the same.

 

Off topic a bit, but you say that you are putting this on the checkout_payment page. The use of a link button on that page will send the customer back to the checkout shipping page or to the shopping cart page, depending on how you have your cart set up. Having to start the checkout process over again may irritate your customers. I suggest putting this on the shopping cart page so that the product can be added before the checkout process starts. I have done this on other carts using the cross sell addon, and it's been quite effective.

 

No, I don't mind teaching, as long as I have the time. If I don't answer, that just means I'm too busy.

 

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

Oh sorry, I see what you mean now. Yes I was being simple. You say you are adding items to the 'check-out' page, so I presume you are trying to get an impulse sale when the customer was about to finish? Having read all the other answers it seems that it is pretty complicated and wondering would it not be easier to install the modular front page add on, then set the sliding banner to show all your 'offers/specials'. Alternatively what about the dangling carrot add on? Neither would actually be focusing on selling when your customers are at check-out, but rather while they are still in shopping mode, which to me seems more sensible anyhow. Personally, once I get to checkout i'm fed up of browsing and just what to be done!

Running a botched up version of  osCommerce Online Merchant v2.3.4 bootstrap with the dresscode theme installed, numerous add-ons, terrible coding, terrible website, but will have to make do until I have made up for my losses and can risk shutting down for a couple of weeks while I start all over again. - I did not install my program but am endeavouring to fix it with your help.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...