Cartweaver.com

 facebook Facebook
 twitter Twitter

Blog Calendar

S M T W T F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
<<  September  >>
2010

Blog search

Cartweaver.com ColdFusion and PHP
Shopping Carts For Adobe Dreamweaver.

Adobe Community Pro

Bookmark and Share

 

Tom MuckTom Muck's blog

Removing items from cart that are sold (PHP)

Saturday, October 03, 2009 9:13:18 AM

One of the frequent questions I get doing support for Cartweaver is "How do I make sure that all the items in a customer's cart are still available?" What happens is that a customer will place an item in the cart, and then leave the site. Another customer comes along and buys the product. The first customer comes back and tries to purchase the item, but cannot. Unfortunately, the customer doesn't find out until the order information is entered.

The following modification for the PHP version will remove items from the cart that are no longer available, and show the customer a message that an item has been removed from the cart because it is no longer available.

In CWIncFunctions.php, add the following section of code at line 470, right after this line:

$Cart["Products"] = array();

Add the following code:

if(!$cartweaver->settings->allowBackOrders) {
  $query_rsCWCheckStock = sprintf("SELECT
  s.SKU_Stock,
  c.cart_Line_ID
  FROM tbl_cart c
  INNER JOIN tbl_skus s
  ON c.cart_sku_ID = s.SKU_ID
  WHERE c.cart_custcart_ID = '%s'
  AND s.SKU_Stock < c.cart_sku_qty",$cartID);
  $rsCWCheckStock = $cartweaver->db->executeQuery($query_rsCWCheckStock, "rsCWCheckStock");
  $rsCWCheckStock_recordCount = $cartweaver->db->recordCount;
  $row_rsCWCheckStock = $cartweaver->db->db_fetch_assoc($rsCWCheckStock);
  if($rsCWCheckStock_recordCount > 0) {
    $cartweaver->setCWError("Item Sold", "An item or items in your cart are no longer available or had quantities adjusted");
    do {
      if($row_rsCWCheckStock["SKU_Stock"] <= "0") {
        $query_rsCW = sprintf("DELETE FROM tbl_cart
        WHERE cart_Line_ID = %d",$row_rsCWCheckStock["cart_Line_ID"]);
        $rsCW = $cartweaver->db->executeQuery($query_rsCW, "rsCW");
      } else {
        $query_rsCW = sprintf("UPDATE tbl_cart SET cart_sku_qty = %d
        WHERE cart_Line_ID = %d",$row_rsCWCheckStock["SKU_Stock"], $row_rsCWCheckStock["cart_Line_ID"]);
        $rsCW = $cartweaver->db->executeQuery($query_rsCW, "rsCW");
      }
    } while ($row_rsCWCheckStock = $cartweaver->db->db_fetch_assoc($rsCWCheckStock));
  }
}

Now, when a user comes back to view his cart, any items that are no longer available are removed from the cart and a message is displayed to the customer. This only affects carts where Allow Backorders is unchecked in the Cartweaver admin.

Update: 10/4/2009 Thanks to Alex in the CW support newsgroup for pointing out that the method does not consider quantities in the cart greater than available quantities. It was written for one situation for a customer, but I have now adjusted the code to work with all situations where a quantity is no longer available for a given item.

Category tags: Cartweaver, FAQ, PHP

Before posting comments or trackbacks, please read the posting policy.

Full Blog Calendar