Hack 46 Create Shopping Cart Links

figs/beginner.gif figs/hack46.gif

Convert Shopping Cart button code to single-line URLs that can be emailed or linked to images.

Although you can create Shopping Cart buttons [Hack #45] at the PayPal web site, you can also create buttons off-site. This gives web page designers more flexibility and gives programmers the ability to create buttons dynamically with programming code. One of the simplest and most flexible approaches involves creating URLs instead of HTML forms.

5.3.1 The Code

The HTML code for a simple Add to Cart button looks like this:

<form method="post" action="https://www.paypal.com/cgi-bin/webscr"


<input type="hidden" name="cmd" value="_cart">

<input type="hidden" name="business" value="pay@paypalhacks.com">

<input type="hidden" name="item_name" value="PayPal Hacks">

<input type="hidden" name="amount" value="24.95">

<input type="submit" name="add" value="Add to Cart">


The equivalent button in the form of an Add to Cart hyperlink looks like this:

<a href=https://www.paypal.com/cgi-bin/webscr?cmd=_cart&add=1&business=


                target="paypal">Add to Cart</a>

This link opens a window and displays the PayPal Shopping Cart with one item in it: PayPal Hacks for $24.95.

In both examples, note the presence of the important target="paypal" attribute, which causes the Shopping Cart to open in a new browser window. Without it, the cart will not display a Continue Shopping button. Always include this attribute in your Add to Cart buttons and also make sure paypal is all in lowercase.

5.3.2 Shortening the Link

Many PayPal URLs can be shortened, which can be useful (and sometimes necessary) when sending links in emails, because it prevents them from getting cut at the end of a line. The short link for the Shopping Cart begins with https://www.paypal.com/cart/. Just append all the fields you want to use to the end, as in this payment link:


This works for displaying the Shopping Cart as well:


?Patrick Breitenbach