[+] Project : Vouchers are now replaced by cart rules
git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9771 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
@@ -296,7 +296,7 @@ class AdminControllerCore extends Controller
|
||||
if (!empty($action) && method_exists($this, 'ajaxProcess'.Tools::toCamelCase($action)))
|
||||
$this->{'ajaxProcess'.Tools::toCamelCase($action)}();
|
||||
else if (method_exists($this, 'ajaxProcess'))
|
||||
$this->ajaxProcess();
|
||||
$this->ajaxProcess();
|
||||
|
||||
// @TODO We should use a displayAjaxError
|
||||
/*$this->displayErrors();
|
||||
@@ -1104,7 +1104,7 @@ class AdminControllerCore extends Controller
|
||||
'content' => $this->content,
|
||||
'url_post' => self::$currentIndex.'&token='.$this->token,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize the invalid doom page of death
|
||||
@@ -1703,7 +1703,7 @@ class AdminControllerCore extends Controller
|
||||
* @param integer $id_lang Language id (optional)
|
||||
* @return string
|
||||
*/
|
||||
protected function getFieldValue($obj, $key, $id_lang = null)
|
||||
public function getFieldValue($obj, $key, $id_lang = null)
|
||||
{
|
||||
if ($id_lang)
|
||||
$default_value = ($obj->id && isset($obj->{$key}[$id_lang])) ? $obj->{$key}[$id_lang] : '';
|
||||
@@ -2334,5 +2334,5 @@ EOF;
|
||||
return $output;
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2167,7 +2167,7 @@ abstract class AdminTabCore
|
||||
* @param integer $id_lang Language id (optional)
|
||||
* @return string
|
||||
*/
|
||||
protected function getFieldValue($obj, $key, $id_lang = NULL, $id_shop = null)
|
||||
public function getFieldValue($obj, $key, $id_lang = NULL, $id_shop = null)
|
||||
{
|
||||
if (!$id_shop && $obj->isLangMultishop())
|
||||
$id_shop = Context::getContext()->shop->getID();
|
||||
|
||||
@@ -174,6 +174,13 @@ class CarrierCore extends ObjectModel
|
||||
return true;
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
if (!parent::delete())
|
||||
return false;
|
||||
return Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'cart_rule_carrier WHERE id_carrier = '.(int)$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change carrier id in delivery prices when updating a carrier
|
||||
*
|
||||
|
||||
+96
-242
@@ -212,7 +212,7 @@ class CartCore extends ObjectModel
|
||||
DELETE FROM `'._DB_PREFIX_.'customization`
|
||||
WHERE `id_cart` = '.(int)$this->id);
|
||||
|
||||
if (!Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cart_discount` WHERE `id_cart` = '.(int)($this->id))
|
||||
if (!Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cart_cart_rule` WHERE `id_cart` = '.(int)($this->id))
|
||||
OR !Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cart_product` WHERE `id_cart` = '.(int)($this->id)))
|
||||
return false;
|
||||
|
||||
@@ -247,6 +247,15 @@ class CartCore extends ObjectModel
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 1.5.0.1
|
||||
*/
|
||||
public function getDiscounts($lite = false, $refresh = false)
|
||||
{
|
||||
Tools::displayAsDeprecated();
|
||||
return $this->getCartRules();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return cart discounts
|
||||
*
|
||||
@@ -254,78 +263,49 @@ class CartCore extends ObjectModel
|
||||
* @param bool true will erase the cache
|
||||
* @result array Discounts
|
||||
*/
|
||||
public function getDiscounts($lite = false, $refresh = false)
|
||||
public function getCartRules()
|
||||
{
|
||||
// if discounts are never used
|
||||
if (!Discount::isFeatureActive())
|
||||
// TODO : add cache
|
||||
|
||||
// If the cart has not been saved, then there can't be any cart rule applied
|
||||
if (!CartRule::isFeatureActive() || !$this->id)
|
||||
return array();
|
||||
|
||||
if (!$this->id)
|
||||
return array();
|
||||
|
||||
if (!$refresh)
|
||||
{
|
||||
if (!$lite AND isset(self::$_discounts[$this->id]))
|
||||
return self::$_discounts[$this->id];
|
||||
|
||||
if ($lite AND isset(self::$_discountsLite[$this->id]))
|
||||
return self::$_discountsLite[$this->id];
|
||||
}
|
||||
|
||||
$total_products_ti = $this->getOrderTotal(true, Cart::ONLY_PRODUCTS);
|
||||
$total_products_te = $this->getOrderTotal(false, Cart::ONLY_PRODUCTS);
|
||||
$shipping_ti = $this->getOrderShippingCost();
|
||||
$shipping_te = $this->getOrderShippingCost(NULL, false);
|
||||
|
||||
$result = Db::getInstance()->executeS('
|
||||
SELECT d.*, `id_cart`
|
||||
FROM `'._DB_PREFIX_.'cart_discount` c
|
||||
LEFT JOIN `'._DB_PREFIX_.'discount` d ON c.`id_discount` = d.`id_discount`
|
||||
WHERE `id_cart` = '.(int)($this->id));
|
||||
SELECT *
|
||||
FROM `'._DB_PREFIX_.'cart_cart_rule` cd
|
||||
LEFT JOIN `'._DB_PREFIX_.'cart_rule` cr ON cd.`id_cart_rule` = cr.`id_cart_rule`
|
||||
LEFT JOIN `'._DB_PREFIX_.'cart_rule_lang` crl ON (cd.`id_cart_rule` = cr.`id_cart_rule` AND crl.id_lang = '.(int)$this->id_lang.')
|
||||
WHERE `id_cart` = '.(int)$this->id);
|
||||
|
||||
$products = $this->getProducts();
|
||||
foreach ($result AS $k => $discount)
|
||||
foreach ($result as &$row)
|
||||
{
|
||||
$categories = Discount::getCategories((int)($discount['id_discount']));
|
||||
$in_category = false;
|
||||
foreach ($products AS $product)
|
||||
if (Product::idIsOnCategoryId((int)($product['id_product']), $categories))
|
||||
{
|
||||
$in_category = true;
|
||||
break;
|
||||
}
|
||||
if (!$in_category)
|
||||
unset($result[$k]);
|
||||
$cartRule = new CartRule($row['id_cart_rule'], (int)$this->id_lang);
|
||||
$row['value_real'] = $cartRule->getValue(true);
|
||||
$row['value_tax_exc'] = $cartRule->getValue(false);
|
||||
|
||||
// Retro compatibility < 1.5.0.2
|
||||
$row['id_discount'] = $row['id_cart_rule'];
|
||||
$row['description'] = $row['name'];
|
||||
}
|
||||
|
||||
if ($lite)
|
||||
{
|
||||
self::$_discountsLite[$this->id] = $result;
|
||||
return $result;
|
||||
}
|
||||
|
||||
$total_products_wt = $this->getOrderTotal(true, Cart::ONLY_PRODUCTS);
|
||||
$total_products = $this->getOrderTotal(false, Cart::ONLY_PRODUCTS);
|
||||
$shipping_wt = $this->getOrderShippingCost();
|
||||
$shipping = $this->getOrderShippingCost(NULL, false);
|
||||
self::$_discounts[$this->id] = array();
|
||||
foreach ($result as $row)
|
||||
{
|
||||
$discount = new Discount($row['id_discount'], (int)($this->id_lang));
|
||||
$row['description'] = $discount->description ? $discount->description : $discount->name;
|
||||
$row['value_real'] = $discount->getValue(sizeof($result), $total_products_wt, $shipping_wt, $this->id);
|
||||
$row['value_tax_exc'] = $discount->getValue(sizeof($result), $total_products, $shipping, $this->id, false);
|
||||
if ($row['value_real'] !== 0)
|
||||
self::$_discounts[$this->id][] = $row;
|
||||
else
|
||||
$this->deleteDiscount($row['id_discount']);
|
||||
}
|
||||
return isset(self::$_discounts[$this->id]) ? self::$_discounts[$this->id] : NULL;
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Todo: see uses and change name
|
||||
public function getDiscountsCustomer($id_discount)
|
||||
{
|
||||
if (!Discount::isFeatureActive())
|
||||
if (!CartRule::isFeatureActive())
|
||||
return 0;
|
||||
|
||||
return Db::getInstance()->getValue('
|
||||
SELECT COUNT(*)
|
||||
FROM `'._DB_PREFIX_.'cart_discount`
|
||||
FROM `'._DB_PREFIX_.'cart_cart_rule`
|
||||
WHERE `id_discount` = '.(int)($id_discount).' AND `id_cart` = '.(int)($this->id));
|
||||
}
|
||||
|
||||
@@ -570,14 +550,17 @@ class CartCore extends ObjectModel
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a discount to the cart (NO controls except doubles)
|
||||
*
|
||||
* @param integer $id_discount The discount to add to the cart
|
||||
* @result boolean Update result
|
||||
* @deprecated 1.5.0.1
|
||||
*/
|
||||
public function addDiscount($id_discount)
|
||||
public function addDiscount($id_discount)
|
||||
{
|
||||
return Db::getInstance()->AutoExecute(_DB_PREFIX_.'cart_discount', array('id_discount' => (int)($id_discount), 'id_cart' => (int)($this->id)), 'INSERT');
|
||||
Tools::displayAsDeprecated();
|
||||
return $this->addCartRule($id_discount);
|
||||
}
|
||||
|
||||
public function addCartRule($id_cart_rule)
|
||||
{
|
||||
return Db::getInstance()->AutoExecute(_DB_PREFIX_.'cart_cart_rule', array('id_cart_rule' => (int)$id_cart_rule, 'id_cart' => (int)$this->id), 'INSERT');
|
||||
}
|
||||
|
||||
public function containsProduct($id_product, $id_product_attribute = 0, $id_customization = false)
|
||||
@@ -701,6 +684,9 @@ class CartCore extends ObjectModel
|
||||
// refresh cache of self::_products
|
||||
$this->_products = $this->getProducts(true);
|
||||
$this->update(true);
|
||||
$context = Context::getContext()->cloneContext();
|
||||
$context->cart = $this;
|
||||
CartRule::autoAddToCart($context);
|
||||
|
||||
if ($product->customizable)
|
||||
return $this->_updateCustomizationQuantity((int)$quantity, (int)$id_customization, (int)$id_product, (int)$id_product_attribute, $operator);
|
||||
@@ -820,19 +806,18 @@ class CartCore extends ObjectModel
|
||||
return (bool)Db::getInstance()->getValue('SELECT `id_cart` FROM `'._DB_PREFIX_.'orders` WHERE `id_cart` = '.(int)$this->id);
|
||||
}
|
||||
|
||||
/*
|
||||
** Deletion
|
||||
*/
|
||||
|
||||
/**
|
||||
* Delete a discount from the cart
|
||||
*
|
||||
* @param integer $id_discount Discount ID
|
||||
* @return boolean result
|
||||
* @deprecated 1.5.0.1
|
||||
*/
|
||||
public function deleteDiscount($id_discount)
|
||||
public function deleteDiscount($id_discount)
|
||||
{
|
||||
return Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cart_discount` WHERE `id_discount` = '.(int)$id_discount.' AND `id_cart` = '.(int)$this->id.' LIMIT 1');
|
||||
Tools::displayAsDeprecated();
|
||||
return $this->removeCartRule($id_discount);
|
||||
}
|
||||
|
||||
public function removeCartRule($id_cart_rule)
|
||||
{
|
||||
return Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'cart_cart_rule` WHERE `id_cart_rule` = '.(int)$id_cart_rule.' AND `id_cart` = '.(int)$this->id.' LIMIT 1');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -982,7 +967,8 @@ class CartCore extends ObjectModel
|
||||
die(Tools::displayError());
|
||||
|
||||
// if discounts are never used
|
||||
if ($type == Cart::ONLY_DISCOUNTS && !Discount::isFeatureActive())
|
||||
// Todo: remove and replace by cart rules
|
||||
if ($type == Cart::ONLY_DISCOUNTS && !CartRule::isFeatureActive())
|
||||
return 0;
|
||||
// no shipping cost if is a cart with only virtuals products
|
||||
$virtual = $this->isVirtualCart();
|
||||
@@ -1025,6 +1011,7 @@ class CartCore extends ObjectModel
|
||||
$order_total += $total_price;
|
||||
}
|
||||
$order_total_products = $order_total;
|
||||
// Todo: consider optimizations
|
||||
if ($type == Cart::ONLY_DISCOUNTS)
|
||||
$order_total = 0;
|
||||
// Wrapping Fees
|
||||
@@ -1039,61 +1026,28 @@ class CartCore extends ObjectModel
|
||||
}
|
||||
$wrapping_fees = Tools::convertPrice(Tools::ps_round($wrapping_fees, 2), Currency::getCurrencyInstance((int)($this->id_currency)));
|
||||
}
|
||||
if ($type != Cart::ONLY_PRODUCTS)
|
||||
|
||||
$order_total_discount = 0;
|
||||
if ($type != Cart::ONLY_PRODUCTS && CartRule::isFeatureActive())
|
||||
{
|
||||
$discounts = array();
|
||||
if (Discount::isFeatureActive())
|
||||
{
|
||||
/* Firstly get all discounts, looking for a free shipping one (in order to substract shipping fees to the total amount) */
|
||||
if ($discountIds = $this->getDiscounts(true))
|
||||
{
|
||||
foreach ($discountIds AS $id_discount)
|
||||
{
|
||||
$discount = new Discount((int)($id_discount['id_discount']));
|
||||
if (Validate::isLoadedObject($discount))
|
||||
{
|
||||
$discounts[] = $discount;
|
||||
if ($discount->id_discount_type == Discount::FREE_SHIPPING)
|
||||
foreach($products AS $product)
|
||||
{
|
||||
$categories = Discount::getCategories($discount->id);
|
||||
if (count($categories) AND Product::idIsOnCategoryId($product['id_product'], $categories))
|
||||
{
|
||||
if($type == Cart::ONLY_DISCOUNTS)
|
||||
$order_total -= $shipping_fees;
|
||||
$shipping_fees = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Secondly applying all vouchers to the correct amount */
|
||||
$shrunk = false;
|
||||
foreach ($discounts AS $discount)
|
||||
if ($discount->id_discount_type != Discount::FREE_SHIPPING)
|
||||
{
|
||||
$order_total -= Tools::ps_round((float)($discount->getValue(sizeof($discounts), $order_total_products, $shipping_fees, $this->id, (int)($withTaxes))), 2);
|
||||
if ($discount->id_discount_type == Discount::AMOUNT)
|
||||
if (in_array($discount->behavior_not_exhausted, array(1,2)))
|
||||
$shrunk = true;
|
||||
}
|
||||
|
||||
$order_total_discount = 0;
|
||||
if ($shrunk AND $order_total < (-$wrapping_fees - $order_total_products - $shipping_fees))
|
||||
$order_total_discount = -$wrapping_fees - $order_total_products - $shipping_fees;
|
||||
else
|
||||
$order_total_discount = $order_total;
|
||||
}
|
||||
}
|
||||
$result = $this->getCartRules();
|
||||
foreach (ObjectModel::hydrateCollection('CartRule', $result, Configuration::get('PS_LANG_DEFAULT')) AS $cartRule)
|
||||
$order_total_discount += Tools::ps_round($cartRule->getValue($withTaxes));
|
||||
$order_total_discount = min(Tools::ps_round($order_total_discount), $wrapping_fees + $order_total_products + $shipping_fees);
|
||||
$order_total -= $order_total_discount;
|
||||
}
|
||||
|
||||
if ($type == Cart::ONLY_SHIPPING) return $shipping_fees;
|
||||
if ($type == Cart::ONLY_WRAPPING) return $wrapping_fees;
|
||||
if ($type == Cart::BOTH) $order_total += $shipping_fees + $wrapping_fees;
|
||||
if ($order_total < 0 AND $type != Cart::ONLY_DISCOUNTS) return 0;
|
||||
if ($type == Cart::ONLY_DISCOUNTS AND isset($order_total_discount))
|
||||
return Tools::ps_round((float)($order_total_discount), 2);
|
||||
return Tools::ps_round((float)($order_total), 2);
|
||||
if ($type == Cart::ONLY_SHIPPING)
|
||||
return $shipping_fees;
|
||||
if ($type == Cart::ONLY_WRAPPING)
|
||||
return $wrapping_fees;
|
||||
if ($type == Cart::BOTH)
|
||||
$order_total += $shipping_fees + $wrapping_fees;
|
||||
if ($order_total < 0 AND $type != Cart::ONLY_DISCOUNTS)
|
||||
return 0;
|
||||
if ($type == Cart::ONLY_DISCOUNTS)
|
||||
return $order_total_discount;
|
||||
return Tools::ps_round((float)$order_total, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1110,33 +1064,6 @@ class CartCore extends ObjectModel
|
||||
if (!$default_country)
|
||||
$default_country = Context::getContext()->country;
|
||||
|
||||
// Checking discounts in cart
|
||||
$products = $this->getProducts();
|
||||
if (Discount::isFeatureActive())
|
||||
$discounts = $this->getDiscounts(true);
|
||||
else
|
||||
$discounts = null;
|
||||
if ($discounts)
|
||||
foreach ($discounts AS $id_discount)
|
||||
if ($id_discount['id_discount_type'] == Discount::FREE_SHIPPING)
|
||||
{
|
||||
if ($id_discount['minimal'] > 0)
|
||||
{
|
||||
$total_cart = 0;
|
||||
|
||||
$categories = Discount::getCategories((int)($id_discount['id_discount']));
|
||||
if (sizeof($categories))
|
||||
foreach($products AS $product)
|
||||
if (Product::idIsOnCategoryId((int)($product['id_product']), $categories))
|
||||
$total_cart += $product['total_wt'];
|
||||
|
||||
if ($total_cart >= $id_discount['minimal'])
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Order total in default currency without fees
|
||||
$order_total = $this->getOrderTotal(true, Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING);
|
||||
|
||||
@@ -1250,9 +1177,9 @@ class CartCore extends ObjectModel
|
||||
$free_fees_price = 0;
|
||||
if (isset($configuration['PS_SHIPPING_FREE_PRICE']))
|
||||
$free_fees_price = Tools::convertPrice((float)($configuration['PS_SHIPPING_FREE_PRICE']), Currency::getCurrencyInstance((int)($this->id_currency)));
|
||||
$orderTotalwithDiscounts = $this->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING);
|
||||
if ($orderTotalwithDiscounts >= (float)($free_fees_price) AND (float)($free_fees_price) > 0)
|
||||
return $shipping_cost;
|
||||
// $orderTotalwithDiscounts = $this->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING);
|
||||
// if ($orderTotalwithDiscounts >= (float)($free_fees_price) AND (float)($free_fees_price) > 0)
|
||||
// return $shipping_cost;
|
||||
if (isset($configuration['PS_SHIPPING_FREE_WEIGHT']) AND $this->getTotalWeight() >= (float)($configuration['PS_SHIPPING_FREE_WEIGHT']) AND (float)($configuration['PS_SHIPPING_FREE_WEIGHT']) > 0)
|
||||
return $shipping_cost;
|
||||
|
||||
@@ -1290,9 +1217,10 @@ class CartCore extends ObjectModel
|
||||
if (isset($configuration['PS_SHIPPING_HANDLING']) AND $carrier->shipping_handling)
|
||||
$shipping_cost += (float)($configuration['PS_SHIPPING_HANDLING']);
|
||||
|
||||
// TODO : $products does not exists
|
||||
// Additional Shipping Cost per product
|
||||
foreach($products AS $product)
|
||||
$shipping_cost += $product['additional_shipping_cost'] * $product['cart_quantity'];
|
||||
// foreach($products AS $product)
|
||||
// $shipping_cost += $product['additional_shipping_cost'] * $product['cart_quantity'];
|
||||
|
||||
$shipping_cost = Tools::convertPrice($shipping_cost, Currency::getCurrencyInstance((int)($this->id_currency)));
|
||||
|
||||
@@ -1357,90 +1285,16 @@ class CartCore extends ObjectModel
|
||||
}
|
||||
return self::$_totalWeight[$this->id];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check discount validity
|
||||
*
|
||||
* @return mixed Return a string if an error occurred and false otherwise
|
||||
*/
|
||||
function checkDiscountValidity($discountObj, $discounts, $order_total, $products, $checkCartDiscount = false,
|
||||
Customer $customer = null, Shop $shop = null)
|
||||
* @deprecated 1.5.0.1
|
||||
*/
|
||||
public function checkDiscountValidity($discountObj, $discounts, $order_total, $products, $checkCartDiscount = false)
|
||||
{
|
||||
if (!$shop)
|
||||
$shop = Context::getContext()->shop;
|
||||
if (!$customer)
|
||||
$customer = Context::getContext()->customer;
|
||||
if (!$order_total)
|
||||
return Tools::displayError('Cannot add voucher if order is free.');
|
||||
if (!$discountObj->active)
|
||||
return Tools::displayError('This voucher has already been used or is disabled.');
|
||||
if (!$discountObj->quantity)
|
||||
return Tools::displayError('This voucher has expired (usage limit attained).');
|
||||
if ($discountObj->id_discount_type == Discount::AMOUNT AND $this->id_currency != $discountObj->id_currency)
|
||||
return Tools::displayError('This voucher can only be used in the following currency:').'
|
||||
'.Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT `name` FROM `'._DB_PREFIX_.'currency` WHERE id_currency = '.(int)$discountObj->id_currency);
|
||||
if ($checkCartDiscount
|
||||
AND (
|
||||
$this->getDiscountsCustomer($discountObj->id) >= $discountObj->quantity_per_user
|
||||
OR (Order::getDiscountsCustomer($customer->id, $discountObj->id) + $this->getDiscountsCustomer($discountObj->id) >= $discountObj->quantity_per_user) >= $discountObj->quantity_per_user
|
||||
)
|
||||
)
|
||||
return Tools::displayError('You cannot use this voucher anymore (usage limit attained).');
|
||||
if (strtotime($discountObj->date_from) > time())
|
||||
return Tools::displayError('This voucher is not yet valid');
|
||||
if (strtotime($discountObj->date_to) < time())
|
||||
return Tools::displayError('This voucher has expired.');
|
||||
if (!$discountObj->isAssociatedToShop((int)$shop->id))
|
||||
return Tools::displayError('This voucher is not available with this shop.');
|
||||
if (sizeof($discounts) >= 1 AND $checkCartDiscount)
|
||||
{
|
||||
if (!$discountObj->cumulable)
|
||||
return Tools::displayError('This voucher is not valid with other current discounts.');
|
||||
foreach ($discounts as $discount)
|
||||
if (!$discount['cumulable'])
|
||||
return Tools::displayError('Voucher is not valid with other discounts.');
|
||||
|
||||
foreach($discounts as $discount)
|
||||
if($discount['id_discount'] == $discountObj->id)
|
||||
return Tools::displayError('This voucher is already in your cart');
|
||||
}
|
||||
|
||||
$groups = Customer::getGroupsStatic($this->id_customer);
|
||||
|
||||
if (($discountObj->id_customer OR $discountObj->id_group) AND ((($this->id_customer != $discountObj->id_customer) OR ($this->id_customer == 0)) AND !in_array($discountObj->id_group, $groups)))
|
||||
{
|
||||
if (!$customer->isLogged())
|
||||
return Tools::displayError('You cannot use this voucher.').' - '.Tools::displayError('Please log in.');
|
||||
return Tools::displayError('You cannot use this voucher.');
|
||||
}
|
||||
|
||||
$onlyProductWithDiscount = true;
|
||||
if (!$discountObj->cumulable_reduction)
|
||||
{
|
||||
foreach ($products as $product)
|
||||
if (!$product['reduction_applies'] AND !$product['on_sale'])
|
||||
$onlyProductWithDiscount = false;
|
||||
}
|
||||
if (!$discountObj->cumulable_reduction AND $onlyProductWithDiscount)
|
||||
return Tools::displayError('This voucher is not valid for marked or reduced products.');
|
||||
$total_cart = 0;
|
||||
$categories = Discount::getCategories($discountObj->id);
|
||||
$returnErrorNoProductCategory = true;
|
||||
foreach($products AS $product)
|
||||
{
|
||||
if(count($categories))
|
||||
if (Product::idIsOnCategoryId($product['id_product'], $categories))
|
||||
{
|
||||
if ((!$discountObj->cumulable_reduction AND !$product['reduction_applies'] AND !$product['on_sale']) OR $discountObj->cumulable_reduction)
|
||||
$total_cart += $discountObj->include_tax ? $product['total_wt'] : $product['total'];
|
||||
$returnErrorNoProductCategory = false;
|
||||
}
|
||||
}
|
||||
if ($returnErrorNoProductCategory)
|
||||
return Tools::displayError('This discount does not apply to that product category.');
|
||||
if ($total_cart < $discountObj->minimal)
|
||||
return Tools::displayError('The order total is not high enough or this voucher cannot be used with those products.');
|
||||
return false;
|
||||
Tools::displayAsDeprecated();
|
||||
$context = Context::getContext()->cloneContext();
|
||||
$context->cart = $this;
|
||||
return $discountObj->checkValidity($context);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1468,7 +1322,7 @@ class CartCore extends ObjectModel
|
||||
$total_free_ship = 0;
|
||||
if ($free_ship = Tools::convertPrice((float)(Configuration::get('PS_SHIPPING_FREE_PRICE')), new Currency((int)($this->id_currency))))
|
||||
{
|
||||
$discounts = $this->getDiscounts();
|
||||
$discounts = $this->getCartRules();
|
||||
$total_free_ship = $free_ship - ($this->getOrderTotal(true, Cart::ONLY_PRODUCTS) + $this->getOrderTotal(true, Cart::ONLY_DISCOUNTS));
|
||||
foreach ($discounts as $discount)
|
||||
if ($discount['id_discount_type'] == Discount::FREE_SHIPPING)
|
||||
@@ -1485,7 +1339,7 @@ class CartCore extends ObjectModel
|
||||
'formattedAddresses' => $formattedAddresses,
|
||||
'carrier' => new Carrier($this->id_carrier, $id_lang),
|
||||
'products' => $this->getProducts(false),
|
||||
'discounts' => $this->getDiscounts(false, true),
|
||||
'discounts' => $this->getCartRules(),
|
||||
'is_virtual_cart' => (int)$this->isVirtualCart(),
|
||||
'total_discounts' => $this->getOrderTotal(true, Cart::ONLY_DISCOUNTS),
|
||||
'total_discounts_tax_exc' => $this->getOrderTotal(false, Cart::ONLY_DISCOUNTS),
|
||||
|
||||
@@ -0,0 +1,624 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2011 PrestaShop SA
|
||||
* @version Release: $Revision: 7040 $
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class CartRuleCore extends ObjectModel
|
||||
{
|
||||
public $id;
|
||||
public $name;
|
||||
public $id_customer;
|
||||
public $date_from;
|
||||
public $date_to;
|
||||
public $description;
|
||||
public $quantity = 1;
|
||||
public $quantity_per_user = 1;
|
||||
public $priority = 1;
|
||||
public $code;
|
||||
public $minimum_amount;
|
||||
public $minimum_amount_tax;
|
||||
public $minimum_amount_currency;
|
||||
public $minimum_amount_shipping;
|
||||
public $country_restriction;
|
||||
public $carrier_restriction;
|
||||
public $group_restriction;
|
||||
public $cart_rule_restriction;
|
||||
public $product_restriction;
|
||||
public $free_shipping;
|
||||
public $reduction_percent;
|
||||
public $reduction_amount;
|
||||
public $reduction_tax;
|
||||
public $reduction_currency;
|
||||
public $reduction_product;
|
||||
public $gift_product;
|
||||
public $active = 1;
|
||||
public $date_add;
|
||||
public $date_upd;
|
||||
|
||||
protected $table = 'cart_rule';
|
||||
protected $identifier = 'id_cart_rule';
|
||||
|
||||
protected $fieldsRequired = array('date_from', 'date_to');
|
||||
protected $fieldsSize = array('code' => 254, 'description' => 65534);
|
||||
protected $fieldsValidate = array(
|
||||
'id_customer' => 'isUnsignedId',
|
||||
'date_from' => 'isDate',
|
||||
'date_to' => 'isDate',
|
||||
'description' => 'isCleanHtml',
|
||||
'quantity' => 'isUnsignedInt',
|
||||
'quantity_per_user' => 'isUnsignedInt',
|
||||
'priority' => 'isUnsignedInt',
|
||||
'code' => 'isCleanHtml',
|
||||
'minimum_amount' => 'isFloat',
|
||||
'minimum_amount_tax' => 'isBool',
|
||||
'minimum_amount_currency' => 'isInt',
|
||||
'minimum_amount_shipping' => 'isBool',
|
||||
'country_restriction' => 'isBool',
|
||||
'carrier_restriction' => 'isBool',
|
||||
'group_restriction' => 'isBool',
|
||||
'cart_rule_restriction' => 'isBool',
|
||||
'product_restriction' => 'isBool',
|
||||
'free_shipping' => 'isBool',
|
||||
'reduction_percent' => 'isFloat',
|
||||
'reduction_amount' => 'isFloat',
|
||||
'reduction_tax' => 'isBool',
|
||||
'reduction_currency' => 'isUnsignedId',
|
||||
'reduction_product' => 'isInt',
|
||||
'gift_product' => 'isUnsignedId',
|
||||
'active' => 'isBool',
|
||||
'date_add' => 'isDate',
|
||||
'date_upd' => 'isDate'
|
||||
);
|
||||
protected $fieldsRequiredLang = array('name');
|
||||
protected $fieldsSizeLang = array('name' => 254);
|
||||
protected $fieldsValidateLang = array('name' => 'isCleanHtml');
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
$this->validateFields();
|
||||
$fields['id_customer'] = (int)$this->id_customer;
|
||||
$fields['priority'] = (int)$this->priority;
|
||||
$fields['code'] = pSQL($this->code);
|
||||
$fields['quantity'] = (int)$this->quantity;
|
||||
$fields['quantity_per_user'] = (int)$this->quantity_per_user;
|
||||
$fields['date_from'] = pSQL($this->date_from);
|
||||
$fields['date_to'] = pSQL($this->date_to);
|
||||
$fields['description'] = pSQL($this->description);
|
||||
$fields['minimum_amount'] = (float)$this->minimum_amount;
|
||||
$fields['minimum_amount_tax'] = (int)$this->minimum_amount_tax;
|
||||
$fields['minimum_amount_currency'] = (int)$this->minimum_amount_currency;
|
||||
$fields['minimum_amount_shipping'] = (int)$this->minimum_amount_shipping;
|
||||
$fields['country_restriction'] = (int)$this->country_restriction;
|
||||
$fields['carrier_restriction'] = (int)$this->carrier_restriction;
|
||||
$fields['group_restriction'] = (int)$this->group_restriction;
|
||||
$fields['cart_rule_restriction'] = (int)$this->cart_rule_restriction;
|
||||
$fields['product_restriction'] = (int)$this->product_restriction;
|
||||
$fields['free_shipping'] = (int)$this->free_shipping;
|
||||
$fields['reduction_percent'] = (float)$this->reduction_percent;
|
||||
$fields['reduction_amount'] = (float)$this->reduction_amount;
|
||||
$fields['reduction_tax'] = (int)$this->reduction_tax;
|
||||
$fields['reduction_currency'] = (int)$this->reduction_currency;
|
||||
$fields['reduction_product'] = (int)$this->reduction_product;
|
||||
$fields['gift_product'] = (int)$this->gift_product;
|
||||
$fields['active'] = (int)$this->active;
|
||||
$fields['date_add'] = pSQL($this->date_add);
|
||||
$fields['date_upd'] = pSQL($this->date_upd);
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function getTranslationsFieldsChild()
|
||||
{
|
||||
if (!$this->validateFieldsLang())
|
||||
return false;
|
||||
return $this->getTranslationsFields(array('name'));
|
||||
}
|
||||
|
||||
public function add($autodate = true, $nullValues = false)
|
||||
{
|
||||
if (!parent::add($autodate, $nullValues))
|
||||
return false;
|
||||
|
||||
Configuration::updateGlobalValue('PS_CART_RULE_FEATURE_ACTIVE', '1');
|
||||
return true;
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
if (!parent::delete())
|
||||
return false;
|
||||
|
||||
Configuration::updateGlobalValue('PS_CART_RULE_FEATURE_ACTIVE', CartRule::isCurrentlyUsed($this->table, true));
|
||||
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'cart_cart_rule` WHERE `id_cart_rule` = '.(int)$this->id);
|
||||
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_carrier` WHERE `id_cart_rule` = '.(int)$this->id);
|
||||
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_group` WHERE `id_cart_rule` = '.(int)$this->id);
|
||||
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_country` WHERE `id_cart_rule` = '.(int)$this->id);
|
||||
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_cart_rule` WHERE `id_cart_rule_1` = '.(int)$this->id.' OR `id_cart_rule_2` = '.(int)$this->id);
|
||||
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_product_rule` WHERE `id_cart_rule` = '.(int)$this->id);
|
||||
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_product_rule_value` WHERE `id_product_rule` NOT IN (SELECT `id_product_rule` FROM `'._DB_PREFIX_.'cart_rule_product_rule`)');
|
||||
}
|
||||
|
||||
public static function getIdByCode($code)
|
||||
{
|
||||
if (!Validate::isDiscountName($code))
|
||||
return false;
|
||||
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT `id_cart_rule` FROM `'._DB_PREFIX_.'cart_rule` WHERE `code` = \''.pSQL($code).'\'');
|
||||
}
|
||||
|
||||
public function getProductRules()
|
||||
{
|
||||
if (!Validate::isLoadedObject($this) OR $this->product_restriction == 0)
|
||||
return array();
|
||||
|
||||
$productRules = array();
|
||||
$result = Db::getInstance()->ExecuteS('
|
||||
SELECT *
|
||||
FROM '._DB_PREFIX_.'cart_rule_product_rule pr
|
||||
LEFT JOIN '._DB_PREFIX_.'cart_rule_product_rule_value prv ON pr.id_product_rule = prv.id_product_rule
|
||||
WHERE pr.id_cart_rule = '.(int)$this->id, false);
|
||||
while ($row = Db::getInstance()->nextRow($result))
|
||||
{
|
||||
if (!isset($productRules[$row['id_product_rule']]))
|
||||
$productRules[$row['id_product_rule']] = array('quantity' => $row['quantity'], 'type' => $row['type'], 'values' => array());
|
||||
$productRules[$row['id_product_rule']]['values'][] = $row['id_item'];
|
||||
}
|
||||
return $productRules;
|
||||
}
|
||||
|
||||
// Todo : Add shop management
|
||||
public function checkValidity(Context $context, $alreadyInCart = false)
|
||||
{
|
||||
if (!CartRule::isFeatureActive())
|
||||
return false;
|
||||
|
||||
if (!$this->active)
|
||||
return Tools::displayError('This voucher is disabled');
|
||||
if (!$this->quantity)
|
||||
return Tools::displayError('This voucher has already been used ');
|
||||
if (strtotime($this->date_from) > time())
|
||||
return Tools::displayError('This voucher is not valid yet');
|
||||
if (strtotime($this->date_to) < time())
|
||||
return Tools::displayError('This voucher has expired');
|
||||
|
||||
if ($context->cart->id_customer)
|
||||
{
|
||||
$quantityUsed = Db::getInstance()->getValue('
|
||||
SELECT count(*)
|
||||
FROM '._DB_PREFIX_.'orders o
|
||||
LEFT JOIN '._DB_PREFIX_.'order_discount od ON o.id_order = od.id_order
|
||||
WHERE o.id_customer = '.$context->cart->id_customer.'
|
||||
AND od.id_cart_rule = '.(int)$this->id.'
|
||||
AND '.(int)Configuration::get('PS_OS_ERROR').' != (
|
||||
SELECT oh.id_order_state
|
||||
FROM '._DB_PREFIX_.'order_history oh
|
||||
WHERE oh.id_order = o.id_order
|
||||
ORDER BY oh.date_add DESC
|
||||
LIMIT 1
|
||||
)');
|
||||
if ($quantityUsed + 1 > $this->quantity_per_user)
|
||||
return Tools::displayError('You cannot use this voucher anymore (usage limit reached)');
|
||||
}
|
||||
|
||||
$otherCartRules = $context->cart->getCartRules();
|
||||
if (count($otherCartRules))
|
||||
foreach ($otherCartRules as $otherCartRule)
|
||||
{
|
||||
if ($otherCartRule['id_cart_rule'] == $this->id && !$alreadyInCart)
|
||||
return Tools::displayError('This voucher is already in your cart');
|
||||
if ($this->carrier_restriction AND $otherCartRule['cart_rule_restriction'])
|
||||
{
|
||||
$combinable = Db::getInstance()->getValue('
|
||||
SELECT id_cart_rule_1
|
||||
FROM '._DB_PREFIX_.'cart_rule_combination
|
||||
WHERE (id_cart_rule_1 = '.(int)$this->id.' AND id_cart_rule_2 = '.(int)$otherCartRule['id_cart_rule'].')
|
||||
OR (id_cart_rule_2 = '.(int)$this->id.' AND id_cart_rule_1 = '.(int)$otherCartRule['id_cart_rule'].')');
|
||||
if (!$combinable)
|
||||
{
|
||||
$cartRule = new CartRule($otherCartRule['cart_rule_restriction'], $context->cart->id_lang);
|
||||
return Tools::displayError('This voucher is not combinable with an other voucher already in your cart:').' '.$this->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get an intersection of the customer groups and the cart rule groups (if the customer is not logged in, the default group is 1)
|
||||
if ($this->group_restriction)
|
||||
{
|
||||
$id_cart_rule = (int)Db::getInstance()->getValue('
|
||||
SELECT crg.id_cart_rule
|
||||
FROM '._DB_PREFIX_.'cart_rule_group crg
|
||||
WHERE crg.id_cart_rule = '.(int)$this->id.'
|
||||
AND crg.id_group '.($context->cart->id_customer ? 'IN (SELECT cg.id_group FROM '._DB_PREFIX_.'customer_group cg WHERE cg.id_customer = '.(int)$context->cart->id_customer.')' : '= 1'));
|
||||
if (!$id_cart_rule)
|
||||
return Tools::displayError('You cannot use this voucher');
|
||||
}
|
||||
|
||||
// Check if the customer delivery address is usable with the cart rule
|
||||
if ($this->country_restriction AND $context->cart->id_address_delivery)
|
||||
{
|
||||
$id_cart_rule = (int)Db::getInstance()->getValue('
|
||||
SELECT crc.id_cart_rule
|
||||
FROM '._DB_PREFIX_.'cart_rule_country crc
|
||||
WHERE crc.id_cart_rule = '.(int)$this->id.'
|
||||
AND crc.id_country = (SELECT a.id_country FROM '._DB_PREFIX_.'address a WHERE a.id_address = '.(int)$context->cart->id_address_delivery.' LIMIT 1)');
|
||||
if (!$id_cart_rule)
|
||||
return Tools::displayError('You cannot use this voucher in your country of delivery');
|
||||
}
|
||||
|
||||
// Check if the carrier chosen by the customer is usable with the cart rule
|
||||
if ($this->carrier_restriction AND $context->cart->id_carrier)
|
||||
{
|
||||
$id_cart_rule = (int)Db::getInstance()->getValue('
|
||||
SELECT crc.id_cart_rule
|
||||
FROM '._DB_PREFIX_.'cart_rule_carrier crc
|
||||
WHERE crc.id_cart_rule = '.(int)$this->id.'
|
||||
AND crc.id_carrier = '.(int)$context->cart->id_carrier);
|
||||
if (!$id_cart_rule)
|
||||
return Tools::displayError('You cannot use this voucher with this carrier');
|
||||
}
|
||||
|
||||
// Check if the products chosen by the customer are usable with the cart rule
|
||||
if ($this->product_restriction)
|
||||
{
|
||||
$productRules = $this->getProductRules();
|
||||
foreach ($productRules as $productRule)
|
||||
{
|
||||
switch ($productRule['type'])
|
||||
{
|
||||
case 'attributes':
|
||||
$cartAttributes = Db::getInstance()->ExecuteS('
|
||||
SELECT cp.quantity, pac.`id_attribute`
|
||||
FROM `'._DB_PREFIX_.'cart_product` cp
|
||||
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON cp.id_product_attribute = pac.id_product_attribute
|
||||
WHERE cp.`id_cart` = '.(int)$context->cart->id.'
|
||||
AND cp.id_product_attribute > 0');
|
||||
$matchingProducts = 0;
|
||||
foreach ($cartAttributes as $cartAttribute)
|
||||
if (in_array($cartAttribute['id_attribute'], $productRule['values']))
|
||||
$matchingProducts += $cartAttribute['quantity'];
|
||||
if ($matchingProducts < $productRule['quantity'])
|
||||
return Tools::displayError('You cannot use this voucher with these products');
|
||||
break;
|
||||
case 'products':
|
||||
$cartProducts = Db::getInstance()->ExecuteS('
|
||||
SELECT cp.quantity, cp.`id_product`
|
||||
FROM `'._DB_PREFIX_.'cart_product` cp
|
||||
WHERE cp.`id_cart` = '.(int)$context->cart->id);
|
||||
$matchingProducts = 0;
|
||||
foreach ($cartProducts as $cartProduct)
|
||||
if (in_array($cartProduct['id_product'], $productRule['values']))
|
||||
$matchingProducts += $cartProduct['quantity'];
|
||||
if ($matchingProducts < $productRule['quantity'])
|
||||
return Tools::displayError('You cannot use this voucher with these products');
|
||||
break;
|
||||
case 'categories':
|
||||
$cartCategories = Db::getInstance()->ExecuteS('
|
||||
SELECT cp.quantity, catp.`id_category`
|
||||
FROM `'._DB_PREFIX_.'cart_product` cp
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_product` catp ON cp.id_product = catp.id_product
|
||||
WHERE cp.`id_cart` = '.(int)$context->cart->id);
|
||||
$matchingProducts = 0;
|
||||
foreach ($cartCategories as $cartCategory)
|
||||
if (in_array($cartCategory['id_category'], $productRule['values']))
|
||||
$matchingProducts += $cartCategory['quantity'];
|
||||
if ($matchingProducts < $productRule['quantity'])
|
||||
return Tools::displayError('You cannot use this voucher with these products');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the cart rule is only usable by a specific customer, and if the current customer is the right one
|
||||
if ($this->id_customer && $context->cart->id_customer != $this->id_customer)
|
||||
{
|
||||
if (!Context::getContext()->customer->isLogged())
|
||||
return Tools::displayError('You cannot use this voucher').' - '.Tools::displayError('Please log in');
|
||||
return Tools::displayError('You cannot use this voucher');
|
||||
}
|
||||
|
||||
if ($this->minimum_amount)
|
||||
{
|
||||
$minimum_amount = $this->minimum_amount;
|
||||
if ($this->minimum_amount_currency != Configuration::get('PS_CURRENCY_DEFAULT'))
|
||||
{
|
||||
$minimumAmountCurrency = new Currency($this->minimum_amount);
|
||||
$minimum_amount = $this->minimum_amount / $minimumAmountCurrency->convertion_rate;
|
||||
}
|
||||
$cartTotal = $context->cart->getOrderTotal($this->minimum_amount_tax, Cart::ONLY_PRODUCTS);
|
||||
if ($this->minimum_amount_shipping)
|
||||
$cartTotal += $context->cart->getOrderTotal($this->minimum_amount_tax, Cart::ONLY_SHIPPING);
|
||||
if ($cartTotal < $minimum_amount)
|
||||
return Tools::displayError('You do not reach the minimum amount required to use this voucher');
|
||||
}
|
||||
}
|
||||
|
||||
// The reduction value is POSITIVE
|
||||
public function getValue($useTax, Context $context = NULL)
|
||||
{
|
||||
if (!CartRule::isFeatureActive())
|
||||
return 0;
|
||||
if (!$context)
|
||||
$context = Context::getContext();
|
||||
|
||||
$reductionValue = 0;
|
||||
|
||||
// Free shipping on selected carriers
|
||||
if ($this->free_shipping)
|
||||
{
|
||||
if (!$this->carrier_restriction)
|
||||
$reductionValue += $context->cart->getOrderShippingCost($context->cart->id_carrier, $useTax = true, $context->country);
|
||||
elseif ($context->cart->id_carrier)
|
||||
{
|
||||
$id_cart_rule = (int)Db::getInstance()->getValue('
|
||||
SELECT crc.id_cart_rule
|
||||
FROM '._DB_PREFIX_.'cart_rule_carrier crc
|
||||
WHERE crc.id_cart_rule = '.(int)$this->id.'
|
||||
AND crc.id_carrier = '.(int)$context->cart->id_carrier);
|
||||
if ($id_cart_rule)
|
||||
$reductionValue += $context->cart->getOrderShippingCost($context->cart->id_carrier, $useTax, $context->country);
|
||||
}
|
||||
}
|
||||
|
||||
// Discount (%) on the whole order
|
||||
if ($this->reduction_percent AND $this->reduction_product == 0)
|
||||
{
|
||||
$reductionValue += $context->cart->getOrderTotal($useTax, Cart::ONLY_PRODUCTS) * $this->reduction_percent / 100;
|
||||
}
|
||||
|
||||
// Discount (%) on a specific product
|
||||
if ($this->reduction_percent AND $this->reduction_product > 0)
|
||||
{
|
||||
foreach ($context->cart->getProducts() as $product)
|
||||
if ($product['id_product'] == $this->reduction_product)
|
||||
$reductionValue += ($useTax ? $product['total_wt'] : $product['total']) * $this->reduction_percent / 100;
|
||||
}
|
||||
|
||||
// Discount (%) on the cheapest product
|
||||
if ($this->reduction_percent AND $this->reduction_product == -1)
|
||||
{
|
||||
$minPrice = false;
|
||||
foreach ($context->cart->getProducts() as $product)
|
||||
{
|
||||
$price = ($useTax ? $product['price_wt'] : $product['price']);
|
||||
if ($price > 0 && ($minPrice === false || $minPrice > $price))
|
||||
$minPrice = $price;
|
||||
$reductionValue += $minPrice * $this->reduction_percent / 100;
|
||||
}
|
||||
}
|
||||
|
||||
// Discount (¤)
|
||||
if ($this->reduction_amount)
|
||||
{
|
||||
$reduction_amount = $this->reduction_amount;
|
||||
// If we need to convert the voucher value to the cart currency
|
||||
if ($this->reduction_currency != $context->currency->id)
|
||||
{
|
||||
$voucherCurrency = new Currency($this->reduction_currency);
|
||||
// First we convert the voucher value to the default currency
|
||||
$reduction_amount /= $voucherCurrency->conversion_rate;
|
||||
// Then we convert the voucher value in the default currency into the cart currency
|
||||
$reduction_amount *= $context->currency->conversion_rate;
|
||||
|
||||
$reduction_amount = Tools::ps_round($reduction_amount);
|
||||
}
|
||||
|
||||
// If it has the same tax application that you need, then it's the right value, whatever the product!
|
||||
if ($this->reduction_tax == $useTax)
|
||||
$reductionValue += $reduction_amount;
|
||||
else
|
||||
{
|
||||
if ($this->reduction_product > 0)
|
||||
{
|
||||
// Todo: optimize with an array_search (and do the same in the other foreach of this function)
|
||||
foreach ($context->cart->getProducts() as $product)
|
||||
if ($product['id_product'] == $this->reduction_product)
|
||||
{
|
||||
$product_price_ti = $product['price_wt'];
|
||||
$product_price_te = $product['price'];
|
||||
$product_vat_amount = $product_price_ti - $product_price_te;
|
||||
$product_vat_rate = $product_vat_amount / $product_price_te;
|
||||
if ($this->reduction_tax && !$useTax)
|
||||
$reductionValue += $reduction_amount / (1 + $product_vat_rate);
|
||||
elseif (!$this->reduction_tax && $useTax)
|
||||
$reductionValue += $reduction_amount * (1 + $product_vat_rate);
|
||||
}
|
||||
}
|
||||
// Discount (¤) on the whole order
|
||||
elseif ($this->reduction_product == 0)
|
||||
{
|
||||
$cart_amount_ti = $context->cart->getOrderTotal(true, Cart::ONLY_PRODUCTS);
|
||||
$cart_amount_te = $context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS);
|
||||
$cart_vat_amount = $cart_amount_ti - $cart_amount_te;
|
||||
$cart_average_vat_rate = $cart_vat_amount / $cart_amount_te;
|
||||
if ($this->reduction_tax && !$useTax)
|
||||
$reductionValue += $reduction_amount / (1 + $cart_average_vat_rate);
|
||||
elseif (!$this->reduction_tax && $useTax)
|
||||
$reductionValue += $reduction_amount * (1 + $cart_average_vat_rate);
|
||||
}
|
||||
// Todo: discount on the cheapest (but this is not meaningful)
|
||||
}
|
||||
}
|
||||
|
||||
// Free gift
|
||||
if ($this->gift_product)
|
||||
{
|
||||
foreach ($context->cart->getProducts() as $product)
|
||||
if ($product['id_product'] == $this->gift_product)
|
||||
$reductionValue += ($useTax ? $product['price_wt'] : $product['price']);
|
||||
}
|
||||
|
||||
return $reductionValue;
|
||||
}
|
||||
|
||||
protected function getCartRuleCombinations()
|
||||
{
|
||||
$array = array();
|
||||
$array['selected'] = Db::getInstance()->ExecuteS('
|
||||
SELECT cr.*, crl.*, 1 as selected
|
||||
FROM '._DB_PREFIX_.'cart_rule cr
|
||||
LEFT JOIN '._DB_PREFIX_.'cart_rule_lang crl ON (cr.id_cart_rule = crl.id_cart_rule AND crl.id_lang = '.(int)Context::getContext()->language->id.')
|
||||
WHERE cr.id_cart_rule != '.(int)$this->id.'
|
||||
AND (
|
||||
cr.cart_rule_restriction = 0
|
||||
OR cr.id_cart_rule IN (
|
||||
SELECT IF(id_cart_rule_1 = '.(int)$this->id.', id_cart_rule_2, id_cart_rule_1)
|
||||
FROM '._DB_PREFIX_.'cart_rule_combination
|
||||
WHERE '.(int)$this->id.' = id_cart_rule_1
|
||||
OR '.(int)$this->id.' = id_cart_rule_2
|
||||
)
|
||||
)');
|
||||
$array['unselected'] = Db::getInstance()->ExecuteS('
|
||||
SELECT cr.*, crl.*, 1 as selected
|
||||
FROM '._DB_PREFIX_.'cart_rule cr
|
||||
LEFT JOIN '._DB_PREFIX_.'cart_rule_lang crl ON (cr.id_cart_rule = crl.id_cart_rule AND crl.id_lang = '.(int)Context::getContext()->language->id.')
|
||||
WHERE cr.cart_rule_restriction = 1
|
||||
AND cr.id_cart_rule != '.(int)$this->id.'
|
||||
AND cr.id_cart_rule NOT IN (
|
||||
SELECT IF(id_cart_rule_1 = '.(int)$this->id.', id_cart_rule_2, id_cart_rule_1)
|
||||
FROM '._DB_PREFIX_.'cart_rule_combination
|
||||
WHERE '.(int)$this->id.' = id_cart_rule_1
|
||||
OR '.(int)$this->id.' = id_cart_rule_2
|
||||
)');
|
||||
return $array;
|
||||
}
|
||||
|
||||
public function getAssociatedRestrictions($type, $active = 1)
|
||||
{
|
||||
$array = array('selected' => array(), 'unselected' => array());
|
||||
|
||||
if (!in_array($type, array('country', 'carrier', 'group', 'cart_rule')))
|
||||
return false;
|
||||
|
||||
if (!Validate::isLoadedObject($this) OR $this->{$type.'_restriction'} == 0)
|
||||
{
|
||||
$array['selected'] = Db::getInstance()->ExecuteS('
|
||||
SELECT t.*, tl.*, 1 as selected
|
||||
FROM '._DB_PREFIX_.''.$type.' t
|
||||
LEFT JOIN '._DB_PREFIX_.''.$type.'_lang tl ON t.id_'.$type.' = tl.id_'.$type.' AND tl.id_lang = '.(int)Context::getContext()->language->id.'
|
||||
WHERE 1
|
||||
'.($active ? 'AND t.active = 1' : '').'
|
||||
'.($type == 'cart_rule' ? 'AND t.id_cart_rule != '.(int)$this->id : '').'
|
||||
ORDER BY name ASC');
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($type == 'cart_rule')
|
||||
$array = $this->getCartRuleCombinations();
|
||||
else
|
||||
{
|
||||
$result = Db::getInstance()->ExecuteS('
|
||||
SELECT t.*, tl.*, IF(crt.id_'.$type.' IS NULL, 0, 1) as selected
|
||||
FROM '._DB_PREFIX_.''.$type.' t
|
||||
LEFT JOIN '._DB_PREFIX_.''.$type.'_lang tl ON t.id_'.$type.' = tl.id_'.$type.' AND tl.id_lang = '.(int)Context::getContext()->language->id.'
|
||||
LEFT JOIN (SELECT id_'.$type.' FROM '._DB_PREFIX_.'cart_rule_'.$type.' WHERE id_cart_rule = '.(int)$this->id.') crt ON t.id_'.$type.' = crt.id_'.$type.'
|
||||
'.($active ? 'WHERE t.active = 1' : '').'
|
||||
ORDER BY name ASC',
|
||||
false);
|
||||
while ($row = Db::getInstance()->nextRow())
|
||||
$array[($row['selected'] || $this->{$type.'_restriction'} == 0) ? 'selected' : 'unselected'][] = $row;
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function autoRemoveFromCart($context = NULL)
|
||||
{
|
||||
if (!CartRule::isFeatureActive())
|
||||
return;
|
||||
|
||||
$errors = array();
|
||||
if (!$context)
|
||||
$context = Context::getContext();
|
||||
$result = $context->cart->getCartRules();
|
||||
$cartRules = ObjectModel::hydrateCollection('CartRule', $result);
|
||||
foreach ($cartRules as $cartRule)
|
||||
{
|
||||
if ($error = $cartRule->checkValidity($context, true))
|
||||
{
|
||||
$context->cart->removeCartRule($cartRule->id);
|
||||
$context->cart->update();
|
||||
$errors[] = $error;
|
||||
}
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
|
||||
public static function autoAddToCart($context = NULL)
|
||||
{
|
||||
if ($context === NULL)
|
||||
$context = Context::getContext();
|
||||
if (!CartRule::isFeatureActive() || !Validate::isLoadedObject($context->cart))
|
||||
return;
|
||||
|
||||
$result = Db::getInstance()->ExecuteS('
|
||||
SELECT cr.*
|
||||
FROM '._DB_PREFIX_.'cart_rule cr
|
||||
LEFT JOIN '._DB_PREFIX_.'cart_rule_carrier crca ON cr.id_cart_rule = crca.id_cart_rule
|
||||
LEFT JOIN '._DB_PREFIX_.'cart_rule_country crco ON cr.id_cart_rule = crco.id_cart_rule
|
||||
WHERE cr.active = 1
|
||||
AND cr.code = ""
|
||||
AND cr.quantity > 0
|
||||
AND cr.date_from < "'.date('Y-m-d H:i:s').'"
|
||||
AND cr.date_to > "'.date('Y-m-d H:i:s').'"
|
||||
AND cr.id_cart_rule NOT IN (SELECT id_cart_rule FROM '._DB_PREFIX_.'cart_cart_rule WHERE id_cart = '.(int)$context->cart->id.')
|
||||
AND (
|
||||
cr.id_customer = 0
|
||||
'.($context->customer->id ? 'OR cr.id_customer = '.(int)$context->cart->id_customer : '').'
|
||||
)
|
||||
AND (
|
||||
cr.carrier_restriction = 0
|
||||
'.($context->cart->id_carrier ? 'OR crca.id_carrier = '.(int)$context->cart->id_carrier : '').'
|
||||
)
|
||||
AND (
|
||||
cr.group_restriction = 0
|
||||
'.($context->customer->id ? 'OR 0 < (
|
||||
SELECT cg.id_group
|
||||
FROM '._DB_PREFIX_.'customer_group cg
|
||||
LEFT JOIN '._DB_PREFIX_.'cart_rule_group crg ON cg.id_group = crg.id_group
|
||||
WHERE cr.id_cart_rule = crg.id_cart_rule
|
||||
AND cg.id_customer = '.(int)$context->customer->id.'
|
||||
)' : '').'
|
||||
)
|
||||
AND (
|
||||
cr.reduction_product = 0
|
||||
OR cr.reduction_product IN (
|
||||
SELECT id_product
|
||||
FROM '._DB_PREFIX_.'cart_product
|
||||
WHERE id_cart = '.(int)$context->cart->id.'
|
||||
)
|
||||
)
|
||||
ORDER BY priority');
|
||||
|
||||
$cartRules = ObjectModel::hydrateCollection('CartRule', $result);
|
||||
|
||||
// Todo: consider optimization (we can avoid many queries in checkValidity)
|
||||
foreach ($cartRules as $cartRule)
|
||||
if (!$cartRule->checkValidity($context))
|
||||
$context->cart->addCartRule($cartRule->id);
|
||||
}
|
||||
|
||||
public static function isFeatureActive()
|
||||
{
|
||||
return (bool)Configuration::get('PS_CART_RULE_FEATURE_ACTIVE');
|
||||
}
|
||||
}
|
||||
@@ -122,6 +122,13 @@ class CountryCore extends ObjectModel
|
||||
$this->validateFieldsLang();
|
||||
return $this->getTranslationsFields(array('name'));
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
if (!parent::delete())
|
||||
return false;
|
||||
return Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'cart_rule_country WHERE id_country = '.(int)$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return available countries
|
||||
|
||||
+10
-9
@@ -25,7 +25,7 @@
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class DiscountCore extends ObjectModel
|
||||
class DiscountCore extends CartRule
|
||||
{
|
||||
public $id;
|
||||
|
||||
@@ -310,14 +310,14 @@ class DiscountCore extends ObjectModel
|
||||
}
|
||||
|
||||
/**
|
||||
* Return discount value
|
||||
*
|
||||
* @param integer $nb_discounts Number of discount currently in cart
|
||||
* @param boolean $order_total_products Total cart products amount
|
||||
* @return mixed Return a float value or '!' if reduction is 'Shipping free'
|
||||
* @deprecated 1.5.0.1
|
||||
*/
|
||||
public function getValue($nb_discounts = 0, $order_total_products = 0, $shipping_fees = 0, $idCart = false, $useTax = true, Currency $currency = null, Shop $shop = null)
|
||||
public function getValue($nb_discounts = 0, $order_total_products = 0, $shipping_fees = 0, $id_cart = false, $useTax = true, Currency $currency = null, Shop $shop = null)
|
||||
{
|
||||
Tools::displayAsDeprecated();
|
||||
$context = Context::getContext();
|
||||
$context->cart = new Cart($id_cart);
|
||||
return parent::getValue($useTax, $context);
|
||||
if (!self::isFeatureActive())
|
||||
return 0;
|
||||
|
||||
@@ -509,7 +509,7 @@ class DiscountCore extends ObjectModel
|
||||
*/
|
||||
public static function getVouchersToCartDisplay($id_lang, $id_customer = 0)
|
||||
{
|
||||
if (!self::isFeatureActive())
|
||||
if (!CartRule::isFeatureActive())
|
||||
return array();
|
||||
|
||||
$sql = '
|
||||
@@ -584,6 +584,7 @@ class DiscountCore extends ObjectModel
|
||||
*/
|
||||
public static function isFeatureActive()
|
||||
{
|
||||
return Configuration::get('PS_DISCOUNT_FEATURE_ACTIVE');
|
||||
Tools::displayAsDeprecated();
|
||||
return CartRule::isFeatureActive();
|
||||
}
|
||||
}
|
||||
@@ -134,6 +134,10 @@ class FrontControllerCore extends Controller
|
||||
if (isset($_GET['logout']) OR ($this->context->customer->logged AND Customer::isBanned($this->context->customer->id)))
|
||||
{
|
||||
$this->context->customer->logout();
|
||||
|
||||
// Login information have changed, so we check if the cart rules still apply
|
||||
CartRule::autoRemoveFromCart();
|
||||
|
||||
Tools::redirect(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL);
|
||||
}
|
||||
elseif (isset($_GET['mylogout']))
|
||||
@@ -285,7 +289,6 @@ class FrontControllerCore extends Controller
|
||||
'customerName' => ($this->context->customer->logged ? $this->context->cookie->customer_firstname.' '.$this->context->cookie->customer_lastname : false)
|
||||
));
|
||||
|
||||
// TODO for better performances (cache usage), remove these assign and use a smarty function to get the right media server in relation to the full ressource name
|
||||
$assignArray = array(
|
||||
'img_ps_dir' => _PS_IMG_,
|
||||
'img_cat_dir' => _THEME_CAT_DIR_,
|
||||
|
||||
@@ -166,6 +166,7 @@ class GroupCore extends ObjectModel
|
||||
return false;
|
||||
if (parent::delete())
|
||||
{
|
||||
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cart_rule_group` WHERE `id_group` = '.(int)$this->id);
|
||||
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'customer_group` WHERE `id_group` = '.(int)$this->id);
|
||||
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'category_group` WHERE `id_group` = '.(int)$this->id);
|
||||
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'group_reduction` WHERE `id_group` = '.(int)$this->id);
|
||||
|
||||
+7
-1
@@ -897,7 +897,13 @@ class OrderCore extends ObjectModel
|
||||
*/
|
||||
public function addDiscount($id_discount, $name, $value)
|
||||
{
|
||||
return Db::getInstance()->AutoExecute(_DB_PREFIX_.'order_discount', array('id_order' => (int)($this->id), 'id_discount' => (int)($id_discount), 'name' => pSQL($name), 'value' => (float)($value)), 'INSERT');
|
||||
Tools::displayAsDeprecated();
|
||||
return Order::addCartRule($id_discount, $name, $value);
|
||||
}
|
||||
|
||||
public function addCartRule($id_cart_rule, $name, $value)
|
||||
{
|
||||
return Db::getInstance()->AutoExecute(_DB_PREFIX_.'order_cart_rule', array('id_order' => (int)$this->id, 'id_cart_rule' => (int)$id_cart_rule, 'name' => pSQL($name), 'value' => (float)$value), 'INSERT');
|
||||
}
|
||||
|
||||
public function getNumberOfDays()
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2011 PrestaShop SA
|
||||
* @version Release: $Revision: 6844 $
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class OrderDiscountCore extends ObjectModel
|
||||
{
|
||||
/** @var integer */
|
||||
public $id_order_cart_rule;
|
||||
|
||||
/** @var integer */
|
||||
public $id_order;
|
||||
|
||||
/** @var integer */
|
||||
public $id_cart_rule;
|
||||
|
||||
/** @var string */
|
||||
public $name;
|
||||
|
||||
/** @var integer */
|
||||
public $value;
|
||||
|
||||
protected $tables = array ('order_cart_rule');
|
||||
|
||||
protected $fieldsRequired = array ('id_order', 'name', 'value');
|
||||
protected $fieldsValidate = array ('id_order' => 'isUnsignedId', 'name' => 'isGenericName', 'value' => 'isInt');
|
||||
|
||||
/* MySQL does not allow 'order detail' for a table name */
|
||||
protected $table = 'order_cart_rule';
|
||||
protected $identifier = 'id_order_cart_rule';
|
||||
|
||||
protected $webserviceParameters = array(
|
||||
'fields' => array(
|
||||
'id_order' => array('xlink_resource' => 'orders'),
|
||||
),
|
||||
);
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
$this->validateFields();
|
||||
|
||||
$fields['id_order'] = (int)($this->id_order);
|
||||
$fields['name'] = pSQL($this->name);
|
||||
$fields['value'] = (int)($this->value);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
}
|
||||
|
||||
+17
-41
@@ -25,47 +25,23 @@
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
class OrderDiscountCore extends ObjectModel
|
||||
class OrderDiscountCore extends OrderCartRule
|
||||
{
|
||||
/** @var integer */
|
||||
public $id_order_discount;
|
||||
|
||||
/** @var integer */
|
||||
public $id_order;
|
||||
|
||||
/** @var integer */
|
||||
public $id_discount;
|
||||
|
||||
/** @var string */
|
||||
public $name;
|
||||
|
||||
/** @var integer */
|
||||
public $value;
|
||||
|
||||
protected $tables = array ('order_discount');
|
||||
|
||||
protected $fieldsRequired = array ('id_order', 'name', 'value');
|
||||
protected $fieldsValidate = array ('id_order' => 'isUnsignedId', 'name' => 'isGenericName', 'value' => 'isInt');
|
||||
|
||||
/* MySQL does not allow 'order detail' for a table name */
|
||||
protected $table = 'order_discount';
|
||||
protected $identifier = 'id_order_discount';
|
||||
|
||||
protected $webserviceParameters = array(
|
||||
'fields' => array(
|
||||
'id_order' => array('xlink_resource' => 'orders'),
|
||||
),
|
||||
);
|
||||
|
||||
public function getFields()
|
||||
public function __get($key)
|
||||
{
|
||||
$this->validateFields();
|
||||
|
||||
$fields['id_order'] = (int)($this->id_order);
|
||||
$fields['name'] = pSQL($this->name);
|
||||
$fields['value'] = (int)($this->value);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
if ($key == 'id_order_discount')
|
||||
return $this->id_order_cart_rule;
|
||||
if ($key == 'id_discount')
|
||||
return $this->id_cart_rule;
|
||||
return $this->{$key};
|
||||
}
|
||||
|
||||
public function __set($key, $value)
|
||||
{
|
||||
if ($key == 'id_order_discount')
|
||||
$this->id_order_cart_rule = $value;
|
||||
if ($key == 'id_discount')
|
||||
$this->id_cart_rule = $value;
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ class OrderHistoryCore extends ObjectModel
|
||||
if ($newOS->logable AND (!$oldOrderStatus OR !$oldOrderStatus->logable))
|
||||
ProductSale::addProductSale($product['id_product'], $product['cart_quantity']);
|
||||
/* If becoming unlogable => removing sale */
|
||||
else if (!$newOS->logable AND ($oldOrderStatus AND $oldOrderStatus->logable))
|
||||
elseif (!$newOS->logable AND ($oldOrderStatus AND $oldOrderStatus->logable))
|
||||
ProductSale::removeProductSale($product['id_product'], $product['cart_quantity']);
|
||||
|
||||
if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && !$isValidated AND $newOS->logable AND isset($oldOrderStatus) AND $oldOrderStatus AND $oldOrderStatus->id == Configuration::get('PS_OS_ERROR'))
|
||||
|
||||
+33
-44
@@ -245,53 +245,42 @@ abstract class PaymentModuleCore extends Module
|
||||
</tr>';
|
||||
} // end foreach ($products)
|
||||
|
||||
// Insert discounts from cart into order_discount table
|
||||
$discounts = $cart->getDiscounts();
|
||||
$discountsList = '';
|
||||
$total_discount_value = 0;
|
||||
$shrunk = false;
|
||||
foreach ($discounts AS $discount)
|
||||
$cartRulesList = '';
|
||||
$result = $cart->getCartRules();
|
||||
$cartRules = ObjectModel::hydrateCollection('CartRule', $result, (int)$order->id_lang);
|
||||
foreach ($cartRules AS $cartRule)
|
||||
{
|
||||
$objDiscount = new Discount((int)$discount['id_discount']);
|
||||
$value = $objDiscount->getValue(sizeof($discounts), $cart->getOrderTotal(true, Cart::ONLY_PRODUCTS), $order->total_shipping, $cart->id);
|
||||
if ($objDiscount->id_discount_type == Discount::AMOUNT AND in_array($objDiscount->behavior_not_exhausted, array(1,2)))
|
||||
$shrunk = true;
|
||||
$value = $cartRule->getValue(true);
|
||||
// Todo: repair shrunk
|
||||
// if ($shrunk AND ($total_discount_value + $value) > ($order->total_products_wt + $order->total_shipping + $order->total_wrapping))
|
||||
// {
|
||||
// $amount_to_add = ($order->total_products_wt + $order->total_shipping + $order->total_wrapping) - $total_discount_value;
|
||||
// if ($cartRule->id_discount_type == Discount::AMOUNT AND $cartRule->behavior_not_exhausted == 2)
|
||||
// {
|
||||
// $voucher = new Discount();
|
||||
// foreach ($cartRule AS $key => $discountValue)
|
||||
// $voucher->$key = $discountValue;
|
||||
// $voucher->name = 'VSRK'.(int)$order->id_customer.'O'.(int)$order->id;
|
||||
// $voucher->value = (float)$value - $amount_to_add;
|
||||
// $voucher->add();
|
||||
// $params['{voucher_amount}'] = Tools::displayPrice($voucher->value, $currency, false);
|
||||
// $params['{voucher_num}'] = $voucher->name;
|
||||
// $params['{firstname}'] = $customer->firstname;
|
||||
// $params['{lastname}'] = $customer->lastname;
|
||||
// $params['{id_order}'] = $order->id;
|
||||
// @Mail::Send((int)$order->id_lang, 'voucher', Mail::l('New voucher regarding your order #').$order->id, $params, $customer->email, $customer->firstname.' '.$customer->lastname);
|
||||
// }
|
||||
// }
|
||||
|
||||
if ($shrunk AND ($total_discount_value + $value) > ($order->total_products_wt + $order->total_shipping + $order->total_wrapping))
|
||||
{
|
||||
$amount_to_add = ($order->total_products_wt + $order->total_shipping + $order->total_wrapping) - $total_discount_value;
|
||||
if ($objDiscount->id_discount_type == Discount::AMOUNT AND $objDiscount->behavior_not_exhausted == 2)
|
||||
{
|
||||
$voucher = new Discount();
|
||||
foreach ($objDiscount AS $key => $discountValue)
|
||||
$voucher->$key = $discountValue;
|
||||
$voucher->name = 'VSRK'.(int)$order->id_customer.'O'.(int)$order->id;
|
||||
$voucher->value = (float)$value - $amount_to_add;
|
||||
$voucher->add();
|
||||
$params['{voucher_amount}'] = Tools::displayPrice($voucher->value, $currency, false);
|
||||
$params['{voucher_num}'] = $voucher->name;
|
||||
$params['{firstname}'] = $customer->firstname;
|
||||
$params['{lastname}'] = $customer->lastname;
|
||||
$params['{id_order}'] = $order->id;
|
||||
@Mail::Send((int)$order->id_lang, 'voucher', Mail::l('New voucher regarding your order #').$order->id, $params, $customer->email, $customer->firstname.' '.$customer->lastname);
|
||||
}
|
||||
}
|
||||
else
|
||||
$amount_to_add = $value;
|
||||
$order->addDiscount($objDiscount->id, $objDiscount->name, $amount_to_add);
|
||||
$total_discount_value += $amount_to_add;
|
||||
$order->addCartRule($cartRule->id, $cartRule->name, $value);
|
||||
if ($id_order_state != Configuration::get('PS_OS_ERROR') AND $id_order_state != Configuration::get('PS_OS_CANCELED'))
|
||||
$objDiscount->quantity = $objDiscount->quantity - 1;
|
||||
$objDiscount->update();
|
||||
$cartRule->quantity = $cartRule->quantity - 1;
|
||||
$cartRule->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;">'.
|
||||
($value != 0.00 ? '-' : '').Tools::displayPrice($value, $currency, false).
|
||||
'</td>
|
||||
$cartRulesList .= '
|
||||
<tr style="background-color:#EBECEE;">
|
||||
<td colspan="4" style="padding:0.6em 0.4em;text-align:right">'.$this->l('Voucher name:').' '.$cartRule->name.'</td>
|
||||
<td style="padding:0.6em 0.4em;text-align:right">'.($value != 0.00 ? '-' : '').Tools::displayPrice($value, $currency, false).'</td>
|
||||
</tr>';
|
||||
}
|
||||
|
||||
@@ -384,7 +373,7 @@ abstract class PaymentModuleCore extends Module
|
||||
'{carrier}' => $carrier->name,
|
||||
'{payment}' => Tools::substr($order->payment, 0, 32),
|
||||
'{products}' => $productsList,
|
||||
'{discounts}' => $discountsList,
|
||||
'{discounts}' => $cartRulesList,
|
||||
'{total_paid}' => Tools::displayPrice($order->total_paid, $currency, false),
|
||||
'{total_products}' => Tools::displayPrice($order->total_paid - $order->total_shipping - $order->total_wrapping + $order->total_discounts, $currency, false),
|
||||
'{total_discounts}' => Tools::displayPrice($order->total_discounts, $currency, false),
|
||||
|
||||
Reference in New Issue
Block a user