Esempio n. 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)));
     }
 }
Esempio n. 2
0
 protected function processChangeProductInCart()
 {
     $mode = Tools::getIsset('update') && $this->id_product ? 'update' : 'add';
     if (!$this->id_product) {
         $this->errors[] = Tools::displayError('Product not found', !Tools::getValue('ajax'));
     }
     $product = new Product($this->id_product, true, $this->context->language->id);
     if (!$product->id || !$product->active || !$product->checkAccess($this->context->cart->id_customer)) {
         $this->errors[] = Tools::displayError('This product is no longer available.', !Tools::getValue('ajax'));
         return;
     }
     $qty_factor = 1;
     $ext_qty_factor = 1;
     $ext_prop_quantities = null;
     $id_cart_product = 0;
     $qty_behavior = 0;
     $icp = (int) Tools::getValue('icp');
     $properties = $product->productProperties();
     if ($icp && $this->context->cart->id) {
         $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;
                     if ($mode == 'add') {
                         if (Tools::getValue('qty') != 'default') {
                             $qty_factor = (int) Tools::getValue('qty');
                         }
                         $_POST['qty'] = (double) $cart_product['cart_quantity_fractional'] > 0 ? (double) $cart_product['cart_quantity_fractional'] : ($product->qtyStep() > 0 ? $product->qtyStep() : 1);
                     } elseif ($mode == 'update') {
                         $qty_behavior = PP::qtyBehavior($product, $cart_product['cart_quantity']);
                     }
                     break;
                 }
             }
         }
     } else {
         if ($properties['pp_ext'] == 1 && in_array($properties['pp_ext_policy'], array(0, 2))) {
             $ext_prop_quantities = array();
             $ext_prop_qty_ratio = array();
             if ($properties['pp_ext_policy'] == 2) {
                 $prop = $product->productProp();
                 if ($this->id_product_attribute) {
                     $id_product_attribute = $this->id_product_attribute;
                 } else {
                     if ($product->hasAttributes()) {
                         $id_product_attribute = Product::getDefaultAttribute($product->id);
                     } else {
                         $id_product_attribute = 0;
                     }
                 }
             }
             $positions = count($properties['pp_ext_prop']);
             for ($position = 1; $position <= $positions; $position++) {
                 $pp_ext_prop = $properties['pp_ext_prop'][$position];
                 if ($properties['pp_ext_policy'] == 2) {
                     $q = PP::productProp($prop, $id_product_attribute, $position, 'quantity');
                     if ($q === false) {
                         $q = (double) $pp_ext_prop['default_quantity'];
                     }
                     if ($q <= 0) {
                         $q = 1;
                     }
                 } else {
                     $q = PP::resolveInputQty(Tools::getValue('pp_ext_prop_quantity_' . $position, 'default'), $properties['pp_qty_policy'], $pp_ext_prop['qty_step'], $pp_ext_prop['default_quantity'] > 0 ? $pp_ext_prop['default_quantity'] : 1);
                 }
                 $ext_prop_quantities[$position] = $q;
                 $ext_prop_qty_ratio[$position] = $properties['pp_ext_prop'][$position]['qty_ratio'];
                 if ($q <= 0) {
                     $this->errors[] = Tools::displayError('Quantity not specified.', !Tools::getValue('ajax'));
                 }
                 $min_qty = (double) $pp_ext_prop['minimum_quantity'];
                 if ($min_qty > 0 && $q < $min_qty) {
                     $this->errors[] = Tools::displayError(sprintf('Please specify at least %s for %s', (string) PP::formatQty($min_qty), (string) $pp_ext_prop['property']), !Tools::getValue('ajax'));
                 }
                 $max_qty = (double) $pp_ext_prop['maximum_quantity'];
                 if ($max_qty > 0 && $q > $max_qty) {
                     $this->errors[] = Tools::displayError(sprintf('Please specify no more than %s for %s', (string) PP::formatQty($max_qty), (string) $pp_ext_prop['property']), !Tools::getValue('ajax'));
                 }
             }
             if (!$this->errors) {
                 $ext_qty_factor = $properties['pp_ext_method'] == 1 ? 1 : 0;
                 $positions = count($ext_prop_quantities);
                 for ($position = 1; $position <= $positions; $position++) {
                     $value = $ext_prop_quantities[$position];
                     $qty_ratio = $ext_prop_qty_ratio[$position];
                     if ($properties['pp_ext_method'] == 1) {
                         $ext_qty_factor *= $qty_ratio > 0 ? $value / $qty_ratio : $value;
                     } elseif ($properties['pp_ext_method'] == 2) {
                         $ext_qty_factor += $qty_ratio > 0 ? $value / $qty_ratio : $value;
                     }
                 }
             }
         }
     }
     if (!$this->errors) {
         if ($this->id_product_attribute) {
             $default_quantity = $product->attributeDefaultQty($this->id_product_attribute);
             $this->qty = $qty_factor * $this->resolveInputQty($properties, $default_quantity);
             if ($this->qty == 0) {
                 $this->errors[] = Tools::displayError('Quantity not specified.', !Tools::getValue('ajax'));
             } else {
                 if (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty($this->id_product_attribute, $ext_qty_factor * $this->qty)) {
                     $this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax'));
                 }
             }
         } else {
             if ($product->hasAttributes()) {
                 $min_quantity = $product->out_of_stock == 2 ? !Configuration::get('PS_ORDER_OUT_OF_STOCK') : !$product->out_of_stock;
                 $this->id_product_attribute = Product::getDefaultAttribute($product->id, $min_quantity);
                 if (!$this->id_product_attribute) {
                     Tools::redirectAdmin($this->context->link->getProductLink($product));
                 } else {
                     $default_quantity = $product->attributeDefaultQty($this->id_product_attribute);
                     $this->qty = $qty_factor * $this->resolveInputQty($properties, $default_quantity);
                     if ($this->qty == 0) {
                         $this->errors[] = Tools::displayError('Quantity not specified.', !Tools::getValue('ajax'));
                     } else {
                         if (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty($this->id_product_attribute, $ext_qty_factor * $this->qty)) {
                             $this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax'));
                         }
                     }
                 }
             } else {
                 $default_quantity = $product->defaultQty();
                 $this->qty = $qty_factor * $this->resolveInputQty($properties, $default_quantity);
                 if ($this->qty == 0) {
                     $this->errors[] = Tools::displayError('Quantity not specified.', !Tools::getValue('ajax'));
                 } else {
                     if (!$product->checkQty($ext_qty_factor * $this->qty)) {
                         $this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax'));
                     }
                 }
             }
         }
     }
     if (!$this->errors && ($mode == 'add' || $mode == 'update' && $qty_behavior)) {
         if ($mode == 'add' && !$this->context->cart->id) {
             if (Context::getContext()->cookie->id_guest) {
                 $guest = new Guest(Context::getContext()->cookie->id_guest);
                 $this->context->cart->mobile_theme = $guest->mobile_theme;
             }
             $this->context->cart->add();
             if ($this->context->cart->id) {
                 $this->context->cookie->id_cart = (int) $this->context->cart->id;
             }
         }
         if (!$product->hasAllRequiredCustomizableFields() && !$this->customization_id) {
             $this->errors[] = Tools::displayError('Please fill in all of the required fields, and then save your customizations.', !Tools::getValue('ajax'));
         }
         if (!$this->errors) {
             $cart_rules = $this->context->cart->getCartRules();
             $update_quantity = $this->context->cart->updateQty($id_cart_product ? $mode == 'add' ? $qty_factor : $this->qty : ($ext_prop_quantities !== null ? $ext_qty_factor : $this->qty), $this->id_product, $this->id_product_attribute, $this->customization_id, $mode == 'update' ? 'update' : Tools::getValue('op', 'up'), $this->id_address_delivery, null, true, $id_cart_product, $ext_prop_quantities, $this->qty);
             if ($update_quantity < 0) {
                 $minimal_quantity = $this->id_product_attribute ? $product->attributeMinQty($this->id_product_attribute) : $product->minQty();
                 $this->errors[] = Tools::displayError(sprintf('You must add %s minimum quantity', $minimal_quantity), !Tools::getValue('ajax'));
             } elseif (!$update_quantity) {
                 $this->errors[] = Tools::displayError('You already have the maximum quantity available for this product.', !Tools::getValue('ajax'));
             } elseif ((int) Tools::getValue('allow_refresh')) {
                 $cart_rules2 = $this->context->cart->getCartRules();
                 if (count($cart_rules2) != count($cart_rules)) {
                     $this->ajax_refresh = true;
                 } else {
                     $rule_list = array();
                     foreach ($cart_rules2 as $rule) {
                         $rule_list[] = $rule['id_cart_rule'];
                     }
                     foreach ($cart_rules as $rule) {
                         if (!in_array($rule['id_cart_rule'], $rule_list)) {
                             $this->ajax_refresh = true;
                             break;
                         }
                     }
                 }
             }
         }
     }
     $removed = CartRule::autoRemoveFromCart();
     CartRule::autoAddToCart();
     if (count($removed) && (int) Tools::getValue('allow_refresh')) {
         $this->ajax_refresh = true;
     }
 }
 public function preProcess()
 {
     parent::preProcess();
     $orderTotal = self::$cart->getOrderTotal(true, Cart::ONLY_PRODUCTS);
     $this->cartDiscounts = self::$cart->getDiscounts();
     foreach ($this->cartDiscounts as $k => $this->cartDiscount) {
         if ($error = self::$cart->checkDiscountValidity(new Discount((int) $this->cartDiscount['id_discount']), $this->cartDiscounts, $orderTotal, self::$cart->getProducts())) {
             self::$cart->deleteDiscount((int) $this->cartDiscount['id_discount']);
         }
     }
     $add = Tools::getIsset('add') ? 1 : 0;
     $delete = Tools::getIsset('delete') ? 1 : 0;
     if (Configuration::get('PS_TOKEN_ENABLE') == 1 && strcasecmp(Tools::getToken(false), strval(Tools::getValue('token'))) && self::$cookie->isLogged() === true) {
         $this->errors[] = Tools::displayError('Invalid token');
     }
     // Update the cart ONLY if $this->cookies are available, in order to avoid ghost carts created by bots
     if (($add or Tools::getIsset('update') or $delete) and isset(self::$cookie->date_add)) {
         //get the values
         $idProduct = (int) Tools::getValue('id_product', NULL);
         $idProductAttribute = (int) Tools::getValue('id_product_attribute', Tools::getValue('ipa'));
         $customizationId = (int) Tools::getValue('id_customization', 0);
         $qty = (int) abs(Tools::getValue('qty', 1));
         if ($qty == 0) {
             $this->errors[] = Tools::displayError('Null quantity');
         } elseif (!$idProduct) {
             $this->errors[] = Tools::displayError('Product not found');
         } else {
             $producToAdd = new Product((int) $idProduct, true, (int) self::$cookie->id_lang);
             if ((!$producToAdd->id or !$producToAdd->active) and !$delete) {
                 if (Tools::getValue('ajax') == 'true') {
                     die('{"hasError" : true, "errors" : ["' . Tools::displayError('Pproduct is no longer available.', false) . '"]}');
                 } else {
                     $this->errors[] = Tools::displayError('Pproduct is no longer available.', false);
                 }
             } else {
                 /* Check the quantity availability */
                 if ($idProductAttribute and is_numeric($idProductAttribute)) {
                     if (!$delete and !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) and !Attribute::checkAttributeQty((int) $idProductAttribute, (int) $qty)) {
                         if (Tools::getValue('ajax') == 'true') {
                             die('{"hasError" : true, "errors" : ["' . Tools::displayError('There is not enough product in stock.', false) . '"]}');
                         } else {
                             $this->errors[] = Tools::displayError('There is not enough product in stock.');
                         }
                     }
                 } elseif ($producToAdd->hasAttributes() and !$delete) {
                     $idProductAttribute = Product::getDefaultAttribute((int) $producToAdd->id, (int) $producToAdd->out_of_stock == 2 ? !(int) Configuration::get('PS_ORDER_OUT_OF_STOCK') : !(int) $producToAdd->out_of_stock);
                     if (!$idProductAttribute) {
                         Tools::redirectAdmin($link->getProductLink($producToAdd));
                     } elseif (!$delete and !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) and !Attribute::checkAttributeQty((int) $idProductAttribute, (int) $qty)) {
                         if (Tools::getValue('ajax') == 'true') {
                             die('{"hasError" : true, "errors" : ["' . Tools::displayError('There is not enough product in stock.', false) . '"]}');
                         } else {
                             $this->errors[] = Tools::displayError('There is not enough product in stock.');
                         }
                     }
                 } elseif (!$delete and !$producToAdd->checkQty((int) $qty)) {
                     if (Tools::getValue('ajax') == 'true') {
                         die('{"hasError" : true, "errors" : ["' . Tools::displayError('There is not enough product in stock.') . '"]}');
                     } else {
                         $this->errors[] = Tools::displayError('There is not enough product in stock.');
                     }
                 }
                 /* Check vouchers compatibility */
                 if ($add and ($producToAdd->specificPrice and (double) $producToAdd->specificPrice['reduction'] or $producToAdd->on_sale)) {
                     $discounts = self::$cart->getDiscounts();
                     foreach ($discounts as $discount) {
                         if (!$discount['cumulable_reduction']) {
                             $this->errors[] = Tools::displayError('Cannot add this product because current voucher does not allow additional discounts.');
                         }
                     }
                 }
                 if (!sizeof($this->errors)) {
                     if ($add and $qty >= 0) {
                         /* Product addition to the cart */
                         if (!isset(self::$cart->id) or !self::$cart->id) {
                             self::$cart->add();
                             if (self::$cart->id) {
                                 self::$cookie->id_cart = (int) self::$cart->id;
                             }
                         }
                         if ($add and !$producToAdd->hasAllRequiredCustomizableFields() and !$customizationId) {
                             $this->errors[] = Tools::displayError('Please fill in all required fields, then save the customization.');
                         }
                         if (!sizeof($this->errors)) {
                             $updateQuantity = self::$cart->updateQty((int) $qty, (int) $idProduct, (int) $idProductAttribute, $customizationId, Tools::getValue('op', 'up'));
                             if ($updateQuantity < 0) {
                                 /* if product has attribute, minimal quantity is set with minimal quantity of attribute*/
                                 if ((int) $idProductAttribute) {
                                     $minimal_quantity = Attribute::getAttributeMinimalQty((int) $idProductAttribute);
                                 } else {
                                     $minimal_quantity = $producToAdd->minimal_quantity;
                                 }
                                 if (Tools::getValue('ajax') == 'true') {
                                     die('{"hasError" : true, "errors" : ["' . Tools::displayError('You must add', false) . ' ' . $minimal_quantity . ' ' . Tools::displayError('Minimum quantity', false) . '"]}');
                                 } else {
                                     $this->errors[] = Tools::displayError('You must add') . ' ' . $minimal_quantity . ' ' . Tools::displayError('Minimum quantity') . ((isset($_SERVER['HTTP_REFERER']) and basename($_SERVER['HTTP_REFERER']) == 'order.php' or !Tools::isSubmit('ajax') and substr(basename($_SERVER['REQUEST_URI']), 0, strlen('cart.php')) == 'cart.php') ? '<script language="javascript">setTimeout("history.back()",5000);</script><br />- ' . Tools::displayError('You will be redirected to your cart in a few seconds.') : '');
                                 }
                             } elseif (!$updateQuantity) {
                                 if (Tools::getValue('ajax') == 'true') {
                                     die('{"hasError" : true, "errors" : ["' . Tools::displayError('You already have the maximum quantity available for this product.', false) . '"]}');
                                 } else {
                                     $this->errors[] = Tools::displayError('You already have the maximum quantity available for this product.') . ((isset($_SERVER['HTTP_REFERER']) and basename($_SERVER['HTTP_REFERER']) == 'order.php' or !Tools::isSubmit('ajax') and substr(basename($_SERVER['REQUEST_URI']), 0, strlen('cart.php')) == 'cart.php') ? '<script language="javascript">setTimeout("history.back()",5000);</script><br />- ' . Tools::displayError('You will be redirected to your cart in a few seconds.') : '');
                                 }
                             }
                         }
                     } elseif ($delete) {
                         if (self::$cart->deleteProduct((int) $idProduct, (int) $idProductAttribute, (int) $customizationId)) {
                             if (!Cart::getNbProducts((int) self::$cart->id)) {
                                 self::$cart->id_carrier = 0;
                                 self::$cart->gift = 0;
                                 self::$cart->gift_message = '';
                                 self::$cart->update();
                             }
                         }
                     }
                 }
                 $discounts = self::$cart->getDiscounts();
                 foreach ($discounts as $discount) {
                     $discountObj = new Discount((int) $discount['id_discount'], (int) self::$cookie->id_lang);
                     if ($error = self::$cart->checkDiscountValidity($discountObj, $discounts, self::$cart->getOrderTotal(true, Cart::ONLY_PRODUCTS), self::$cart->getProducts())) {
                         self::$cart->deleteDiscount((int) $discount['id_discount']);
                         self::$cart->update();
                         $errors[] = $error;
                     }
                 }
                 if (!sizeof($this->errors)) {
                     $queryString = Tools::safeOutput(Tools::getValue('query', NULL));
                     if ($queryString and !Configuration::get('PS_CART_REDIRECT')) {
                         Tools::redirect('search.php?search=' . $queryString);
                     }
                     if (isset($_SERVER['HTTP_REFERER'])) {
                         // Redirect to previous page
                         preg_match('!http(s?)://(.*)/(.*)!', $_SERVER['HTTP_REFERER'], $regs);
                         if (isset($regs[3]) and !Configuration::get('PS_CART_REDIRECT') and Tools::getValue('ajax') != 'true') {
                             Tools::redirect($regs[3]);
                         }
                     }
                 }
             }
             if (Tools::getValue('ajax') != 'true' and !sizeof($this->errors)) {
                 Tools::redirect('order.php?' . (isset($idProduct) ? 'ipa=' . (int) $idProduct : ''));
             }
         }
     }
 }
Esempio n. 4
0
 /**
  * This process add or update a product in the cart
  */
 protected function processChangeProductInCart()
 {
     $mode = Tools::getIsset('update') && $this->id_product ? 'update' : 'add';
     if ($this->qty == 0) {
         $this->errors[] = Tools::displayError('Null quantity.', !Tools::getValue('ajax'));
     } elseif (!$this->id_product) {
         $this->errors[] = Tools::displayError('Product not found', !Tools::getValue('ajax'));
     }
     $product = new Product($this->id_product, true, $this->context->language->id);
     if (!$product->id || !$product->active || !$product->checkAccess($this->context->cart->id_customer)) {
         $this->errors[] = Tools::displayError('This product is no longer available.', !Tools::getValue('ajax'));
         return;
     }
     $qty_to_check = $this->qty;
     $cart_products = $this->context->cart->getProducts();
     if (is_array($cart_products)) {
         foreach ($cart_products as $cart_product) {
             if ((!isset($this->id_product_attribute) || $cart_product['id_product_attribute'] == $this->id_product_attribute) && (isset($this->id_product) && $cart_product['id_product'] == $this->id_product)) {
                 $qty_to_check = $cart_product['cart_quantity'];
                 if (Tools::getValue('op', 'up') == 'down') {
                     $qty_to_check -= $this->qty;
                 } else {
                     $qty_to_check += $this->qty;
                 }
                 break;
             }
         }
     }
     // Check product quantity availability
     if ($this->id_product_attribute) {
         if (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty($this->id_product_attribute, $qty_to_check)) {
             $this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax'));
         }
     } elseif ($product->hasAttributes()) {
         $minimumQuantity = $product->out_of_stock == 2 ? !Configuration::get('PS_ORDER_OUT_OF_STOCK') : !$product->out_of_stock;
         $this->id_product_attribute = Product::getDefaultAttribute($product->id, $minimumQuantity);
         // @todo do something better than a redirect admin !!
         if (!$this->id_product_attribute) {
             Tools::redirectAdmin($this->context->link->getProductLink($product));
         } elseif (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty($this->id_product_attribute, $qty_to_check)) {
             $this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax'));
         }
     } elseif (!$product->checkQty($qty_to_check)) {
         $this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax'));
     }
     // If no errors, process product addition
     if (!$this->errors && $mode == 'add') {
         // Add cart if no cart found
         if (!$this->context->cart->id) {
             if (Context::getContext()->cookie->id_guest) {
                 $guest = new Guest(Context::getContext()->cookie->id_guest);
                 $this->context->cart->mobile_theme = $guest->mobile_theme;
             }
             $this->context->cart->add();
             if ($this->context->cart->id) {
                 $this->context->cookie->id_cart = (int) $this->context->cart->id;
             }
         }
         // Check customizable fields
         if (!$product->hasAllRequiredCustomizableFields() && !$this->customization_id) {
             $this->errors[] = Tools::displayError('Please fill in all of the required fields, and then save your customizations.', !Tools::getValue('ajax'));
         }
         if (!$this->errors) {
             $cart_rules = $this->context->cart->getCartRules();
             $available_cart_rules = CartRule::getCustomerCartRules($this->context->language->id, isset($this->context->customer->id) ? $this->context->customer->id : 0, true, true, true, $this->context->cart, false, true);
             $update_quantity = $this->context->cart->updateQty($this->qty, $this->id_product, $this->id_product_attribute, $this->customization_id, Tools::getValue('op', 'up'), $this->id_address_delivery);
             if ($update_quantity < 0) {
                 // If product has attribute, minimal quantity is set with minimal quantity of attribute
                 $minimal_quantity = $this->id_product_attribute ? Attribute::getAttributeMinimalQty($this->id_product_attribute) : $product->minimal_quantity;
                 $this->errors[] = sprintf(Tools::displayError('You must add %d minimum quantity', !Tools::getValue('ajax')), $minimal_quantity);
             } elseif (!$update_quantity) {
                 $this->errors[] = Tools::displayError('You already have the maximum quantity available for this product.', !Tools::getValue('ajax'));
             } elseif ((int) Tools::getValue('allow_refresh')) {
                 // If the cart rules has changed, we need to refresh the whole cart
                 $cart_rules2 = $this->context->cart->getCartRules();
                 if (count($cart_rules2) != count($cart_rules)) {
                     $this->ajax_refresh = true;
                 } elseif (count($cart_rules2)) {
                     $rule_list = array();
                     foreach ($cart_rules2 as $rule) {
                         $rule_list[] = $rule['id_cart_rule'];
                     }
                     foreach ($cart_rules as $rule) {
                         if (!in_array($rule['id_cart_rule'], $rule_list)) {
                             $this->ajax_refresh = true;
                             break;
                         }
                     }
                 } else {
                     $available_cart_rules2 = CartRule::getCustomerCartRules($this->context->language->id, isset($this->context->customer->id) ? $this->context->customer->id : 0, true, true, true, $this->context->cart, false, true);
                     if (count($available_cart_rules2) != count($available_cart_rules)) {
                         $this->ajax_refresh = true;
                     } elseif (count($available_cart_rules2)) {
                         $rule_list = array();
                         foreach ($available_cart_rules2 as $rule) {
                             $rule_list[] = $rule['id_cart_rule'];
                         }
                         foreach ($cart_rules2 as $rule) {
                             if (!in_array($rule['id_cart_rule'], $rule_list)) {
                                 $this->ajax_refresh = true;
                                 break;
                             }
                         }
                     }
                 }
             }
         }
     }
     $removed = CartRule::autoRemoveFromCart();
     CartRule::autoAddToCart();
     if (count($removed) && (int) Tools::getValue('allow_refresh')) {
         $this->ajax_refresh = true;
     }
 }
Esempio n. 5
0
 public function validateGlobalCartProductQuantityUpdate($add, $delete, $qty, $producToAdd, $idProduct, $idProductAttribute, &$errors)
 {
     global $cookie, $cart;
     // code taken from CartController preProcess()
     if ((!$producToAdd->id or !$producToAdd->active) and !$delete) {
         $errors[] = Tools::displayError('Product is no longer available.', false);
         return false;
     }
     /* Check the quantity availability */
     if ($idProductAttribute and is_numeric($idProductAttribute)) {
         if (!$delete and !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) and !Attribute::checkAttributeQty((int) $idProductAttribute, (int) $qty)) {
             $errors[] = Tools::displayError('There is not enough product in stock.', false);
             return false;
         }
     } elseif ($producToAdd->hasAttributes() and !$delete) {
         $errors[] = 'Product must have a combination chosen';
         return false;
     } elseif (!$delete and !$producToAdd->checkQty((int) $qty)) {
         $errors[] = Tools::displayError('There is not enough product in stock.', false);
         return false;
     }
     /* Check vouchers compatibility */
     if ($add and ($producToAdd->specificPrice and (double) $producToAdd->specificPrice['reduction'] or $producToAdd->on_sale)) {
         $discounts = $cart->getDiscounts();
         $hasUndiscountedProduct = null;
         foreach ($discounts as $discount) {
             if (is_null($hasUndiscountedProduct)) {
                 $hasUndiscountedProduct = false;
                 foreach ($cart->getProducts() as $product) {
                     if ($product['reduction_applies'] === false) {
                         $hasUndiscountedProduct = true;
                         break;
                     }
                 }
             }
             if (!$discount['cumulable_reduction'] && ($discount['id_discount_type'] != 1 || !$hasUndiscountedProduct)) {
                 $errors[] = Tools::displayError('Cannot add this product because current voucher does not allow additional discounts.');
                 return false;
             }
         }
     }
     return true;
 }
Esempio n. 6
0
     $errors[] = Tools::displayError('product not found');
 } else {
     $producToAdd = new Product(intval($idProduct), false, intval($cookie->id_lang));
     if ((!$producToAdd->id or !$producToAdd->active) and !$delete) {
         $errors[] = Tools::displayError('product is no longer available');
     } else {
         /* Check the quantity availability */
         if ($idProductAttribute and is_numeric($idProductAttribute)) {
             if (!$delete and !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) and !Attribute::checkAttributeQty(intval($idProductAttribute), intval($qty))) {
                 $errors[] = Tools::displayError('product is no longer available');
             }
         } elseif ($producToAdd->hasAttributes() and !$delete) {
             $idProductAttribute = Product::getDefaultAttribute(intval($producToAdd->id), intval($producToAdd->out_of_stock) == 2 ? !intval(Configuration::get('PS_ORDER_OUT_OF_STOCK')) : !intval($producToAdd->out_of_stock));
             if (!$idProductAttribute) {
                 Tools::redirectAdmin($link->getProductLink($producToAdd));
             } elseif (!$delete and !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) and !Attribute::checkAttributeQty(intval($idProductAttribute), intval($qty))) {
                 $errors[] = Tools::displayError('product is no longer available');
             }
         } elseif (!$delete and !$producToAdd->checkQty(intval($qty))) {
             $errors[] = Tools::displayError('product is no longer available');
         }
         /* Check vouchers compatibility */
         if ($add and (intval($producToAdd->reduction_price) or intval($producToAdd->reduction_percent) or $producToAdd->on_sale)) {
             $discounts = $cart->getDiscounts();
             foreach ($discounts as $discount) {
                 if (!$discount['cumulable_reduction']) {
                     $errors[] = Tools::displayError('cannot add this product because current voucher doesn\'t allow additional discounts');
                 }
             }
         }
         if (!sizeof($errors)) {
 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)));
     }
 }
Esempio n. 9
0
 /**
  * @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;
 }