< Day Day Up > |
Indicate whether the products on your web site are in stock using up-to-date inventory data maintained by some add-ons to your IPN processing script . Merchants who sell tangible goods typically don't have an unlimited supply of any item. When you sell out of something, you might no longer want it to appear on your web site: you can't sell what you don't have. Managing inventory counts for each order and updating your web pages accordingly can be a time-consuming and tedious process, but it can be mostly automated with PayPal's IPN system. This hack consists of a database table, tblProducts , that holds our inventory count, an IPN processing page that manages the count, a web page that displays an out-of-stock message when appropriate, and an email notification to alert you when the inventory count for a particular item is running low (or has been depleted). 7.18.1 Updating the Inventory CountCreate a database table, tblProducts , that contains fields for the product's unique item number, item_number , and the initial inventory count, count_inventory , as shown in Table 7-2. Table 7-2. A database table to manage your store inventory
When a payment is made, PayPal will post the transaction details to your IPN processing page. Included in these details is the unique item number, for which you'll need to query your database for the in-stock inventory. Finally, decrement the value by the number of products purchased: Dim item_number Dim count_inventory_new item_number = Request.Form("item_number") quantity = Request.Form("quantity") 'Retrieve the current inventory count from the database 'Connect to database and create recordset connStore = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="C:/InetPub/wwwroot/database/dbPayPal.mdb") set rsInventoryCount = Server.CreateObject("ADODB.Recordset") rsInventoryCount.ActiveConnection = connStore rsInventoryCount.Source = "SELECT count_inventory FROM tblProducts WHERE item_number = " & item_number rsInventoryCount.Open( ) count_inventory_new = rsInventoryCount("count_inventory") - quantity 'Store the reduced inventory count in the database set cInsPayment = Server.CreateObject("ADODB.Command") cInsPayment.ActiveConnection = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ="C:/InetPub/wwwroot/database/dbPayPal.mdb") cInsPayment.CommandText = "UPDATE tblProducts SET count_inventory = " & count_inventory_new & " WHERE item_number = " & item_number & "" cInsPayment.CommandType = 1 cInsPayment.CommandTimeout = 0 cInsPayment.Prepared = true cInsPayment.Execute( ) This code only handles the inventory count; see [Hack #65] for the complete code necessary to implement IPN. 7.18.2 Creating the Selling PageAn inventory count will not do much good if the web store allows people to purchase items that are no longer available. You can remove the Buy Now button for an out-of-stock item with a simple conditional statement on a dynamic page. Start by placing the current inventory count into the rsInventoryCount variable, using a SQL statement something like this: SELECT count_inventory FROM tblProducts WHERE item_number = 'Wid-001'
Next, compare that value to zero, and display the button only if the item is available: <% If rsInventoryCount("count_inventory") > 0 Then 'We have it in stock, display PayPal purchase button %> <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="business@paypalhacks.com"> <input type="hidden" name="item_name" value="<%=rsProduct("item_name")%>"> <input type="hidden" name="item_number" value="<%=rsProduct("item_number")%>"> <input type="hidden" name="amount" value="<%=rsProduct("item_price")%>"> <input type="hidden" name="no_note" value="1"> <input type="hidden" name="currency_code" value="USD"> <input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit"> </form> <% Else 'We do not have any left, show OoS message %> %> We're sorry, this item is out of stock. <% End If %>
7.18.3 Alerting Yourself if Inventory Is LowFinally, set up a script to email yourself or let your staff know when inventory is low or has become depleted. Insert this code into your IPN processing script: If count_inventory_new < 5 Then 'Low count, send email Dim InvCDO Set InvCDO = Server.CreateObject("CDONTS.NewMail") InvCDO.From = " sales@paypalhacks.com " InvCDO.To =" sales@paypalhacks.com " InvCDO.Subject = "Order More Inventory" InvCDO.Body = "We need to order more of item # " & item_number InvCDO.Send( ) Set InvCDO = Nothing End If If, immediately after a purchase, you have fewer than five of the item left in your inventory, you'll get an email that contains a warning, along with the product's item_number .
|
< Day Day Up > |