$h_myshop_products->increaseStock($product); } if (isset($_GET['stock']) && $_GET['stock'] == 'substract' && myshop_utils::isMemberOfGroup(myshop_utils::getModuleOption('grp_qty'))) { $h_myshop_products->decreaseStock($product); $h_myshop_products->verifyLowStock($product); } $currentUser = myshop_utils::getCurrentUserID(); $baseurl = MYSHOP_URL . basename(__FILE__) . '?product_id=' . $product->getVar('product_id'); // Options for template $xoopsTpl->assign('baseurl', $baseurl); $xoopsTpl->assign('nostock_msg', myshop_utils::getModuleOption('nostock_msg')); $xoopsTpl->assign('mod_pref', $mod_pref); $xoopsTpl->assign('icones', $icones); $xoopsTpl->assign('canRateProducts', myshop_utils::getModuleOption('rateproducts')); $xoopsTpl->assign('mail_link', 'mailto:?subject=' . sprintf(_MYSHOP_INTARTICLE, $xoopsConfig['sitename']) . '&body=' . sprintf(_MYSHOP_INTARTFOUND, $xoopsConfig['sitename']) . ': ' . XOOPS_URL . '/modules/myshop/product.php?product_id=' . $product_id); $xoopsTpl->assign('canChangeQuantity', myshop_utils::isMemberOfGroup(myshop_utils::getModuleOption('grp_qty'))); // Group with permissions to change product quantity $xoopsTpl->assign('ProductStockQuantity', sprintf(_MYSHOP_QUANTITY_STOCK, $product->getVar('product_stock'))); // Search Category of Product $tbl_tmp = $tbl_categories = $tbl_ancestors = array(); $tbl_categories = $h_myshop_cat->getAllCategories(); $product_category = null; $product_category = isset($tbl_categories[$product->getVar('product_cid')]) ? $tbl_categories[$product->getVar('product_cid')] : null; if (!is_object($product_category)) { myshop_utils::redirect(_MYSHOP_ERROR4, 'index.php', 5); } // store $product_store = null; $product_store = $h_myshop_stores->get($product->getVar('product_store_id')); if (!is_object($product_store)) { myshop_utils::redirect(_MYSHOP_ERROR5, 'index.php', 5);
/** * Calculate cart order * Content of cart session : * * $datas['number'] = product ref. * $datas['id'] = product id. * $datas['qty'] = quantity * * Private var of $cart is similar + object 'myshop_products' in key 'product' * * @param array $cartForTemplate * @param boolean emptyCart * @param float $shippingAmount * @param float $commandAmount * @param float $vatAmount * @param string $goOn Link to last visited category * @param float $commandAmountTTC Amount with All Fee * @param array $discountsDescription * @param integer $discountsCount */ function computeCart(&$cartForTemplate, &$emptyCart, &$shippingAmount, &$commandAmount, &$vatAmount, &$goOn, &$commandAmountTTC, &$discountsDescription, &$discountsCount) { $emptyCart = false; $goOn = ''; $vats = array(); $cpt = 0; $discountsCount = 0; $this->cart = isset($_SESSION[MyshopMyshop_caddyHandler::CADDY_NAME]) ? $_SESSION[MyshopMyshop_caddyHandler::CADDY_NAME] : array(); $cartCount = count($this->cart); if ($cartCount < 0) { $emptyCart = true; return true; } // Initialize Private Data $this->initializePrivateData(); // Load related products and calculate categories quantity $this->loadProductsAssociatedToCart(); // VAT $vats = $this->handlers->h_myshop_vat->getAllVats(); $myshop_Currency =& myshop_Currency::getInstance(); $caddyCount = count($this->cart); // Initialize total of ht=without VAT, tva= VAT, and shipping $totalHT = $totalVAT = $totalShipping = 0; // Check each product and discount rule foreach ($this->cart as $cartProduct) { if ($cartProduct['product']->getVar('product_discount_price') > 0) { $ht = floatval($cartProduct['product']->getVar('product_discount_price')); } else { $ht = floatval($cartProduct['product']->getVar('product_price')); } $discountedPrice = $ht; $quantity = $cartProduct['qty']; if (myshop_utils::getModuleOption('shipping_quantity')) { $discountedShipping = $cartProduct['product']->getVar('product_shipping_price') * $quantity; } else { $discountedShipping = $cartProduct['product']->getVar('product_shipping_price'); } $totalPrice = 0; $reduction = ''; $cpt++; if ($cpt == $caddyCount) { $category = null; $category = $this->handlers->h_myshop_cat->get($cartProduct['product']->getVar('product_cid')); if (is_object($category)) { $goOn = $category->getLink(); } } // Check All Active Rules foreach ($this->allActiveRules as $rule) { $applyRule = false; if ($rule->disc_group != 0 && myshop_utils::isMemberOfGroup($rule->disc_group) || $rule->disc_group == 0) { if ($rule->disc_cat_cid != 0 && $cartProduct['product']->getVar('product_cid') == $rule->disc_cat_cid || $rule->disc_cat_cid == 0) { if ($rule->disc_store_id != 0 && $cartProduct['product']->getVar('disc_store_id') == $rule->disc_store_id || $rule->disc_store_id == 0) { if ($rule->disc_product_id != 0 && $cartProduct['product']->getVar('product_id') == $rule->disc_product_id || $rule->disc_product_id == 0) { // When to apply switch ($rule->disc_price_case) { case MYSHOP_DISCOUNT_PRICE_CASE_ALL: // All $applyRule = true; break; case MYSHOP_DISCOUNT_PRICE_CASE_FIRST_BUY: // Fist Order if ($this->handlers->h_myshop_commands->isFirstCommand()) { $applyRule = true; } break; case MYSHOP_DISCOUNT_PRICE_CASE_PRODUCT_NEVER: // Firs time user buy the product if (!$this->handlers->h_myshop_commands->productAlreadyBought(0, $cartProduct['product']->getVar('product_id'))) { $applyRule = true; } break; case MYSHOP_DISCOUNT_PRICE_CASE_QTY_IS: // if quantity of product is... switch ($rule->disc_price_case_qty_cond) { case MYSHOP_DISCOUNT_PRICE_QTY_COND1: // > if ($cartProduct['qty'] > $rule->disc_price_case_qty_value) { $applyRule = true; } break; case MYSHOP_DISCOUNT_PRICE_QTY_COND2: // >= if ($cartProduct['qty'] >= $rule->disc_price_case_qty_value) { $applyRule = true; } break; case MYSHOP_DISCOUNT_PRICE_QTY_COND3: // < if ($cartProduct['qty'] < $rule->disc_price_case_qty_value) { $applyRule = true; } break; case MYSHOP_DISCOUNT_PRICE_QTY_COND4: // <= if ($cartProduct['qty'] <= $rule->disc_price_case_qty_value) { $applyRule = true; } break; case MYSHOP_DISCOUNT_PRICE_QTY_COND5: // == if ($cartProduct['qty'] == $rule->disc_price_case_qty_value) { $applyRule = true; } break; } } } } } } if ($applyRule) { // Apply rule // Calculate product price without fee switch ($rule->disc_price_type) { case MYSHOP_DISCOUNT_PRICE_TYPE1: // Amount related to quantity if ($quantity >= $rule->disc_price_degress_l1qty1 && $quantity <= $rule->disc_price_degress_l1qty2) { $discountedPrice = $rule->disc_price_degress_l1total; } if ($quantity >= $rule->disc_price_degress_l2qty1 && $quantity <= $rule->disc_price_degress_l2qty2) { $discountedPrice = $rule->disc_price_degress_l2total; } if ($quantity >= $rule->disc_price_degress_l3qty1 && $quantity <= $rule->disc_price_degress_l3qty2) { $discountedPrice = $rule->disc_price_degress_l3total; } if ($quantity >= $rule->disc_price_degress_l4qty1 && $quantity <= $rule->disc_price_degress_l4qty2) { $discountedPrice = $rule->disc_price_degress_l4total; } if ($quantity >= $rule->disc_price_degress_l5qty1 && $quantity <= $rule->disc_price_degress_l5qty2) { $discountedPrice = $rule->disc_price_degress_l5total; } $reduction = $rule->disc_description; $discountsCount++; break; case MYSHOP_DISCOUNT_PRICE_TYPE2: // Discount amount or percentage if ($rule->disc_price_amount_on == MYSHOP_DISCOUNT_PRICE_AMOUNT_ON_PRODUCT) { // Discount on product if ($rule->disc_price_amount_type == MYSHOP_DISCOUNT_PRICE_REDUCE_PERCENT) { // Discount percentage $discountedPrice = $this->getDiscountedPrice($discountedPrice, $rule->getVar('disc_price_amount_amount')); } elseif ($rule->disc_price_amount_type == MYSHOP_DISCOUNT_PRICE_REDUCE_MONEY) { // Discount amount $discountedPrice -= floatval($rule->getVar('disc_price_amount_amount')); } // No negative value if ($discountedPrice < 0) { $discountedPrice = 0; } $reduction = $rule->disc_description; $discountsCount++; } elseif ($rule->disc_price_amount_on == MYSHOP_DISCOUNT_PRICE_AMOUNT_ON_CART) { // Rule to apply to cart if (!isset($this->rulesForTheWhole[$rule->disc_id])) { $this->rulesForTheWhole[$rule->disc_id] = $rule; } } break; } // Shipping Fee switch ($rule->disc_shipping_type) { case MYSHOP_DISCOUNT_SHIPPING_TYPE1: // Fee break; case MYSHOP_DISCOUNT_SHIPPING_TYPE2: // Free if user order is more than X euros if ($this->totalAmountBeforeDiscounts > $rule->disc_shipping_free_morethan) { $discountedShipping = 0; } break; case MYSHOP_DISCOUNT_SHIPPING_TYPE3: // Discount on shipping fee of X euros if order is > x if ($this->totalAmountBeforeDiscounts > $rule->disc_shipping_reduce_cartamount) { $discountedShipping -= floatval($rule->disc_shipping_reduce_amount); } // No negative value if ($discountedShipping < 0) { $discountedShipping = 0; } break; case MYSHOP_DISCOUNT_SHIPPING_TYPE4: // Shipping fee degressive if ($quantity >= $rule->disc_shipping_degress_l1qty1 && $quantity <= $rule->disc_shipping_degress_l1qty2) { $discountedShipping = $rule->disc_shipping_degress_l1total; } if ($quantity >= $rule->disc_shipping_degress_l2qty1 && $quantity <= $rule->disc_shipping_degress_l2qty2) { $discountedShipping = $rule->disc_shipping_degress_l2total; } if ($quantity >= $rule->disc_shipping_degress_l3qty1 && $quantity <= $rule->disc_shipping_degress_l3qty2) { $discountedShipping = $rule->disc_shipping_degress_l3total; } if ($quantity >= $rule->disc_shipping_degress_l4qty1 && $quantity <= $rule->disc_shipping_degress_l4qty2) { $discountedShipping = $rule->disc_shipping_degress_l4total; } if ($quantity >= $rule->disc_shipping_degress_l5qty1 && $quantity <= $rule->disc_shipping_degress_l5qty2) { $discountedShipping = $rule->disc_shipping_degress_l5total; } break; } // Discount rule for shipping fee } // Apply discount rule } // Check discounts // Calculate product VAT $vatId = $cartProduct['product']->getVar('product_vat_id'); $vatRate = $vats[$vatId]->getVar('vat_rate'); $vatAmount = myshop_utils::getVAT($discountedPrice * $quantity, $vatRate); // Calculate product all fee ((ht * qte) + vat + shipping) $totalPrice = $discountedPrice * $quantity + $vatAmount + $discountedShipping; // Total $totalHT += $discountedPrice * $quantity; $totalVAT += $vatAmount; $totalShipping += $discountedShipping; // Product related $associatedStore = $associatedCategory = $associatedManufacturers = array(); $manufacturersJoinList = ''; // store if (isset($this->associatedStores[$cartProduct['product']->product_store_id])) { $associatedStore = $this->associatedStores[$cartProduct['product']->product_store_id]->toArray(); } // Catéegory if (isset($this->associatedCategories[$cartProduct['product']->product_cid])) { $associatedCategory = $this->associatedCategories[$cartProduct['product']->product_cid]->toArray(); } // Manufacturers $product_id = $cartProduct['product']->product_id; if (isset($this->associatedManufacturersPerProduct[$product_id])) { $manufacturers = $this->associatedManufacturersPerProduct[$product_id]; $manufacturersList = array(); foreach ($manufacturers as $manufacturer_id) { if (isset($this->associatedManufacturers[$manufacturer_id])) { $associatedManufacturers[] = $this->associatedManufacturers[$manufacturer_id]->toArray(); } $manufacturersList[] = $this->associatedManufacturers[$manufacturer_id]->manu_commercialname . ' ' . $this->associatedManufacturers[$manufacturer_id]->manu_name; } $manufacturersJoinList = implode(MYSHOP_STRING_TO_JOIN_MANUFACTURERS, $manufacturersList); } $productTemplate = array(); $productTemplate = $cartProduct['product']->toArray(); $productTemplate['number'] = $cartProduct['number']; $productTemplate['id'] = $cartProduct['id']; $productTemplate['product_qty'] = $cartProduct['qty']; $productTemplate['unitBasePrice'] = $ht; // Unity price HT (without vat) WITHOUT discount réduction $productTemplate['discountedPrice'] = $discountedPrice; // Unity price HT (without vat) WITH discount $productTemplate['discountedPriceWithQuantity'] = $discountedPrice * $quantity; // Price HT (without vat) WITH discount and quantity // Formated prices $productTemplate['unitBasePriceFormated'] = $myshop_Currency->amountForDisplay($ht); $productTemplate['discountedPriceFormated'] = $myshop_Currency->amountForDisplay($discountedPrice); $productTemplate['discountedPriceWithQuantityFormated'] = $myshop_Currency->amountForDisplay($discountedPrice * $quantity); $productTemplate['vatRate'] = $myshop_Currency->amountInCurrency($vatRate); $productTemplate['vatAmount'] = $vatAmount; $productTemplate['normalShipping'] = $cartProduct['product']->getVar('product_shipping_price'); $productTemplate['discountedShipping'] = $discountedShipping; $productTemplate['totalPrice'] = $totalPrice; $productTemplate['reduction'] = $reduction; $productTemplate['templateProduct'] = $cartProduct['product']->toArray(); $productTemplate['vatAmountFormated'] = $myshop_Currency->amountInCurrency($vatAmount); $productTemplate['normalShippingFormated'] = $myshop_Currency->amountForDisplay($cartProduct['product']->getVar('product_shipping_price')); $productTemplate['discountedShippingFormated'] = $myshop_Currency->amountForDisplay($discountedShipping); $productTemplate['totalPriceFormated'] = $myshop_Currency->amountForDisplay($totalPrice); $productTemplate['templateCategory'] = $associatedCategory; $productTemplate['templateStore'] = $associatedStore; $productTemplate['templateManufacturers'] = $associatedManufacturers; $productTemplate['manufacturersJoinList'] = $manufacturersJoinList; $this->cartForTemplate[] = $productTemplate; } // foreach product in cart // Rules if any if (count($this->rulesForTheWhole) > 0) { // $discountsDescription foreach ($this->rulesForTheWhole as $rule) { switch ($rule->disc_price_type) { case MYSHOP_DISCOUNT_PRICE_TYPE2: // Amount or percentage if ($rule->disc_price_amount_on == MYSHOP_DISCOUNT_PRICE_AMOUNT_ON_CART) { // Rule to apply to cart if ($rule->disc_price_amount_type == MYSHOP_DISCOUNT_PRICE_REDUCE_PERCENT) { // Discount pourcentage $totalHT = $this->getDiscountedPrice($totalHT, $rule->getVar('disc_price_amount_amount')); $totalVAT = $this->getDiscountedPrice($totalVAT, $rule->getVar('disc_price_amount_amount')); } elseif ($rule->disc_price_amount_type == MYSHOP_DISCOUNT_PRICE_REDUCE_MONEY) { // Discount amount $totalHT -= floatval($rule->getVar('disc_price_amount_amount')); $totalVAT -= floatval($rule->getVar('disc_price_amount_amount')); } // No negative value if ($totalHT < 0) { $totalHT = 0; } if ($totalVAT < 0) { $totalVAT = 0; } $discountsDescription[] = $rule->disc_description; $discountsCount++; } // Rule to apply to cart break; } // Switch } // Foreach } // if any global rule // return total $shippingAmount = $totalShipping; $commandAmount = $totalHT; $vatAmount = $totalVAT; $commandAmountTTC = $totalHT + $totalVAT + $totalShipping; $cartForTemplate = $this->cartForTemplate; $emptyCart = false; return true; }