Example #1
0
 public function ajaxProcessUpdateQty()
 {
     if ($this->tabAccess['edit'] === '1') {
         $errors = array();
         if (!$this->context->cart->id) {
             return;
         }
         if ($this->context->cart->OrderExists()) {
             $errors[] = Tools::displayError('An order has already been placed with this cart.');
         } elseif (!($id_product = (int) Tools::getValue('id_product')) || !($product = new Product((int) $id_product, true, $this->context->language->id))) {
             $errors[] = Tools::displayError('Invalid product');
         } elseif (!($qty = Tools::getValue('qty')) || $qty == 0) {
             $errors[] = Tools::displayError('Invalid quantity');
         }
         // Don't try to use a product if not instanciated before due to errors
         if (isset($product) && $product->id) {
             if (($id_product_attribute = Tools::getValue('id_product_attribute')) != 0) {
                 if (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty((int) $id_product_attribute, (int) $qty)) {
                     $errors[] = Tools::displayError('There is not enough product in stock.');
                 }
             } else {
                 if (!$product->checkQty((int) $qty)) {
                     $errors[] = Tools::displayError('There is not enough product in stock.');
                 }
             }
             if (!($id_customization = (int) Tools::getValue('id_customization', 0)) && !$product->hasAllRequiredCustomizableFields()) {
                 $errors[] = Tools::displayError('Please fill in all the required fields.');
             }
             $this->context->cart->save();
         } else {
             $errors[] = Tools::displayError('This product cannot be added to the cart.');
         }
         if (!count($errors)) {
             if ((int) $qty < 0) {
                 $qty = str_replace('-', '', $qty);
                 $operator = 'down';
             } else {
                 $operator = 'up';
             }
             if (!($qty_upd = $this->context->cart->updateQty($qty, $id_product, (int) $id_product_attribute, (int) $id_customization, $operator))) {
                 $errors[] = Tools::displayError('You already have the maximum quantity available for this product.');
             } elseif ($qty_upd < 0) {
                 $minimal_qty = $id_product_attribute ? Attribute::getAttributeMinimalQty((int) $id_product_attribute) : $product->minimal_quantity;
                 $errors[] = sprintf(Tools::displayError('You must add a minimum quantity of %d', false), $minimal_qty);
             }
         }
         echo Tools::jsonEncode(array_merge($this->ajaxReturnVars(), array('errors' => $errors)));
     }
 }
 /**
  * @param \Product $product
  *
  * @return string
  * @author Panagiotis Vagenas <*****@*****.**>
  * @since 150213
  */
 protected function isInStock(\Product &$product)
 {
     return $product->checkQty(1) || $this->backOrdersAllowed($product) ? 'Y' : 'N';
 }
Example #3
0
 /**
  * Checks the availability of the product and returns the "availability constant".
  *
  * The product is considered available if it is visible in the shop and is in stock.
  *
  * @param Product $product the product model.
  * @return string the value, i.e. self::IN_STOCK or self::OUT_OF_STOCK.
  */
 protected function checkAvailability(Product $product)
 {
     $is_visible = _PS_VERSION_ >= '1.5' ? $product->visibility !== 'none' : true;
     return $product->checkQty(1) && $is_visible ? self::IN_STOCK : self::OUT_OF_STOCK;
 }
 protected function restoreOrderToCart(Cart $cart)
 {
     if (!$cart || !ValidateCore::isLoadedObject($cart)) {
         return null;
     }
     Db::getInstance()->execute('BEGIN');
     $new_cart = null;
     try {
         /** @var CartCore $new_cart */
         /** @noinspection PhpUndefinedClassInspection */
         $new_cart = new Cart();
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_customer = (int) $cart->id_customer;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_address_delivery = (int) $cart->id_address_delivery;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_address_invoice = (int) $cart->id_address_invoice;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_lang = (int) $cart->id_lang;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_currency = (int) $cart->id_currency;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_carrier = (int) $cart->id_carrier;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->recyclable = (int) $cart->recyclable;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->gift = (int) $cart->gift;
         $new_cart->add();
         /** @noinspection PhpUndefinedMethodInspection */
         $products = $cart->getProducts();
         if ($products) {
             foreach ($products as $p) {
                 $idProduct = $p['id_product'];
                 $idProductAttribute = $p['id_product_attribute'];
                 $qty = $p['cart_quantity'];
                 /** @noinspection PhpUndefinedClassInspection */
                 /** @noinspection PhpUndefinedFieldInspection */
                 $producToAdd = new Product((int) $idProduct, true, (int) $cart->id_lang);
                 /** @noinspection PhpUndefinedFieldInspection */
                 if (!$producToAdd->id || !$producToAdd->active) {
                     continue;
                 }
                 /* Check the quantity availability  */
                 if ($idProductAttribute > 0 and is_numeric($idProductAttribute)) {
                     /** @noinspection PhpUndefinedClassInspection */
                     /** @noinspection PhpUndefinedMethodInspection */
                     /** @noinspection PhpUndefinedFieldInspection */
                     if (!$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) and !Attribute::checkAttributeQty((int) $idProductAttribute, (int) $qty)) {
                         /* There is not enough product attribute in stock - set customer qty to current stock on hand */
                         /** @noinspection PhpUndefinedFunctionInspection */
                         $qty = getAttributeQty($idProductAttribute);
                     }
                 } elseif (!$producToAdd->checkQty((int) $qty)) {
                     /* There is not enough product in stock - set customer qty to current stock on hand */
                     /** @noinspection PhpUndefinedMethodInspection */
                     $qty = $producToAdd->getQuantity($idProduct);
                 }
                 $new_cart->updateQty((int) $qty, (int) $idProduct, (int) $idProductAttribute, NULL, 'up');
                 unset($p);
             }
         }
         $new_cart->update();
         Db::getInstance()->execute('COMMIT');
     } catch (Exception $e) {
         Db::getInstance()->execute('ROLLBACK');
         throw $e;
     }
     /** @noinspection PhpUndefinedFieldInspection */
     $this->context->cookie->id_cart = (int) $new_cart->id;
     return $new_cart;
 }
 public function ajaxProcessUpdateQty()
 {
     if ($this->tabAccess['edit'] === '1') {
         $errors = array();
         if (!$this->context->cart->id) {
             return;
         }
         if ($this->context->cart->OrderExists()) {
             $errors[] = Tools::displayError('An order has already been placed with this cart.');
         } elseif (!($id_product = (int) Tools::getValue('id_product')) || !($product = new Product((int) $id_product, true, $this->context->language->id))) {
             $errors[] = Tools::displayError('Invalid product');
         } elseif (!($qty = Tools::getValue('qty')) || $qty == 0) {
             $errors[] = Tools::displayError('Invalid quantity');
         } else {
             $icp = (int) Tools::getValue('icp');
             if ($icp != 'add' && (!($icp = (int) $icp) || $icp == 0)) {
                 $errors[] = Tools::displayError('Invalid cart product reference');
             }
         }
         if (!count($errors)) {
             // Don't try to use a product if not instanciated before due to errors
             if (isset($product) && $product->id) {
                 $id_product_attribute = Tools::getValue('id_product_attribute');
                 if ($icp == 'add') {
                     $id_cart_product = 0;
                     $properties = $product->productProperties();
                     $qty_policy = $properties['pp_qty_policy'];
                     $qty = PP::resolveInputQty($qty, $qty_policy, $properties['pp_qty_step']);
                     if (PP::qtyPolicyFractional($qty_policy)) {
                         $quantity_fractional = $qty;
                         $qty = 1;
                         $update_qty = $quantity_fractional;
                     } else {
                         $qty = (int) $qty;
                         $quantity_fractional = 0;
                         $update_qty = $qty;
                     }
                 } else {
                     $cart_products = $this->context->cart->getProducts();
                     if (count($cart_products)) {
                         foreach ($cart_products as $cart_product) {
                             if ($icp == (int) $cart_product['id_cart_product']) {
                                 $id_cart_product = $icp;
                                 $qty = (int) $qty;
                                 $quantity_fractional = $cart_product['cart_quantity_fractional'];
                                 $update_qty = $qty;
                                 break;
                             }
                         }
                     }
                 }
                 if (isset($id_cart_product)) {
                     if ($id_product_attribute != 0) {
                         if (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty((int) $id_product_attribute, PP::resolveQty($qty, $quantity_fractional))) {
                             $errors[] = Tools::displayError('There is not enough product in stock.');
                         }
                     } else {
                         if (!$product->checkQty(PP::resolveQty($qty, $quantity_fractional))) {
                             $errors[] = Tools::displayError('There is not enough product in stock.');
                         }
                     }
                     if (!($id_customization = (int) Tools::getValue('id_customization', 0)) && !$product->hasAllRequiredCustomizableFields()) {
                         $errors[] = Tools::displayError('Please fill in all required fields.');
                     }
                     $this->context->cart->save();
                 } else {
                     $errors[] = Tools::displayError('This product cannot be added to the cart.');
                 }
             } else {
                 $errors[] = Tools::displayError('This product cannot be added to the cart.');
             }
         }
         if (!count($errors)) {
             if ((int) $update_qty < 0) {
                 $update_qty = str_replace('-', '', $update_qty);
                 $operator = 'down';
             } else {
                 $operator = 'up';
             }
             if (!($qty_upd = $this->context->cart->updateQty($update_qty, $id_product, (int) $id_product_attribute, (int) $id_customization, $operator, 0, null, true, $id_cart_product))) {
                 $errors[] = Tools::displayError('You already have the maximum quantity available for this product.');
             } elseif ($qty_upd < 0) {
                 $minimal_qty = $id_product_attribute ? $product->attributeMinQty((int) $id_product_attribute) : $product->minQty();
                 $errors[] = sprintf(Tools::displayError('You must add a minimum quantity of %d', false), $minimal_qty);
             }
         }
         echo Tools::jsonEncode(array_merge($this->ajaxReturnVars(), array('errors' => $errors)));
     }
 }
 /**
  * @param ShopgateCart $cart
  * @return array
  */
 public function checkStock(ShopgateCart $cart)
 {
     $result = array();
     foreach ($cart->getItems() as $item) {
         $cartItem = new ShopgateCartItem();
         $cartItem->setItemNumber($item->getItemNumber());
         list($productId, $attributeId) = ShopgateHelper::getProductIdentifiers($item);
         /** @var ProductCore $product */
         if (version_compare(_PS_VERSION_, '1.5.2.0', '<')) {
             $product = new BWProduct($productId, true, $this->getPlugin()->getLanguageId());
         } else {
             $product = new Product($productId, $this->getPlugin()->getLanguageId());
         }
         if (empty($attributeId) && !empty($productId) && $product->hasAttributes()) {
             $result[] = $cartItem;
             continue;
         }
         $product->loadStockData();
         /**
          * validate attributes
          */
         if ($product->hasAttributes()) {
             $invalidAttribute = false;
             $message = '';
             if (!$attributeId) {
                 $cartItem->setError(ShopgateLibraryException::UNKNOWN_ERROR_CODE);
                 $cartItem->setErrorText('attributeId required');
                 $message = 'attributeId required';
                 $invalidAttribute = true;
             } else {
                 $validAttributeId = false;
                 if (version_compare(_PS_VERSION_, '1.5.0', '<')) {
                     $attributeIds = BWProduct::getProductAttributesIds($productId);
                 } else {
                     $attributeIds = $product->getProductAttributesIds($productId, true);
                 }
                 foreach ($attributeIds as $attribute) {
                     if ($attributeId == $attribute['id_product_attribute']) {
                         $validAttributeId = true;
                         continue;
                     }
                 }
                 if (!$validAttributeId) {
                     $invalidAttribute = true;
                     $message = 'invalid attributeId';
                 }
             }
             if ($invalidAttribute) {
                 $cartItem->setError(ShopgateLibraryException::UNKNOWN_ERROR_CODE);
                 $cartItem->setErrorText($message);
                 $result[] = $cartItem;
                 continue;
             }
         }
         if ($product->id) {
             if (version_compare(_PS_VERSION_, '1.5.0', '<')) {
                 $quantity = $product->getStockAvailable();
                 //getQuantityAvailableByProduct($productId, $attributeId, $this->getPlugin()->getContext()->shop->id);
             } else {
                 $quantity = StockAvailable::getQuantityAvailableByProduct($productId, $attributeId, $this->getPlugin()->getContext()->shop->id);
             }
             $cartItem->setStockQuantity($quantity);
             $cartItem->setIsBuyable($product->available_for_order && ($attributeId ? Attribute::checkAttributeQty($attributeId, ShopgateItemsCartExportJson::DEFAULT_QTY_TO_CHECK) : $product->checkQty(ShopgateItemsCartExportJson::DEFAULT_QTY_TO_CHECK)) || Product::isAvailableWhenOutOfStock($product->out_of_stock) ? 1 : 0);
         } else {
             $cartItem->setError(ShopgateLibraryException::CART_ITEM_PRODUCT_NOT_FOUND);
             $cartItem->setErrorText(ShopgateLibraryException::getMessageFor($cartItem->getError()));
         }
         $result[] = $cartItem;
     }
     return $result;
 }