예제 #1
0
    public function updateQty($quantity, $id_product, $id_product_attribute = null, $id_customization = false, $operator = 'up', $id_address_delivery = 0, Shop $shop = null, $auto_add_cart_rule = true, $id_cart_product = 0, $ext_prop_quantities = null, $ext_qty = 0)
    {
        if (!$shop) {
            $shop = Context::getContext()->shop;
        }
        if (Context::getContext()->customer->id) {
            if ($id_address_delivery == 0 && (int) $this->id_address_delivery) {
                // The $id_address_delivery is null, use the cart delivery address
                $id_address_delivery = $this->id_address_delivery;
            } elseif ($id_address_delivery == 0) {
                // The $id_address_delivery is null, get the default customer address
                $id_address_delivery = (int) Address::getFirstCustomerAddressId((int) Context::getContext()->customer->id);
            } elseif (!Customer::customerHasAddress(Context::getContext()->customer->id, $id_address_delivery)) {
                // The $id_address_delivery must be linked with customer
                $id_address_delivery = 0;
            }
        }
        //$quantity = (int)$quantity;
        $id_product = (int) $id_product;
        $id_product_attribute = (int) $id_product_attribute;
        $product = new Product($id_product, false, Configuration::get('PS_LANG_DEFAULT'), $shop->id);
        if ($id_product_attribute) {
            $combination = new Combination((int) $id_product_attribute);
            if ($combination->id_product != $id_product) {
                return false;
            }
        }
        $properties = $product->productProperties();
        $quantity = $product->normalizeQty($quantity);
        /* If we have a product combination, the minimal quantity is set with the one of this combination */
        if (!empty($id_product_attribute)) {
            $minimal_quantity = $product->attributeMinQty($id_product_attribute);
        } else {
            $minimal_quantity = $product->minQty();
        }
        if (!Validate::isLoadedObject($product)) {
            die(Tools::displayError());
        }
        if (isset(self::$_nbProducts[$this->id])) {
            unset(self::$_nbProducts[$this->id]);
        }
        if (isset(self::$_totalWeight[$this->id])) {
            unset(self::$_totalWeight[$this->id]);
        }
        Hook::exec('actionBeforeCartUpdateQty', array('cart' => $this, 'product' => $product, 'id_product_attribute' => $id_product_attribute, 'id_customization' => $id_customization, 'quantity' => $quantity, 'operator' => $operator, 'id_address_delivery' => $id_address_delivery, 'shop' => $shop, 'auto_add_cart_rule' => $auto_add_cart_rule));
        if ($quantity <= 0) {
            return $this->deleteProduct($id_product, $id_product_attribute, (int) $id_customization, 0, $id_cart_product);
        } elseif (!$product->available_for_order || Configuration::get('PS_CATALOG_MODE') && !defined('_PS_ADMIN_DIR_')) {
            return false;
        } else {
            if ($id_cart_product == 0 && !PP::qtyPolicyLegacy($properties['pp_qty_policy'])) {
                $result = false;
            } else {
                /* Check if the product is already in the cart */
                $result = $this->containsProduct($id_product, $id_product_attribute, (int) $id_customization, (int) $id_address_delivery, PP::productQtyPolicyLegacy($product) ? 0 : $quantity, $id_cart_product);
            }
            /* Update quantity if product already exist */
            if ($result) {
                if ($operator == 'up' || $operator == 'update') {
                    $sql = 'SELECT stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, IFNULL(stock.quantity_remainder, 0) as quantity_remainder
							FROM ' . _DB_PREFIX_ . 'product p
							' . Product::sqlStock('p', $id_product_attribute, true, $shop) . '
							WHERE p.id_product = ' . $id_product;
                    $result2 = Db::getInstance()->getRow($sql);
                    $product_qty = (int) $result2['quantity'] + (double) $result2['quantity_remainder'];
                    // Quantity for product pack
                    if (Pack::isPack($id_product)) {
                        $product_qty = Pack::getQuantity($id_product, $id_product_attribute);
                    }
                    if ($operator == 'up') {
                        $q = $id_cart_product > 0 || PP::qtyPolicyLegacy($properties['pp_qty_policy']) ? (int) $quantity : 1;
                        $new_qty = PP::resolveQty((int) $result['quantity'] + $q, $result['quantity_fractional']);
                        $new_min_qty = $properties['pp_ext'] == 1 ? (int) $result['quantity'] + $q : $new_qty;
                        $qty = '+ ' . (int) $q;
                    } else {
                        $new_qty = $new_min_qty = $qty = $quantity;
                    }
                    if (!Product::isAvailableWhenOutOfStock((int) $result2['out_of_stock'])) {
                        if ($new_qty > $product_qty) {
                            return false;
                        }
                    }
                } else {
                    if ($operator == 'down') {
                        $q = $id_cart_product > 0 ? (int) $quantity : 1;
                        $new_qty = PP::resolveQty((int) $result['quantity'] - $q, $result['quantity_fractional']);
                        $new_min_qty = $properties['pp_ext'] == 1 ? (int) $result['quantity'] - $q : $new_qty;
                        $qty = '- ' . (int) $q;
                        if ($new_min_qty < $minimal_quantity && (PP::qtyPolicyLegacy($properties['pp_qty_policy'] || $properties['pp_ext'] == 1) ? $minimal_quantity > 1 : $new_qty > 0)) {
                            return -1;
                        }
                    } else {
                        return false;
                    }
                }
                /* Delete product from cart */
                if (($properties['pp_ext'] == 1 ? $new_min_qty : $new_qty) <= 0) {
                    return $this->deleteProduct((int) $id_product, (int) $id_product_attribute, (int) $id_customization, $id_address_delivery, $id_cart_product);
                } else {
                    if ($new_min_qty < $minimal_quantity) {
                        return -1;
                    } else {
                        if ($operator == 'up' || $operator == 'down') {
                            Db::getInstance()->execute('
								UPDATE `' . _DB_PREFIX_ . 'cart_product`
								SET `quantity` = `quantity` ' . $qty . ', `date_add` = NOW()
								WHERE `id_cart_product` = ' . (int) $result['id_cart_product'] . '
								LIMIT 1');
                        } else {
                            Db::getInstance()->execute('
							UPDATE `' . _DB_PREFIX_ . 'cart_product`
							SET `quantity_fractional` = ' . $qty . ', `date_add` = NOW()
							WHERE `id_cart_product` = ' . (int) $result['id_cart_product'] . '
							LIMIT 1');
                        }
                    }
                }
                $id_cart_product = (int) $result['id_cart_product'];
            } elseif ($operator == 'up') {
                $sql = 'SELECT stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, IFNULL(stock.quantity_remainder, 0) as quantity_remainder
						FROM ' . _DB_PREFIX_ . 'product p
						' . Product::sqlStock('p', $id_product_attribute, true, $shop) . '
						WHERE p.id_product = ' . $id_product;
                $result2 = Db::getInstance()->getRow($sql);
                // Quantity for product pack
                if (Pack::isPack($id_product)) {
                    $result2['quantity'] = Pack::getQuantity($id_product, $id_product_attribute);
                }
                $total_quantity = PP::qtyPolicyLegacy($properties['pp_qty_policy']) ? $quantity : ($ext_prop_quantities !== null && $ext_qty > 0 ? $quantity * $ext_qty : $quantity);
                if (!Product::isAvailableWhenOutOfStock((int) $result2['out_of_stock'])) {
                    if ($total_quantity > $result2['quantity'] + (PP::qtyPolicyFractional($properties['pp_qty_policy']) ? (double) $result2['quantity_remainder'] : 0)) {
                        return false;
                    }
                }
                if (PP::qtyPolicyFractional($properties['pp_qty_policy']) && $ext_prop_quantities !== null && $ext_qty > 0) {
                    if ($ext_qty < $minimal_quantity) {
                        return -1;
                    } else {
                        if ($total_quantity < $minimal_quantity) {
                            return -1;
                        }
                    }
                }
                $result_add = Db::getInstance()->insert('cart_product', array('id_product' => (int) $id_product, 'id_product_attribute' => (int) $id_product_attribute, 'id_cart' => (int) $this->id, 'id_address_delivery' => (int) $id_address_delivery, 'id_shop' => $shop->id, 'quantity' => PP::qtyPolicyLegacy($properties['pp_qty_policy']) ? $quantity : ($ext_prop_quantities !== null && $ext_qty > 0 ? $ext_qty : 1), 'quantity_fractional' => PP::qtyPolicyLegacy($properties['pp_qty_policy']) ? 0 : $quantity, 'date_add' => date('Y-m-d H:i:s')));
                if (!$result_add) {
                    return false;
                }
                $id_cart_product = Db::getInstance()->Insert_ID();
                if (count($ext_prop_quantities)) {
                    $db = Db::getInstance();
                    foreach ($ext_prop_quantities as $index => $value) {
                        $db->insert('pp_product_ext', array('id_cart_product' => (int) $id_cart_product, 'position' => (int) $index, 'quantity' => (double) $value));
                    }
                }
            }
            $this->last_icp = $id_cart_product;
        }
        // refresh cache of self::_products
        $this->_products = $this->getProducts(true);
        $this->update();
        $context = Context::getContext()->cloneContext();
        $context->cart = $this;
        Cache::clean('getContextualValue_*');
        if ($auto_add_cart_rule) {
            CartRule::autoAddToCart($context);
        }
        if ($product->customizable) {
            return $this->_updateCustomizationQuantity($quantity, (int) $id_customization, (int) $id_product, (int) $id_product_attribute, (int) $id_address_delivery, $operator, $id_cart_product);
        } else {
            return true;
        }
    }
예제 #2
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');
         } 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)));
     }
 }
예제 #3
0
 private function processPrice()
 {
     if ($this->id_product) {
         $product = new Product($this->id_product, true, $this->context->language->id);
         if ($product->id && $product->active) {
             $error = false;
             $properties = $product->productProperties();
             if ($properties['pp_ext'] == 1 && in_array($properties['pp_ext_policy'], array(0, 2))) {
                 $qty = PP::resolveInputQty(Tools::getValue('qty'), 0, $properties['pp_qty_step']);
             } else {
                 $qty = PP::resolveInputQty(Tools::getValue('qty'), $properties['pp_qty_policy'], $properties['pp_qty_step']);
             }
             if ($qty <= 0) {
                 $error = true;
             }
             if (!$error) {
                 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();
                     }
                     $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, $this->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) {
                             $error = true;
                         }
                         $min_qty = (double) $pp_ext_prop['minimum_quantity'];
                         if ($min_qty > 0 && $q < $min_qty) {
                             $error = true;
                         }
                         $max_qty = (double) $pp_ext_prop['maximum_quantity'];
                         if ($max_qty > 0 && $q > $max_qty) {
                             $error = true;
                         }
                     }
                     if (!$error) {
                         $ext_prop_qty = array();
                         $quantity = $qty;
                         $quantity_fractional = $properties['pp_ext_method'] == 1 ? 1 : 0;
                         $count = count($ext_prop_quantities);
                         for ($position = 1; $position <= $count; $position++) {
                             $value = $ext_prop_quantities[$position];
                             $qty_ratio = $ext_prop_qty_ratio[$position];
                             $ext_prop_qty[$position] = $qty_ratio > 0 ? $value / $qty_ratio : $value;
                             if ($properties['pp_ext_method'] == 1) {
                                 $quantity_fractional *= $ext_prop_qty[$position];
                             } elseif ($properties['pp_ext_method'] == 2) {
                                 $quantity_fractional += $ext_prop_qty[$position];
                             }
                         }
                     }
                 } else {
                     if (PP::qtyPolicyFractional($properties['pp_qty_policy'])) {
                         $quantity = 1;
                         $quantity_fractional = $qty;
                     } else {
                         $quantity = $qty;
                         $quantity_fractional = 0;
                     }
                 }
             }
             if (!$error) {
                 $id_customer = isset($this->context->customer) ? $this->context->customer->id : null;
                 $this->id_product_attribute = $this->id_product_attribute > 0 ? $this->id_product_attribute : null;
                 $qty = array($quantity, $quantity_fractional);
                 $address = new Address($this->context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
                 $tax = (double) $product->getTaxesRate($address);
                 $no_tax = Tax::excludeTaxeOption() || !$tax || Group::getPriceDisplayMethod($this->context->customer->id_default_group);
                 $specific_price_output = null;
                 $total = Product::getPriceStatic($this->id_product, !$no_tax, $this->id_product_attribute, 6, null, false, true, $qty, false, $id_customer, null, $address->id, $specific_price_output, true, true, $this->context);
                 $total = PP::calcPrice($total, $quantity, $quantity_fractional, $this->id_product, false);
                 $result = array('status' => 'success', 'total' => &$total);
                 /*[HOOK ppropertiessmartprice]*/
                 $result['total'] = Tools::ps_round($total, _PS_PRICE_COMPUTE_PRECISION_);
             }
         }
     }
     if (!isset($result)) {
         $result = array('status' => 'error');
     }
     die(Tools::jsonEncode($result));
 }