public static function getQuantityDiscounts($id_product, $price) { global $cookie; $result = Db::getInstance()->ExecuteS(' SELECT * FROM ' . _DB_PREFIX_ . 'discount_quantity dq LEFT JOIN `' . _DB_PREFIX_ . 'discount_type_lang` dtl ON (dq.`id_discount_type` = dtl.`id_discount_type` AND dtl.`id_lang` = ' . intval($cookie->id_lang) . ') WHERE dq.`id_product` = ' . intval($id_product) . ' ORDER BY dq.`quantity` ASC'); foreach ($result as $key => &$row) { $row['real_value'] = QuantityDiscount::getValue(floatval($price), intval($row['id_discount_type']), $row['value']); $row['nextQuantity'] = isset($result[$key + 1]) ? intval($result[$key + 1]['quantity']) : -1; } return $result; }
public static function getQuantityDiscounts($id_product, $price) { global $cookie; /* Well, we don't get $id_product_attribute from anywhere so I guess we can't do much, but the DB schema accommodates for price reductions per attribute... */ $base_price = Product::getBasePriceStaticLC($id_product, NULL); $result = Db::getInstance()->ExecuteS(' SELECT * FROM ' . _DB_PREFIX_ . 'discount_quantity dq LEFT JOIN `' . _DB_PREFIX_ . 'discount_type_lang` dtl ON (dq.`id_discount_type` = dtl.`id_discount_type` AND dtl.`id_lang` = ' . intval($cookie->id_lang) . ') WHERE dq.`id_product_price` = ' . intval($base_price['id_product_price']) . ' ORDER BY dq.`quantity` ASC'); foreach ($result as $key => &$row) { $row['real_value'] = QuantityDiscount::getValue(floatval($price), intval($row['id_discount_type']), $row['value']); $row['nextQuantity'] = isset($result[$key + 1]) ? intval($result[$key + 1]['quantity']) : -1; } return $result; }
/** * Validate an order in database * Function called from a payment module * * @param integer $id_cart Value * @param integer $id_order_state Value * @param float $amountPaid Amount really paid by customer (in the default currency) * @param string $paymentMethod Payment method (eg. 'Credit cart') * @param string $message Message to attach to order */ function validateOrder($id_cart, $id_order_state, $amountPaid, $paymentMethod = 'Unknown', $message = NULL, $extraVars = array(), $currency_special = NULL, $dont_touch_amount = false) { global $cart; $cart = new Cart(intval($id_cart)); // Does order already exists ? if (Validate::isLoadedObject($cart) and $cart->OrderExists() === 0) { // Copying data from cart $order = new Order(); $order->id_carrier = intval($cart->id_carrier); $order->id_customer = intval($cart->id_customer); $order->id_address_invoice = intval($cart->id_address_invoice); $order->id_address_delivery = intval($cart->id_address_delivery); $vat_address = new Address(intval($order->id_address_delivery)); $id_zone = Address::getZoneById(intval($vat_address->id)); $order->id_currency = $currency_special ? intval($currency_special) : intval($cart->id_currency); $order->id_lang = intval($cart->id_lang); $order->id_cart = intval($cart->id); $customer = new Customer(intval($order->id_customer)); $order->secure_key = pSQL($customer->secure_key); $order->payment = Tools::substr($paymentMethod, 0, 32); if (isset($this->name)) { $order->module = $this->name; } $order->recyclable = $cart->recyclable; $order->gift = intval($cart->gift); $order->gift_message = $cart->gift_message; $currency = new Currency($order->id_currency); $amountPaid = !$dont_touch_amount ? Tools::ps_round(floatval($amountPaid), 2) : $amountPaid; $order->total_paid_real = $amountPaid; $order->total_products = floatval($cart->getOrderTotal(false, 1)); $order->total_products_wt = floatval($cart->getOrderTotal(true, 1)); $order->total_discounts = floatval(abs($cart->getOrderTotal(true, 2))); $order->total_shipping = floatval($cart->getOrderShippingCost()); $order->total_wrapping = floatval(abs($cart->getOrderTotal(true, 6))); $order->total_paid = floatval(Tools::ps_round(floatval($cart->getOrderTotal(true, 3)), 2)); $order->invoice_date = '0000-00-00 00:00:00'; $order->delivery_date = '0000-00-00 00:00:00'; // Amount paid by customer is not the right one -> Status = payment error if ($order->total_paid != $order->total_paid_real) { $id_order_state = _PS_OS_ERROR_; } // Creating order if ($cart->OrderExists() === 0) { $result = $order->add(); } else { die(Tools::displayError('An order has already been placed using this cart')); } // Next ! if ($result and isset($order->id)) { // Optional message to attach to this order if (isset($message) and !empty($message)) { $msg = new Message(); $message = strip_tags($message, '<br>'); if (!Validate::isCleanHtml($message)) { $message = $this->l('Payment message is not valid, please check your module!'); } $msg->message = $message; $msg->id_order = intval($order->id); $msg->private = 1; $msg->add(); } // Insert products from cart into order_detail table $products = $cart->getProducts(); $productsList = ''; $db = Db::getInstance(); $query = 'INSERT INTO `' . _DB_PREFIX_ . 'order_detail` (`id_order`, `product_id`, `product_attribute_id`, `product_name`, `product_quantity`, `product_quantity_in_stock`, `product_price`, `reduction_percent`, `reduction_amount`, `product_quantity_discount`, `product_ean13`, `product_reference`, `product_supplier_reference`, `product_weight`, `tax_name`, `tax_rate`, `ecotax`, `discount_quantity_applied`, `download_deadline`, `download_hash`) VALUES '; $customizedDatas = Product::getAllCustomizedDatas(intval($order->id_cart)); Product::addCustomizationPrice($products, $customizedDatas); foreach ($products as $key => $product) { $outOfStock = false; $productQuantity = intval(Product::getQuantity(intval($product['id_product']), $product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL)); $quantityInStock = $productQuantity - intval($product['cart_quantity']) < 0 ? $productQuantity : intval($product['cart_quantity']); if ($id_order_state != _PS_OS_CANCELED_ and $id_order_state != _PS_OS_ERROR_) { if (($updateResult = Product::updateQuantity($product)) === false or $updateResult === -1) { $outOfStock = true; } if (!$outOfStock) { $product['stock_quantity'] -= $product['cart_quantity']; } Hook::updateQuantity($product, $order); } $price = Product::getPriceStatic(intval($product['id_product']), false, $product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL, 6, NULL, false, true, $product['cart_quantity'], false, intval($order->id_customer), intval($order->id_cart), intval($order->id_address_delivery)); $price_wt = Product::getPriceStatic(intval($product['id_product']), true, $product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL, 2, NULL, false, true, $product['cart_quantity'], false, intval($order->id_customer), intval($order->id_cart), intval($order->id_address_delivery)); // Add some informations for virtual products $deadline = '0000-00-00 00:00:00'; $download_hash = NULL; if ($id_product_download = ProductDownload::getIdFromIdProduct(intval($product['id_product']))) { $productDownload = new ProductDownload(intval($id_product_download)); $deadline = $productDownload->getDeadLine(); $download_hash = $productDownload->getHash(); } // Exclude VAT if (Tax::excludeTaxeOption()) { $product['tax'] = 0; $product['rate'] = 0; $tax = 0; } else { $tax = Tax::getApplicableTax(intval($product['id_tax']), floatval($product['rate']), intval($order->id_address_delivery)); } $currentDate = date('Y-m-d H:m:i'); if ($product['reduction_from'] != $product['reduction_to'] and ($currentDate > $product['reduction_to'] or $currentDate < $product['reduction_from'])) { $reduction_percent = 0.0; $reduction_amount = 0.0; } else { $reduction_percent = floatval($product['reduction_percent']); $reduction_amount = Tools::ps_round(floatval($product['reduction_price']) / (1 + floatval($tax) / 100), 6); } // Quantity discount $reduc = 0.0; if ($product['cart_quantity'] > 1 and $qtyD = QuantityDiscount::getDiscountFromQuantity($product['id_product'], $product['cart_quantity'])) { $reduc = QuantityDiscount::getValue($price_wt, $qtyD->id_discount_type, $qtyD->value, new Currency(intval($order->id_currency))); } $query .= '(' . intval($order->id) . ', ' . intval($product['id_product']) . ', ' . (isset($product['id_product_attribute']) ? intval($product['id_product_attribute']) : 'NULL') . ', \'' . pSQL($product['name'] . ((isset($product['attributes']) and $product['attributes'] != NULL) ? ' - ' . $product['attributes'] : '')) . '\', ' . intval($product['cart_quantity']) . ', ' . $quantityInStock . ', ' . floatval(Product::getPriceStatic(intval($product['id_product']), false, $product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL, Product::getTaxCalculationMethod(intval($order->id_customer)) == PS_TAX_EXC ? 2 : 6, NULL, false, false, $product['cart_quantity'], false, intval($order->id_customer), intval($order->id_cart), intval($order->id_address_delivery))) . ', ' . floatval($reduction_percent) . ', ' . floatval($reduction_amount) . ', ' . floatval($reduc) . ', ' . (empty($product['ean13']) ? 'NULL' : '\'' . pSQL($product['ean13']) . '\'') . ', ' . (empty($product['reference']) ? 'NULL' : '\'' . pSQL($product['reference']) . '\'') . ', ' . (empty($product['supplier_reference']) ? 'NULL' : '\'' . pSQL($product['supplier_reference']) . '\'') . ', ' . floatval($product['id_product_attribute'] ? $product['weight_attribute'] : $product['weight']) . ', \'' . (!$tax ? '' : pSQL($product['tax'])) . '\', ' . floatval($tax) . ', ' . floatval($product['ecotax']) . ', ' . (int) QuantityDiscount::getDiscountFromQuantity(intval($product['id_product']), intval($product['cart_quantity'])) . ', \'' . pSQL($deadline) . '\', \'' . pSQL($download_hash) . '\'),'; $priceWithTax = number_format($price * (($tax + 100) / 100), 2, '.', ''); $customizationQuantity = 0; if (isset($customizedDatas[$product['id_product']][$product['id_product_attribute']])) { $customizationText = ''; foreach ($customizedDatas[$product['id_product']][$product['id_product_attribute']] as $customization) { if (isset($customization['datas'][_CUSTOMIZE_TEXTFIELD_])) { foreach ($customization['datas'][_CUSTOMIZE_TEXTFIELD_] as $text) { $customizationText .= $text['name'] . $this->l(':') . ' ' . $text['value'] . ', '; } } } $customizationText = rtrim($customizationText, ', '); $customizationQuantity = intval($product['customizationQuantityTotal']); $productsList .= '<tr style="background-color: ' . ($key % 2 ? '#DDE2E6' : '#EBECEE') . ';"> <td style="padding: 0.6em 0.4em;">' . $product['reference'] . '</td> <td style="padding: 0.6em 0.4em;"><strong>' . $product['name'] . (isset($product['attributes_small']) ? ' ' . $product['attributes_small'] : '') . ' - ' . $this->l('Customized') . (!empty($customizationText) ? ' - ' . $customizationText : '') . '</strong></td> <td style="padding: 0.6em 0.4em; text-align: right;">' . Tools::displayPrice(Product::getTaxCalculationMethod() == PS_TAX_EXC ? $price : $price_wt, $currency, false, false) . '</td> <td style="padding: 0.6em 0.4em; text-align: center;">' . $customizationQuantity . '</td> <td style="padding: 0.6em 0.4em; text-align: right;">' . Tools::displayPrice($customizationQuantity * (Product::getTaxCalculationMethod() == PS_TAX_EXC ? $price : $price_wt), $currency, false, false) . '</td> </tr>'; } if (!$customizationQuantity or intval($product['cart_quantity']) > $customizationQuantity) { $productsList .= '<tr style="background-color: ' . ($key % 2 ? '#DDE2E6' : '#EBECEE') . ';"> <td style="padding: 0.6em 0.4em;">' . $product['reference'] . '</td> <td style="padding: 0.6em 0.4em;"><strong>' . $product['name'] . (isset($product['attributes_small']) ? ' ' . $product['attributes_small'] : '') . '</strong></td> <td style="padding: 0.6em 0.4em; text-align: right;">' . Tools::displayPrice(Product::getTaxCalculationMethod() == PS_TAX_EXC ? $price : $price_wt, $currency, false, false) . '</td> <td style="padding: 0.6em 0.4em; text-align: center;">' . (intval($product['cart_quantity']) - $customizationQuantity) . '</td> <td style="padding: 0.6em 0.4em; text-align: right;">' . Tools::displayPrice((intval($product['cart_quantity']) - $customizationQuantity) * (Product::getTaxCalculationMethod() == PS_TAX_EXC ? $price : $price_wt), $currency, false, false) . '</td> </tr>'; } } // end foreach ($products) $query = rtrim($query, ','); $result = $db->Execute($query); // Insert discounts from cart into order_discount table $discounts = $cart->getDiscounts(); $discountsList = ''; foreach ($discounts as $discount) { $objDiscount = new Discount(intval($discount['id_discount'])); $value = $objDiscount->getValue(sizeof($discounts), $cart->getOrderTotal(true, 1), $order->total_shipping, $cart->id); $order->addDiscount($objDiscount->id, $objDiscount->name, $value); if ($id_order_state != _PS_OS_ERROR_ and $id_order_state != _PS_OS_CANCELED_) { $objDiscount->quantity = $objDiscount->quantity - 1; } $objDiscount->update(); $discountsList .= '<tr style="background-color:#EBECEE;"> <td colspan="4" style="padding: 0.6em 0.4em; text-align: right;">' . $this->l('Voucher code:') . ' ' . $objDiscount->name . '</td> <td style="padding: 0.6em 0.4em; text-align: right;">-' . Tools::displayPrice($value, $currency, false, false) . '</td> </tr>'; } // Specify order id for message $oldMessage = Message::getMessageByCartId(intval($cart->id)); if ($oldMessage) { $message = new Message(intval($oldMessage['id_message'])); $message->id_order = intval($order->id); $message->update(); } // Hook new order $orderStatus = new OrderState(intval($id_order_state)); if (Validate::isLoadedObject($orderStatus)) { Hook::newOrder($cart, $order, $customer, $currency, $orderStatus); foreach ($cart->getProducts() as $product) { if ($orderStatus->logable) { ProductSale::addProductSale(intval($product['id_product']), intval($product['cart_quantity'])); } } } if (isset($outOfStock) and $outOfStock) { $history = new OrderHistory(); $history->id_order = intval($order->id); $history->changeIdOrderState(_PS_OS_OUTOFSTOCK_, intval($order->id)); $history->addWithemail(); } // Set order state in order history ONLY even if the "out of stock" status has not been yet reached // So you migth have two order states $new_history = new OrderHistory(); $new_history->id_order = intval($order->id); $new_history->changeIdOrderState(intval($id_order_state), intval($order->id)); $new_history->addWithemail(true, $extraVars); // Send an e-mail to customer if ($id_order_state != _PS_OS_ERROR_ and $id_order_state != _PS_OS_CANCELED_ and $customer->id) { $invoice = new Address(intval($order->id_address_invoice)); $delivery = new Address(intval($order->id_address_delivery)); $carrier = new Carrier(intval($order->id_carrier)); $delivery_state = $delivery->id_state ? new State(intval($delivery->id_state)) : false; $invoice_state = $invoice->id_state ? new State(intval($invoice->id_state)) : false; $data = array('{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{email}' => $customer->email, '{delivery_company}' => $delivery->company, '{delivery_firstname}' => $delivery->firstname, '{delivery_lastname}' => $delivery->lastname, '{delivery_address1}' => $delivery->address1, '{delivery_address2}' => $delivery->address2, '{delivery_city}' => $delivery->city, '{delivery_postal_code}' => $delivery->postcode, '{delivery_country}' => $delivery->country, '{delivery_state}' => $delivery->id_state ? $delivery_state->name : '', '{delivery_phone}' => $delivery->phone, '{delivery_other}' => $delivery->other, '{invoice_company}' => $invoice->company, '{invoice_firstname}' => $invoice->firstname, '{invoice_lastname}' => $invoice->lastname, '{invoice_address2}' => $invoice->address2, '{invoice_address1}' => $invoice->address1, '{invoice_city}' => $invoice->city, '{invoice_postal_code}' => $invoice->postcode, '{invoice_country}' => $invoice->country, '{invoice_state}' => $invoice->id_state ? $invoice_state->name : '', '{invoice_phone}' => $invoice->phone, '{invoice_other}' => $invoice->other, '{order_name}' => sprintf("#%06d", intval($order->id)), '{date}' => Tools::displayDate(date('Y-m-d H:i:s'), intval($order->id_lang), 1), '{carrier}' => strval($carrier->name) != '0' ? $carrier->name : Configuration::get('PS_SHOP_NAME'), '{payment}' => $order->payment, '{products}' => $productsList, '{discounts}' => $discountsList, '{total_paid}' => Tools::displayPrice($order->total_paid, $currency, false, false), '{total_products}' => Tools::displayPrice($order->total_paid - $order->total_shipping - $order->total_wrapping + $order->total_discounts, $currency, false, false), '{total_discounts}' => Tools::displayPrice($order->total_discounts, $currency, false, false), '{total_shipping}' => Tools::displayPrice($order->total_shipping, $currency, false, false), '{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $currency, false, false)); if (is_array($extraVars)) { $data = array_merge($data, $extraVars); } // Join PDF invoice if (intval(Configuration::get('PS_INVOICE')) and Validate::isLoadedObject($orderStatus) and $orderStatus->invoice and $order->invoice_number) { $fileAttachment['content'] = PDF::invoice($order, 'S'); $fileAttachment['name'] = Configuration::get('PS_INVOICE_PREFIX', intval($order->id_lang)) . sprintf('%06d', $order->invoice_number) . '.pdf'; $fileAttachment['mime'] = 'application/pdf'; } else { $fileAttachment = NULL; } if ($orderStatus->send_email and Validate::isEmail($customer->email)) { Mail::Send(intval($order->id_lang), 'order_conf', 'Order confirmation', $data, $customer->email, $customer->firstname . ' ' . $customer->lastname, NULL, NULL, $fileAttachment); } $this->currentOrder = intval($order->id); return true; } $this->currentOrder = intval($order->id); return true; } else { die(Tools::displayError('Order creation failed')); } } else { die(Tools::displayError('An order has already been placed using this cart')); } }
/** * Get product price * * @param integer $id_product Product id * @param boolean $tax With taxes or not (optional) * @param integer $id_product_attribute Product attribute id (optional) * @param integer $decimals Number of decimals (optional) * @param integer $divisor Useful when paying many time without fees (optional) * @return float Product price */ public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = NULL, $decimals = 6, $divisor = NULL, $only_reduc = false, $usereduc = true, $quantity = 1, $forceAssociatedTax = false, $id_customer = NULL, $id_cart = NULL, $id_address_delivery = NULL) { global $cookie, $cart; // Get id_customer if exists if (!$id_customer) { $id_customer = (Validate::isCookie($cookie) and isset($cookie->id_customer) and $cookie->id_customer) ? intval($cookie->id_customer) : null; } if (!is_object($cart)) { /* * When a user (e.g., guest, customer, Google...) is on PrestaShop, he has already its cart as the global (see /init.php) * When a non-user calls directly this method (e.g., payment module...) is on PrestaShop, he does not have already it BUT knows the cart ID */ if (!$id_cart and !Validate::isCookie($cookie)) { die(Tools::displayError()); } $cart = $id_cart ? new Cart(intval($id_cart)) : new Cart(intval($cookie->id_cart)); } if (Validate::isLoadedObject($cart)) { $currency = new Currency(intval($cart->id_currency)); } elseif (isset($cookie->id_currency) and intval($cookie->id_currency)) { $currency = new Currency(intval($cookie->id_currency)); } else { $currency = new Currency(intval(Configuration::get('PS_CURRENCY_DEFAULT'))); } if (!$id_address_delivery) { $id_address_delivery = $cart->id_address_delivery; } if (!Validate::isBool($usetax) or !Validate::isUnsignedId($id_product)) { die(Tools::displayError()); } if (Tax::excludeTaxeOption()) { $usetax = false; } // Caching system $cacheId = $id_product . '-' . ($usetax ? '1' : '0') . '-' . $id_product_attribute . '-' . $decimals . '-' . $divisor . '-' . ($only_reduc ? '1' : '0') . '-' . ($usereduc ? '1' : '0') . '-' . $quantity . '-' . ($id_customer ? $id_customer : '0'); if (isset(self::$_prices[$cacheId])) { return self::$_prices[$cacheId]; } // Getting price $result = Db::getInstance()->getRow(' SELECT p.`price`, p.`reduction_price`, p.`reduction_percent`, p.`reduction_from`, p.`reduction_to`, p.`id_tax`, t.`rate`, ' . ($id_product_attribute ? 'pa.`price`' : 'IFNULL((SELECT pa.price FROM `' . _DB_PREFIX_ . 'product_attribute` pa WHERE id_product = ' . intval($id_product) . ' AND default_on = 1), 0)') . ' AS attribute_price FROM `' . _DB_PREFIX_ . 'product` p ' . ($id_product_attribute ? 'LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON pa.`id_product_attribute` = ' . intval($id_product_attribute) : '') . ' LEFT JOIN `' . _DB_PREFIX_ . 'tax` AS t ON t.`id_tax` = p.`id_tax` WHERE p.`id_product` = ' . intval($id_product)); $price = Tools::convertPrice(floatval($result['price']), $currency); // Exclude tax $tax = floatval(Tax::getApplicableTax(intval($result['id_tax']), floatval($result['rate']), $id_address_delivery ? intval($id_address_delivery) : NULL)); if ($forceAssociatedTax) { $tax = floatval($result['rate']); } if (!$tax) { $usetax = false; } if ($usetax) { $price = $price * (1 + $tax / 100); } /* Keep this line */ // $price = Tools::ps_round($price, ($only_reduc OR $usereduc) ? 2 : $decimals); // Attribute price $attribute_price = Tools::convertPrice(array_key_exists('attribute_price', $result) ? floatval($result['attribute_price']) : 0, $currency); $attribute_price = $usetax ? Tools::ps_round($attribute_price, 2) : $attribute_price / (1 + ($tax ? $tax : $result['rate']) / 100); $price += $attribute_price; $reduc = Tools::convertPrice($result['reduction_price'], $currency); if ($only_reduc or $usereduc) { $reduc = self::getReductionValue($reduc, $result['reduction_percent'], $result['reduction_from'], $result['reduction_to'], Tools::ps_round($price, 2), $usetax, floatval($result['rate'])); } // Only reduction if ($only_reduc) { return $reduc; } // Reduction if ($usereduc) { if ($reduc) { $price = Tools::ps_round($price, 2); } $price -= $reduc; } if (intval($id_cart)) { $totalQuantity = intval(Db::getInstance()->getValue(' SELECT SUM(`quantity`) FROM `' . _DB_PREFIX_ . 'cart_product` WHERE `id_product` = ' . intval($id_product) . ' AND `id_cart` = ' . intval($id_cart))) + intval($quantity); } if ($quantity > 1 and $qtyD = QuantityDiscount::getDiscountFromQuantity($id_product, $quantity)) { $discount_qty_price = QuantityDiscount::getValue($price, $qtyD->id_discount_type, $qtyD->value, $usetax, floatval($result['rate']), $currency); $price -= $discount_qty_price; } // Group reduction $price *= (100 - Group::getReduction((isset($id_customer) and $id_customer) ? $id_customer : 0)) / 100; $price = ($divisor and $divisor != NULL) ? $price / $divisor : $price; if ($quantity <= 1 or !$qtyD) { $price = Tools::ps_round($price, $decimals); } self::$_prices[$cacheId] = $price; return self::$_prices[$cacheId]; }
/** * Get product price * * @param integer $id_product Product id * @param boolean $tax With taxes or not (optional) * @param integer $id_product_attribute Product attribute id (optional) * @param integer $decimals Number of decimals (optional) * @param integer $divisor Useful when paying many time without fees (optional) * @return float Product price */ public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = NULL, $decimals = 6, $divisor = NULL, $only_reduc = false, $usereduc = true, $quantity = 1, $forceAssociatedTax = false, $id_customer = NULL, $id_cart = NULL, $id_address_delivery = NULL) { global $cookie; // Get id_customer if exists if (!$id_customer) { $id_customer = (isset($cookie) and get_class($cookie) == 'Cookie' and isset($cookie->id_customer) and $cookie->id_customer) ? intval($cookie->id_customer) : null; } if (!$id_cart) { $id_cart = (isset($cookie) and get_class($cookie) == 'Cookie' and isset($cookie->id_cart) and $cookie->id_cart) ? intval($cookie->id_cart) : null; } if (!$id_address_delivery) { $id_address_delivery = (isset($cookie) and get_class($cookie) == 'Cookie' and isset($cookie->id_address_delivery) and $cookie->id_address_delivery) ? intval($cookie->id_address_delivery) : null; } if (!Validate::isBool($usetax) or !Validate::isUnsignedId($id_product)) { die(Tools::displayError()); } // Caching system $cacheId = $id_product . '-' . ($usetax ? '1' : '0') . '-' . $id_product_attribute . '-' . $decimals . '-' . $divisor . '-' . ($only_reduc ? '1' : '0') . '-' . ($usereduc ? '1' : '0') . '-' . $quantity; if (isset(self::$_prices[$cacheId])) { return self::$_prices[$cacheId]; } // Getting price $result = Db::getInstance()->getRow(' SELECT p.`price`, p.`reduction_price`, p.`reduction_percent`, p.`reduction_from`, p.`reduction_to`, p.`id_tax`, t.`rate`, ' . ($id_product_attribute ? 'pa.`price`' : 'IFNULL((SELECT pa.price FROM `' . _DB_PREFIX_ . 'product_attribute` pa WHERE id_product = ' . intval($id_product) . ' AND default_on = 1), 0)') . ' AS attribute_price FROM `' . _DB_PREFIX_ . 'product` p ' . ($id_product_attribute ? 'LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON pa.`id_product_attribute` = ' . intval($id_product_attribute) : '') . ' LEFT JOIN `' . _DB_PREFIX_ . 'tax` AS t ON t.`id_tax` = p.`id_tax` WHERE p.`id_product` = ' . intval($id_product)); $price = Tools::convertPrice(floatval($result['price'])); // Exclude tax $tax = floatval(Tax::getApplicableTax(intval($result['id_tax']), floatval($result['rate']), $id_address_delivery ? intval($id_address_delivery) : NULL)); if ($forceAssociatedTax) { $tax = floatval($result['rate']); } if (Tax::excludeTaxeOption() or !$tax) { $usetax = false; } if ($usetax) { $price = $price * (1 + $tax / 100); } // Attribute price $attribute_price = Tools::convertPrice(array_key_exists('attribute_price', $result) ? floatval($result['attribute_price']) : 0); $attribute_price = $usetax ? Tools::ps_round($attribute_price, 2) : $attribute_price / (1 + ($tax ? $tax : $result['rate']) / 100); $price += $attribute_price; if ($only_reduc or $usereduc) { $reduc = self::getReductionValue($result['reduction_price'], $result['reduction_percent'], $result['reduction_from'], $result['reduction_to'], $price, $usetax, floatval($result['rate'])); } // Only reduction if ($only_reduc) { return $reduc; } // Reduction if ($usereduc) { $price -= $reduc; } // Quantity discount if (intval($id_cart)) { $totalQuantity = intval(Db::getInstance()->getValue(' SELECT SUM(`quantity`) FROM `' . _DB_PREFIX_ . 'cart_product` WHERE `id_product` = ' . intval($id_product) . ' AND `id_cart` = ' . intval($cookie->id_cart))) + intval($quantity); } if ($quantity > 1 and $qtyD = QuantityDiscount::getDiscountFromQuantity($id_product, $quantity)) { $discount_qty_price = QuantityDiscount::getValue($price, $qtyD->id_discount_type, $qtyD->value); $price -= $discount_qty_price; } // Group reduction if ($id_customer) { $price *= (100 - Group::getReduction($id_customer)) / 100; } $price = ($divisor and $divisor != NULL) ? $price / $divisor : $price; if ($quantity <= 1 or !$qtyD) { $price = Tools::ps_round($price, $decimals); } self::$_prices[$cacheId] = $price; return self::$_prices[$cacheId]; }
/** * Get product price * * @param integer $id_product Product id * @param boolean $tax With taxes or not (optional) * @param integer $id_product_attribute Product attribute id (optional) * @param integer $decimals Number of decimals (optional) * @param integer $divisor Util when paying many time without fees (optional) * @return float Product price */ public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = NULL, $decimals = 6, $divisor = NULL, $only_reduc = false, $usereduc = true, $quantity = 1, $forceAssociatedTax = false) { global $cookie; // Get id_customer if exists $id_customer = (isset($cookie) and get_class($cookie) == 'Cookie' and isset($cookie->id_customer) and $cookie->id_customer) ? intval($cookie->id_customer) : null; if (!Validate::isBool($usetax) or !Validate::isUnsignedId($id_product)) { die(Tools::displayError()); } // Caching system $cacheId = $id_product . '-' . ($usetax ? '1' : '0') . '-' . $id_product_attribute . '-' . $decimals . '-' . $divisor . '-' . ($only_reduc ? '1' : '0') . '-' . ($usereduc ? '1' : '0') . '-' . $quantity; if (isset(self::$_prices[$cacheId])) { return self::$_prices[$cacheId]; } // Getting price $result = Db::getInstance()->getRow(' SELECT p.`price`, p.`reduction_price`, p.`reduction_percent`, p.`reduction_from`, p.`reduction_to`, p.`id_tax`, t.`rate`, ' . ($id_product_attribute ? 'pa.`price`' : 'IFNULL((SELECT pa.price FROM `' . _DB_PREFIX_ . 'product_attribute` pa WHERE id_product = ' . intval($id_product) . ' AND default_on = 1), 0)') . ' AS attribute_price FROM `' . _DB_PREFIX_ . 'product` p ' . ($id_product_attribute ? 'LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON pa.`id_product_attribute` = ' . intval($id_product_attribute) : '') . ' LEFT JOIN `' . _DB_PREFIX_ . 'tax` AS t ON t.`id_tax` = p.`id_tax` WHERE p.`id_product` = ' . intval($id_product)); $price = $result['price']; // Exclude tax $tax = floatval(Tax::getApplicableTax(intval($result['id_tax']), floatval($result['rate']))); if ($forceAssociatedTax) { $tax = floatval($result['rate']); } if (Tax::excludeTaxeOption() or !$tax) { $usetax = false; } if ($usetax) { $price *= 1 + $tax / 100; } // Attribute price $attribute_price = $usetax ? $result['attribute_price'] : $result['attribute_price'] / (1 + ($tax ? $tax : $result['rate']) / 100); if (isset($result['attribute_price'])) { $price += $attribute_price; } $reduc = self::getReductionValue($result['reduction_price'], $result['reduction_percent'], $result['reduction_from'], $result['reduction_to'], $price, $usetax, floatval($result['rate'])); // Only reduction if ($only_reduc) { return $reduc; } // Reduction if ($usereduc) { $price -= $reduc; } // Quantity discount if ($quantity > 1 and $qtyD = QuantityDiscount::getDiscountFromQuantity($id_product, $quantity)) { $price -= QuantityDiscount::getValue($price, $qtyD->id_discount_type, $qtyD->value); } // Group reduction if ($id_customer) { $price *= (100 - Group::getReduction($id_customer)) / 100; } self::$_prices[$cacheId] = ($divisor and $divisor != 'NULL') ? number_format($price / $divisor, $decimals, '.', '') : number_format($price, $decimals, '.', ''); return self::$_prices[$cacheId]; }
/** * Get product price * * @param integer $id_product Product id * @param boolean $tax With taxes or not (optional) * @param integer $id_product_attribute Product attribute id (optional) * @param integer $decimals Number of decimals (optional) * @param integer $divisor Util when paying many time without fees (optional) * @return float Product price */ public static function getPriceStaticLC($id_product, $usetax = true, $id_product_attribute = NULL, $decimals = 6, $divisor = NULL, $only_reduc = false, $usereduc = true, $quantity = 1, $forceAssociatedTax = false) { global $cookie, $currency; // Get id_customer if exists $id_customer = (isset($cookie) and get_class($cookie) == 'Cookie' and isset($cookie->id_customer) and $cookie->id_customer) ? intval($cookie->id_customer) : null; if (!Validate::isBool($usetax) or !Validate::isUnsignedId($id_product)) { die(Tools::displayError()); } // Caching system $currency_id = ''; if ($currency) { $currency_id = $currency->id; } $cacheId = $currency_id . '-' . $id_product . '-' . ($usetax ? '1' : '0') . '-' . $id_product_attribute . '-' . $decimals . '-' . $divisor . '-' . ($only_reduc ? '1' : '0') . '-' . ($usereduc ? '1' : '0') . '-' . $quantity; if (isset(self::$_prices[$cacheId])) { return self::$_prices[$cacheId]; } // Getting price $prices = self::getBasePriceStaticLC($id_product, $id_product_attribute); $price = $prices['price']; // Exclude tax $tax = floatval(Tax::getApplicableTax(intval($prices['id_tax']), floatval($prices['rate']))); if ($forceAssociatedTax) { $tax = floatval($prices['rate']); } if (Tax::excludeTaxeOption() or !$tax) { $usetax = false; } if ($usetax) { $price *= 1 + $tax / 100; } // Attribute price $attribute_price = $usetax ? $prices['attribute_price'] : $prices['attribute_price'] / (1 + ($tax ? $tax : $prices['rate']) / 100); if (isset($prices['attribute_price'])) { $price += $attribute_price; } $reduc = self::getReductionValue($prices['reduction_price'], $prices['reduction_percent'], $prices['reduction_from'], $prices['reduction_to'], $price, $usetax, floatval($prices['rate'])); // Only reduction if ($only_reduc) { return $reduc; } // Reduction if ($usereduc) { $price -= $reduc; } // Quantity discount if ($quantity > 1 and $qtyD = QuantityDiscount::getDiscountFromQuantity($id_product, $quantity)) { $price -= QuantityDiscount::getValue($price, $qtyD->id_discount_type, $qtyD->value); } // * $currency->conversion_rate; // Group reduction if ($id_customer) { $price *= (100 - Group::getReduction($id_customer)) / 100; } self::$_prices[$cacheId] = ($divisor and $divisor != 'NULL') ? number_format($price / $divisor, $decimals, '.', '') : number_format($price, $decimals, '.', ''); return self::$_prices[$cacheId]; }