// Retro compatibility and updater improved for Discount/CartRules

This commit is contained in:
dMetzger
2011-11-02 14:23:44 +00:00
parent 61213efb5b
commit 4fbb25168e
21 changed files with 238 additions and 275 deletions
+8 -31
View File
@@ -87,8 +87,6 @@ class CartCore extends ObjectModel
protected $_products = NULL;
protected static $_totalWeight = array();
protected $_taxCalculationMethod = PS_TAX_EXC;
protected static $_discounts = NULL;
protected static $_discountsLite = NULL;
protected static $_carriers = NULL;
protected static $_taxes_rate = NULL;
protected static $_attributesLists = array();
@@ -256,13 +254,6 @@ class CartCore extends ObjectModel
return $this->getCartRules();
}
/**
* Return cart discounts
*
* @param bool true will return discounts with basic informations
* @param bool true will erase the cache
* @result array Discounts
*/
public function getCartRules()
{
// TODO : add cache
@@ -552,10 +543,10 @@ class CartCore extends ObjectModel
/**
* @deprecated 1.5.0.1
*/
public function addDiscount($id_discount)
public function addDiscount($id_cart_rule)
{
Tools::displayAsDeprecated();
return $this->addCartRule($id_discount);
return $this->addCartRule($id_cart_rule);
}
public function addCartRule($id_cart_rule)
@@ -809,10 +800,10 @@ class CartCore extends ObjectModel
/**
* @deprecated 1.5.0.1
*/
public function deleteDiscount($id_discount)
public function deleteDiscount($id_cart_rule)
{
Tools::displayAsDeprecated();
return $this->removeCartRule($id_discount);
return $this->removeCartRule($id_cart_rule);
}
public function removeCartRule($id_cart_rule)
@@ -966,8 +957,7 @@ class CartCore extends ObjectModel
if (!in_array($type, array(Cart::ONLY_PRODUCTS, Cart::ONLY_DISCOUNTS, Cart::BOTH, Cart::BOTH_WITHOUT_SHIPPING, Cart::ONLY_SHIPPING, Cart::ONLY_WRAPPING, Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING)))
die(Tools::displayError());
// if discounts are never used
// Todo: remove and replace by cart rules
// if cart rules are not used
if ($type == Cart::ONLY_DISCOUNTS && !CartRule::isFeatureActive())
return 0;
// no shipping cost if is a cart with only virtuals products
@@ -1289,12 +1279,12 @@ class CartCore extends ObjectModel
/**
* @deprecated 1.5.0.1
*/
public function checkDiscountValidity($discountObj, $discounts, $order_total, $products, $checkCartDiscount = false)
public function checkDiscountValidity($obj, $discounts, $order_total, $products, $checkCartDiscount = false)
{
Tools::displayAsDeprecated();
$context = Context::getContext()->cloneContext();
$context->cart = $this;
return $discountObj->checkValidity($context);
return $obj->checkValidity($context);
}
/**
@@ -1319,18 +1309,6 @@ class CartCore extends ObjectModel
if ($total_tax < 0)
$total_tax = 0;
$total_free_ship = 0;
if ($free_ship = Tools::convertPrice((float)(Configuration::get('PS_SHIPPING_FREE_PRICE')), new Currency((int)($this->id_currency))))
{
$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)
{
$total_free_ship = 0;
break;
}
}
return array(
'delivery' => $delivery,
'delivery_state' => State::getNameById($delivery->id_state),
@@ -1351,8 +1329,7 @@ class CartCore extends ObjectModel
'total_products' => $this->getOrderTotal(false, Cart::ONLY_PRODUCTS),
'total_price' => $this->getOrderTotal(),
'total_tax' => $total_tax,
'total_price_without_tax' => $this->getOrderTotal(false),
'free_ship' => $total_free_ship);
'total_price_without_tax' => $this->getOrderTotal(false));
}
public function checkQuantities()
+19 -21
View File
@@ -614,29 +614,32 @@ class OrderCore extends ObjectModel
}
return $virtual;
}
/**
* Get order discounts
*
* @return array Discounts with price and quantity
* @deprecated 1.5.0.1
*/
public function getDiscounts($details = false)
{
Tools::displayAsDeprecated();
return Order::getCartRules();
}
public function getCartRules()
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT *
FROM `'._DB_PREFIX_.'order_cart_rule` od '.
($details ? 'LEFT JOIN `'._DB_PREFIX_.'discount` d ON (d.`id_discount` = od.`id_discount`)' : '').'
WHERE od.`id_order` = '.(int)($this->id));
FROM `'._DB_PREFIX_.'order_cart_rule` ocr
LEFT JOIN `'._DB_PREFIX_.'cart_rule` cr ON cr.`id_cart_rule` = ocr.`id_cart_rule`
WHERE ocr.`id_order` = '.(int)$this->id);
}
public static function getDiscountsCustomer($id_customer, $id_discount)
public static function getDiscountsCustomer($id_customer, $id_cart_rule)
{
return Db::getInstance()->getValue('
SELECT COUNT(*) FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN '._DB_PREFIX_.'order_cart_rule od ON (od.id_order = o.id_order)
WHERE o.id_customer = '.(int)($id_customer).'
AND od.id_discount = '.(int)($id_discount));
SELECT COUNT(*) FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN '._DB_PREFIX_.'order_cart_rule ocr ON (ocr.id_order = o.id_order)
WHERE o.id_customer = '.(int)$id_customer.'
AND ocr.id_cart_rule = '.(int)$id_cart_rule);
}
/**
@@ -888,17 +891,12 @@ class OrderCore extends ObjectModel
}
/**
* Add a discount to order
*
* @param integer $id_discount Discount id
* @param string $name Discount name
* @param float $value Discount value
* @return boolean Query sucess or not
* @deprecated 1.5.0.1
*/
public function addDiscount($id_discount, $name, $value)
public function addDiscount($id_cart_rule, $name, $value)
{
Tools::displayAsDeprecated();
return Order::addCartRule($id_discount, $name, $value);
return Order::addCartRule($id_cart_rule, $name, $value);
}
public function addCartRule($id_cart_rule, $name, $value)
+8 -7
View File
@@ -99,7 +99,7 @@ class OrderSlipCore extends ObjectModel
public static function getOrdersSlipProducts($orderSlipId, $order)
{
$discounts = $order->getDiscounts(true);
$cart_rules = $order->getCartRules(true);
$productsRet = self::getOrdersSlipDetail($orderSlipId);
$products = $order->getProductsDetail();
@@ -112,16 +112,17 @@ class OrderSlipCore extends ObjectModel
{
$resTab[$key] = $product;
$resTab[$key]['product_quantity'] = $tmp[$product['id_order_detail']];
if (sizeof($discounts))
if (sizeof($cart_rules))
{
$order->setProductPrices($product);
$realProductPrice = $resTab[$key]['product_price'];
foreach ($discounts as $discount)
// Todo : must be updated to use the cart rules
foreach ($cart_rules as $cart_rule)
{
if ($discount['id_discount_type'] == Discount::PERCENT)
$resTab[$key]['product_price'] -= $realProductPrice * ($discount['value'] / 100);
elseif ($discount['id_discount_type'] == Discount::AMOUNT)
$resTab[$key]['product_price'] -= (($discount['value'] * ($product['product_price_wt'] / $order->total_products_wt)) / (1.00 + ($product['tax_rate'] / 100)));
if ($cart_rule['reduction_percent'])
$resTab[$key]['product_price'] -= $realProductPrice * ($cart_rule['reduction_percent'] / 100);
elseif ($cart_rule['reduction_amount'])
$resTab[$key]['product_price'] -= (($cart_rule['reduction_amount'] * ($product['product_price_wt'] / $order->total_products_wt)) / (1.00 + ($product['tax_rate'] / 100)));
}
}
+4 -4
View File
@@ -260,14 +260,14 @@ class ValidateCore
}
/**
* Check for discount coupon name validity
* Check for voucher name validity
*
* @param string $discountName Discount coupon name to validate
* @param string $voucher voucher to validate
* @return boolean Validity is ok or not
*/
public static function isDiscountName($discountName)
public static function isDiscountName($voucher)
{
return preg_match('/^[^!<>,;?=+()@"°{}_$%:]{3,32}$/u', $discountName);
return preg_match('/^[^!<>,;?=+()@"°{}_$%:]{3,32}$/u', $voucher);
}
/**
-1
View File
@@ -68,7 +68,6 @@ class ShopCore extends ObjectModel
'contact' => array('type' => 'shop'),
'country' => array('type' => 'shop'),
'currency' => array('type' => 'shop'),
'discount' => array('type' => 'shop'),
'employee' => array('type' => 'shop'),
'hook_module' => array('type' => 'fk_shop'),
'hook_module_exceptions' => array('type' => 'fk_shop', 'primary' => 'id_hook_module_exceptions'),