Post by JasonC » Sat Nov 05, 2011 2:49 am

The majority of sales on my site will be single item so I have pieced together a button that will add the product and open the cart.

Code: Select all

<div class="cart"><a onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" href="index.php?route=checkout/cart" ><span><?php echo $button_cart; ?></span></a></div>
Most of the time it works, but on occasion the shopping cart will be empty.
This is from the error log when the button does not work correctly.

2011-10-26 19:51:16 - PHP Notice: Undefined variable: checkout in
/home/content/mystore/catalog/view/theme/POS/template/product/category.tpl on line 65

I am using 1.5.1


Thanks!
Last edited by JasonC on Thu Nov 10, 2011 4:15 am, edited 1 time in total.

Newbie

Posts

Joined
Tue Nov 16, 2010 1:49 am

Post by Xsecrets » Sat Nov 05, 2011 5:17 am

that doesn't seem like a great way of doing things expecting an ajax onclick event to fire before the actual click event. I could see that failing quite often.

try making your button with something like this

Code: Select all

<form action="index.php?route=checkout/cart" method="POST">
        <input type="hidden" name="product_id" value="65" />
        <input type="hidden" name="quantity" value="1" />
        <input type="submit" value="Add to Cart" name="Add to Cart" />
</form>
of course the product id would need to be dynamic to match your product id this is just an example.

of course you could always use a input type image for the button, or a link with an onclick submit. Doing it this way does not count on the ajax firing first everything happens in one pass.

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by JasonC » Thu Nov 10, 2011 4:13 am

Works great.
Thank you!

Newbie

Posts

Joined
Tue Nov 16, 2010 1:49 am

Post by harry729 » Wed Mar 07, 2012 7:05 pm

Xsecrets wrote: of course the product id would need to be dynamic to match your product id this is just an example.
Guys, what does it mean? I'm not an expert in php... Could you explain more, please. How exactly should the code look like? Besides, how to make a product's adding to cart and then automatically redirect not to cart page, but directly to 'checkout'? This is what I really consider a button "Buy now".

--------------------
OC 1.5.1.3 user.

Newbie

Posts

Joined
Sun Dec 25, 2011 3:21 am
Who is online

Users browsing this forum: No registered users and 18 guests