Lightroom 3 is out »

Add to Cart Button and Link in ZenCart

In order to perform advanced customization on ZenCart add to cart functionality, you have to understand how add to cart button work.  Take the simple case where you want to have the user click a button and then a product gets added to the shopping cart and the user lands into the shopping cart page.

Add to Cart Button in ZenCart

The HTML output of the form containing the button and quantity field would look something like this....


<form name="cart_quantity" action="http://www.example.com/index.php?main_page=index&amp;cPath=65&amp;action=add_product" method="post" enctype="multipart/form-data">
<input name="cart_quantity" value="1" maxlength="6" size="4" type="text">
<input name="products_id" value="186" type="hidden">
<input image="yourimage.png" type="submit" >
</form>


Note the unique product id in the hidden field.  You can make cart_quantity hidden if you do not want to show the quantity input quantity and just add quantity one.

The PHP that generates the above HTML output would be something like ...


$lc_button= zen_draw_form('cart_quantity', zen_href_link(zen_get_info_page($listing->fields['products_id']), zen_get_all_get_params(array('action')) . 'action=add_product&products_id=' . $listing->fields['products_id']), 'post', 'enctype="multipart/form-data"') . '<input type="text" name="cart_quantity" value="' . (zen_get_buy_now_qty($listing->fields['products_id'])) . '" maxlength="6" size="4" /><br />' . zen_draw_hidden_field('products_id', $listing->fields['products_id']) . zen_image_submit(BUTTON_IMAGE_IN_CART, BUTTON_IN_CART_ALT) . '</form>';


as copied from includes/modules/product_listing.php

Here is a related ZenCart thread on how to construct the "Add to Cart" button for a single product.

Add To Cart Link in ZenCart

Also, if you have a product with no attributes, you can also have that added to the cart via an URL as described in this thread.