public static function createOrderDiscount($order, $productList, $qtyList, $name, $shipping_cost = false, $id_category = 0, $subcategory = 0) { $languages = Language::getLanguages($order); $products = $order->getProducts(false, $productList, $qtyList); // Totals are stored in the order currency (or at least should be) $total = $order->getTotalProductsWithTaxes($products); $discounts = $order->getDiscounts(true); $total_tmp = $total; foreach ($discounts as $discount) { if ($discount['id_discount_type'] == 1) { $total -= $total_tmp * ($discount['value'] / 100); } elseif ($discount['id_discount_type'] == 2) { $total -= $discount['value'] * ($total_tmp / $order->total_products_wt); } } if ($shipping_cost) { $total += $order->total_shipping; } // create discount $voucher = new Discount(); $voucher->id_discount_type = 2; foreach ($languages as $language) { $voucher->description[$language['id_lang']] = strval($name) . (int) $order->id; } $voucher->value = (double) $total; $voucher->name = 'V0C' . (int) $order->id_customer . 'O' . (int) $order->id; $voucher->id_customer = (int) $order->id_customer; $voucher->id_currency = (int) $order->id_currency; $voucher->quantity = 1; $voucher->quantity_per_user = 1; $voucher->cumulable = 1; $voucher->cumulable_reduction = 1; $voucher->minimal = (double) $voucher->value; $voucher->active = 1; $voucher->cart_display = 1; $now = time(); $voucher->date_from = date('Y-m-d H:i:s', $now); $voucher->date_to = date('Y-m-d H:i:s', $now + 3600 * 24 * 365.25); /* 1 year */ if (!$voucher->validateFieldsLang(false) or !$voucher->add()) { return false; } // set correct name $voucher->name = 'V' . (int) $voucher->id . 'C' . (int) $order->id_customer . 'O' . $order->id; if (!$voucher->update()) { return false; } return $voucher; }
public static function createOrderDiscount($order, $productList, $qtyList, $name, $shipping_cost = false, $id_category = 0, $subcategory = 0) { $languages = Language::getLanguages($order); $products = $order->getProducts(false, $productList, $qtyList); $total = $order->getTotalProductsWithTaxes($products); if ($shipping_cost) { $total += $order->total_shipping; } // create discount $voucher = new Discount(); $voucher->id_discount_type = 2; foreach ($languages as $language) { $voucher->description[$language['id_lang']] = strval($name) . intval($order->id); } $voucher->value = floatval($total); $voucher->name = 'V0C' . intval($order->id_customer) . 'O' . intval($order->id); $voucher->id_customer = intval($order->id_customer); $voucher->quantity = 1; $voucher->quantity_per_user = 1; $voucher->cumulable = 1; $voucher->cumulable_reduction = 1; $voucher->minimal = floatval($voucher->value); $voucher->active = 1; $now = time(); $voucher->date_from = date('Y-m-d H:i:s', $now); $voucher->date_to = date('Y-m-d H:i:s', $now + 60 * 60 * 24 * 184); if (!$voucher->validateFieldsLang(false) or !$voucher->add()) { return false; } // set correct name $voucher->name = 'V' . intval($voucher->id) . 'C' . intval($order->id_customer) . 'O' . $order->id; if (!$voucher->update()) { return false; } return $voucher; }
} $cart->addCartRule($voucher->id); } else { $voucher = new Discount(); $voucher->id_discount_type = 2; $voucher->value = $amount; $languages = Language::getLanguages(true); $voucher->name = 'Fid\'Bag'; $voucher->description = 'Discount Fid\'Bag'; $voucher->id_customer = (int) $cart->id_customer; $voucher->id_currency = (int) $cart->id_currency; $voucher->quantity = 1; $voucher->quantity_per_user = 1; $voucher->cumulable = 1; $voucher->cumulable_reduction = 1; $voucher->minimal = (double) $voucher->value; $voucher->include_tax = 1; $voucher->active = 1; $voucher->cart_display = 1; $now = time(); $voucher->date_from = date('Y-m-d H:i:s', $now); $voucher->date_to = date('Y-m-d H:i:s', $now + 3600 * 24 * 365.25); if (!$voucher->validateFieldsLang(false) || !$voucher->add()) { die('0'); } $cart->addDiscount($voucher->id); Discount::getVouchersToCartDisplay(1, $cart->id_customer); } } $values = array('total' => Tools::ps_round($cart->getOrderTotal(), $currency->decimals), 'discount' => $amount); die(Tools::jsonEncode($values));
public static function addCoupons($idCustomer) { global $cookie; if ((int) $cookie->id_country === 110) { $vouchers = array("6" => array('INR 300 off on order over INR 1500', Tools::ps_round(Tools::convertPrice(300, Currency::getCurrencyInstance(4), false)), Tools::ps_round(Tools::convertPrice(1500, Currency::getCurrencyInstance(4), false)), 3600 * 24 * 30 * 1), "7" => array('INR 700 off on order over INR 3000', Tools::ps_round(Tools::convertPrice(700, Currency::getCurrencyInstance(4), false)), Tools::ps_round(Tools::convertPrice(3000, Currency::getCurrencyInstance(4), false)), 3600 * 24 * 30 * 1), "8" => array('INR 1500 off on order over INR 5000', Tools::ps_round(Tools::convertPrice(1500, Currency::getCurrencyInstance(4), false)), Tools::ps_round(Tools::convertPrice(5000, Currency::getCurrencyInstance(4), false)), 3600 * 24 * 30 * 1)); } else { $vouchers = array("6" => array('USD 15 off on order over USD 100', 15, 100.0, 3600 * 24 * 30 * 1), "7" => array('USD 30 off on order over USD 200', 30, 200.0, 3600 * 24 * 30 * 2), "8" => array('USD 55 off on order over USD 300', 55, 300.0, 3600 * 24 * 30 * 3)); } foreach ($vouchers as $idCoupon => $coupon) { $couponCode = "" . $idCustomer . $idCoupon . Tools::rand_string(3); // create discount $languages = Language::getLanguages($order); $voucher = new Discount(); $voucher->id_discount_type = 2; foreach ($languages as $language) { $voucher->description[$language['id_lang']] = $coupon[0]; } // coupon description $voucher->value = (double) $coupon[1]; // coupon value $voucher->name = $couponCode; $voucher->id_customer = $idCustomer; $voucher->id_currency = 2; //USD $voucher->quantity = 1; $voucher->quantity_per_user = 1; $voucher->cumulable = 0; $voucher->cumulable_reduction = 1; $voucher->minimal = (double) $coupon[2]; // min order value $voucher->active = 1; $voucher->cart_display = 1; $now = time(); $voucher->date_from = date('Y-m-d H:i:s', $now); $voucher->date_to = date('Y-m-d H:i:s', $now + $coupon[3]); //validity if (!$voucher->validateFieldsLang(false) or !$voucher->add()) { return false; } } }