Exemplo n.º 1
0
 private function resolveInputQty($properties, $default_qty)
 {
     return PP::resolveInputQty(Tools::getValue('qty'), $properties['pp_qty_policy'], $properties['pp_qty_step'], $default_qty);
 }
Exemplo n.º 2
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));
 }
Exemplo n.º 3
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)));
     }
 }
Exemplo n.º 4
0
    /**
     * Add bought product
     *
     * @return boolean succeed
     */
    public static function addBoughtProduct($id_wishlist, $id_product, $id_product_attribute, $id_cart, $quantity)
    {
        if (!Validate::isUnsignedId($id_wishlist) || !Validate::isUnsignedId($id_product) || !Validate::isUnsignedId($quantity)) {
            die(Tools::displayError());
        }
        $properties = PP::getProductProperties($id_product);
        $qty_policy_legacy = PP::qtyPolicyLegacy($properties['pp_qty_policy']);
        $quantity = PP::resolveInputQty(Tools::getValue('qty'), $properties['pp_qty_policy'], $properties['pp_qty_step'], $quantity);
        $result = Db::getInstance()->getRow('
			SELECT `quantity`, `quantity_fractional`, `id_wishlist_product`
		  FROM `' . _DB_PREFIX_ . 'wishlist_product` wp
			WHERE `id_wishlist` = ' . (int) $id_wishlist . '
			AND `id_product` = ' . (int) $id_product . '
			AND `id_product_attribute` = ' . (int) $id_product_attribute);
        $qty = $qty_policy_legacy ? (int) $result['quantity'] : (double) $result['quantity_fractional'];
        if (!count($result) || $qty - $quantity < 0 || $quantity > $qty) {
            return false;
        }
        Db::getInstance()->executeS('
			SELECT *
			FROM `' . _DB_PREFIX_ . 'wishlist_product_cart`
			WHERE `id_wishlist_product`=' . (int) $result['id_wishlist_product'] . ' AND `id_cart`=' . (int) $id_cart);
        if (Db::getInstance()->NumRows() > 0) {
            $result2 = Db::getInstance()->execute('
				UPDATE `' . _DB_PREFIX_ . 'wishlist_product_cart`
				SET `quantity`=`quantity` + ' . ($qty_policy_legacy ? $quantity : 0) . ',
				`quantity_fractional`=`quantity_fractional` + ' . ($qty_policy_legacy ? 0 : $quantity) . '
				WHERE `id_wishlist_product`=' . (int) $result['id_wishlist_product'] . ' AND `id_cart`=' . (int) $id_cart);
        } else {
            $result2 = Db::getInstance()->execute('
				INSERT INTO `' . _DB_PREFIX_ . 'wishlist_product_cart`
				(`id_wishlist_product`, `id_cart`, `quantity`, `quantity_fractional`, `date_add`) VALUES(
				' . (int) $result['id_wishlist_product'] . ',
				' . (int) $id_cart . ',
				' . ($qty_policy_legacy ? $quantity : 1) . ',
				' . ($qty_policy_legacy ? 0 : $quantity) . ',
				\'' . pSQL(date('Y-m-d H:i:s')) . '\')');
        }
        if ($result2 === false) {
            return false;
        }
        return Db::getInstance()->execute('
			UPDATE `' . _DB_PREFIX_ . 'wishlist_product` SET
			`quantity`=`quantity` - ' . ($qty_policy_legacy ? $quantity : 0) . ',
			`quantity_fractional`=`quantity_fractional` - ' . ($qty_policy_legacy ? 0 : $quantity) . '
			WHERE `id_wishlist` = ' . (int) $id_wishlist . '
			AND `id_product` = ' . (int) $id_product . '
			AND `id_product_attribute` = ' . (int) $id_product_attribute);
    }