示例#1
0
 public function isEqual(Cart66CartItem $item)
 {
     $isEqual = true;
     if ($this->_productId != $item->getProductId()) {
         $isEqual = false;
     }
     if ($this->_optionInfo != $item->getOptionInfo()) {
         $isEqual = false;
     }
     return $isEqual;
 }
 /**
  * Returns the item that was added (or updated) in the cart
  * 
  * @return Cart66CartItem
  */
 public function addItem($id, $qty = 1, $optionInfo = '', $formEntryId = 0, $productUrl = '', $ajax = false, $suppressHook = false, $optionResult = '')
 {
     Cart66Session::set('Cart66Tax', 0);
     Cart66Session::set('Cart66TaxRate', 0);
     $the_final_item = false;
     $alert = array();
     $options_valid = true;
     $product = new Cart66Product($id);
     do_action('cart66_before_add_to_cart', $product, $qty);
     try {
         $optionInfo = $this->_processOptionInfo($product, $optionInfo);
     } catch (Exception $e) {
         Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Exception due to invalid product option: " . $e->getMessage());
         $options_valid = false;
         $message = __('We could not add the product', 'cart66') . ' <strong>' . $product->name . '</strong> ' . __('to the cart because the product options are invalid.', 'cart66');
         $alert['msg'] = $message;
         $alert['msgId'] = -2;
         $alert['quantityInCart'] = 0;
         $alert['requestedQuantity'] = $qty;
         $alert['productName'] = $product->name;
         $alert['productOptions'] = $optionInfo;
         if (!$ajax) {
             Cart66Session::set('Cart66InvalidOptions', $message);
         }
     }
     if ($product->id > 0 && $options_valid) {
         $newItem = new Cart66CartItem($product->id, $qty, $optionInfo->options, $optionInfo->priceDiff, $productUrl);
         $the_final_item = $newItem;
         if (($product->isSubscription() || $product->isMembershipProduct()) && ($this->hasSubscriptionProducts() || $this->hasMembershipProducts())) {
             // Make sure only one subscription can be added to the cart. Spreedly only allows one subscription per subscriber.
             Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] about to remove membership item");
             $this->removeMembershipProductItem();
         }
         if ($product->isGravityProduct()) {
             Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Product being added is a Gravity Product: {$formEntryId}");
             if ($formEntryId > 0) {
                 $newItem->addFormEntryId($formEntryId);
                 $newItem->setQuantity($qty);
                 $actualQty = Cart66Product::checkInventoryLevelForProduct($product->id, $optionInfo->options);
                 $confirmInventory = Cart66Product::confirmInventory($product->id, $optionInfo->options, $qty);
                 if ($actualQty !== NULL && $actualQty < $qty && !$confirmInventory) {
                     if ($actualQty > 0) {
                         $message = '<div class="alert-message alert-error Cart66Unavailable"><p>' . __('We are not able to fulfill your order for', 'cart66') . ' <strong>' . $qty . '</strong> ' . $newItem->getFullDisplayName() . " " . __('because we only have', 'cart66') . " <strong>{$actualQty} " . __('in stock.', 'cart66') . "</strong></p>" . "<p>" . __('Your cart has been updated based on our available inventory.', 'cart66') . "</p>" . '</div>';
                     } else {
                         $soldOutLabel = Cart66Setting::getValue('label_out_of_stock') ? strtolower(Cart66Setting::getValue('label_out_of_stock')) : __('out of stock', 'cart66');
                         $message = '<div class="alert-message alert-error Cart66Unavailable"><p>' . __('We are not able to fulfill your order for', 'cart66') . ' <strong>' . $qty . '</strong> ' . $newItem->getFullDisplayName() . " " . __('because it is', 'cart66') . " <strong>" . $soldOutLabel . ".</strong></p>";
                         $message .= '</div>';
                     }
                     if (!empty($message)) {
                         Cart66Session::set('Cart66InventoryWarning', $message);
                     }
                 } else {
                     $this->_items[] = $newItem;
                 }
             }
         } else {
             Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Product being added is NOT a Gravity Product");
             $isNew = true;
             $newItem->setQuantity($qty);
             foreach ($this->_items as $item) {
                 if ($item->isEqual($newItem)) {
                     $isNew = false;
                     $newQuantity = $item->getQuantity() + $qty;
                     $actualQty = Cart66Product::checkInventoryLevelForProduct($id, $optionInfo->options);
                     $confirmInventory = Cart66Product::confirmInventory($id, $optionInfo->options, $newQuantity);
                     if ($actualQty !== NULL && $actualQty < $newQuantity && !$confirmInventory) {
                         if ($actualQty > 0) {
                             $message = '<p>' . __('We are not able to fulfill your order for', 'cart66') . ' <strong>' . $qty . '</strong> ' . $item->getFullDisplayName() . " " . __('because we only have', 'cart66') . " <strong>{$actualQty} " . __('in stock.', 'cart66') . "</strong></p>";
                             $message = "{$message} <p>" . __('Your cart has been updated based on our available inventory.', 'cart66') . "</p>";
                             $alert['msg'] = $message;
                             $alert['msgId'] = -1;
                             $alert['quantityInCart'] = $actualQty;
                             $alert['requestedQuantity'] = $qty;
                             $alert['productName'] = $item->getFullDisplayName();
                             $alert['productOptions'] = $optionInfo;
                         } else {
                             $soldOutLabel = Cart66Setting::getValue('label_out_of_stock') ? strtolower(Cart66Setting::getValue('label_out_of_stock')) : __('out of stock', 'cart66');
                             $message = '<p>' . __('We are not able to fulfill your order for', 'cart66') . ' <strong>' . $qty . '</strong> ' . $item->getFullDisplayName() . " " . __('because it is', 'cart66') . " <strong>" . $soldOutLabel . ".</strong></p>";
                             $message = "{$message} <p>" . __('Your cart has been updated based on our available inventory.', 'cart66') . "</p>";
                             $alert['msg'] = $message;
                             $alert['msgId'] = -2;
                             $alert['quantityInCart'] = $actualQty;
                             $alert['requestedQuantity'] = $qty;
                             $alert['productName'] = $item->getFullDisplayName();
                             $alert['productOptions'] = $optionInfo;
                         }
                         if (!empty($message)) {
                             if (!$ajax) {
                                 Cart66Session::set('Cart66InventoryWarning', $message);
                             }
                         }
                         $newQuantity = $actualQty;
                     }
                     $item->setQuantity($newQuantity);
                     if ($formEntryId > 0) {
                         $item->addFormEntryId($formEntryId);
                     }
                     if (empty($alert)) {
                         $message = __('We have successfully added', 'cart66') . " <strong>{$newQuantity}</strong> " . $product->name . " {$optionResult} " . __('to the cart', 'cart66') . ".";
                         $message = "<p>{$message}</p>";
                         $alert['msg'] = $message;
                         $alert['msgId'] = 0;
                         $alert['quantityInCart'] = $newQuantity;
                         $alert['requestedQuantity'] = $qty;
                         $alert['productName'] = $product->name;
                         $alert['productOptions'] = $optionInfo;
                     }
                     $the_final_item = $item;
                     break;
                 }
             }
             if ($isNew) {
                 $newQuantity = $qty;
                 $actualQty = Cart66Product::checkInventoryLevelForProduct($id, $optionInfo->options);
                 $confirmInventory = Cart66Product::confirmInventory($id, $optionInfo->options, $newQuantity);
                 if ($actualQty !== NULL && $actualQty < $newQuantity && !$confirmInventory) {
                     if ($actualQty > 0) {
                         $message = '<p>' . __('We are not able to fulfill your order for', 'cart66') . ' <strong>' . $qty . '</strong> ' . $product->name . " " . __('because we only have', 'cart66') . " <strong>{$actualQty} " . __('in stock.', 'cart66') . "</strong></p>";
                         $message = "{$message} <p>" . __('Your cart has been updated based on our available inventory.', 'cart66') . "</p>";
                         $alert['msg'] = $message;
                         $alert['msgId'] = -1;
                         $alert['quantityInCart'] = $actualQty;
                         $alert['requestedQuantity'] = $qty;
                         $alert['productName'] = $product->name;
                         $alert['productOptions'] = $optionInfo;
                     } else {
                         $soldOutLabel = Cart66Setting::getValue('label_out_of_stock') ? strtolower(Cart66Setting::getValue('label_out_of_stock')) : __('out of stock', 'cart66');
                         $message = '<p>' . __('We are not able to fulfill your order for', 'cart66') . ' <strong>' . $qty . '</strong> ' . $product->name . " " . __('because it is', 'cart66') . " <strong>" . $soldOutLabel . ".</strong></p>";
                         $message = "{$message} <p>" . __('Your cart has been updated based on our available inventory.', 'cart66') . "</p>";
                         $alert['msg'] = $message;
                         $alert['msgId'] = -2;
                         $alert['quantityInCart'] = $actualQty;
                         $alert['requestedQuantity'] = $qty;
                         $alert['productName'] = $product->name;
                         $alert['productOptions'] = $optionInfo;
                     }
                     if (!empty($alert)) {
                         if (!$ajax) {
                             Cart66Session::set('Cart66InventoryWarning', $message);
                         }
                     }
                     $newQuantity = $actualQty;
                 }
                 $newItem->setQuantity($newQuantity);
                 if ($formEntryId > 0) {
                     $newItem->addFormEntryId($formEntryId);
                 }
                 if (empty($alert)) {
                     $message = __('We have successfully added', 'cart66') . " <strong>{$newQuantity}</strong> " . $product->name . " {$optionResult} " . __('to the cart', 'cart66') . ".";
                     $message = "<p>{$message}</p>";
                     $alert['msg'] = $message;
                     $alert['msgId'] = 0;
                     $alert['quantityInCart'] = $newQuantity;
                     $alert['requestedQuantity'] = $qty;
                     $alert['productName'] = $product->name;
                     $alert['productOptions'] = $optionInfo;
                 }
                 $the_final_item = $newItem;
                 $this->_items[] = $newItem;
             }
         }
         $this->_setPromoFromPost();
         Cart66Session::touch();
         if (!$suppressHook) {
             do_action('cart66_after_add_to_cart', $product, $qty);
         }
         if ($ajax) {
             return $alert;
         }
         return $the_final_item;
     }
 }