/** * Add an item to the shopping cart when an Add To Cart button is clicked. * Combine the product options, check inventory, and add the item to the shopping cart. * If the inventory check fails redirect the user back to the referring page. * This function assumes that a form post triggered the call. */ public function addToCart($ajax = false) { $itemId = Cart66Common::postVal('cart66ItemId'); Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Adding item to cart: {$itemId}"); $options = ''; $optionResult = ''; if (isset($_POST['options_1'])) { $options = Cart66Common::postVal('options_1'); $optionResult = Cart66Common::postVal('options_1'); } if (isset($_POST['options_2'])) { $options .= '~' . Cart66Common::postVal('options_2'); $optionResult .= ', ' . Cart66Common::postVal('options_2'); } $optionResult = $optionResult != null ? '(' . $optionResult . ') ' : ''; if (isset($_POST['item_quantity'])) { $itemQuantity = $_POST['item_quantity'] > 0 ? round($_POST['item_quantity'], 0) : 1; } else { $itemQuantity = 1; } if (isset($_POST['item_user_price'])) { $userPrice = $_POST['item_user_price'] === 0 || $_POST['item_user_price'] == '0' ? '0' . Cart66Setting::getValue('currency_dec_point') . '00' : $_POST['item_user_price']; $sanitizedPrice = Cart66Common::cleanNumber($userPrice); $sanitizedPrice = $sanitizedPrice === 0 || $sanitizedPrice == '0' ? '0.00' : $sanitizedPrice; Cart66Session::set("userPrice_{$itemId}", $sanitizedPrice); } $productUrl = null; if (isset($_POST['product_url'])) { $productUrl = $_POST['product_url']; } if (Cart66Product::confirmInventory($itemId, $options)) { if ($ajax) { Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] yes, ajax here"); Cart66Session::set('Cart66LastPage', $productUrl); return $this->addItem($itemId, $itemQuantity, $options, null, $productUrl, $ajax, false, $optionResult); } $this->addItem($itemId, $itemQuantity, $options, null, $productUrl); } else { Cart66Common::log("Item not added due to inventory failure"); wp_redirect($_SERVER['HTTP_REFERER']); exit; } //$this->_setAutoPromoFromPost(); $this->_setPromoFromPost(); }