[*] BO : you can now choose the combination of the free product of a cart rule

[*] FO : the free product is now removed from the cart along with the cart rule (where applicable)
This commit is contained in:
dMetzger
2012-02-20 17:04:41 +00:00
parent 26e79ff58e
commit f01657a576
9 changed files with 229 additions and 70 deletions
+28 -23
View File
@@ -705,7 +705,7 @@ class CartCore extends ObjectModel
Cache::clean('Cart::getCartRules'.$this->id);
if ((int)$cartRule->gift_product)
return $this->updateQty(1, $cartRule->gift_product);
$this->updateQty(1, $cartRule->gift_product, $cartRule->gift_product_attribute);
return true;
}
@@ -751,8 +751,8 @@ class CartCore extends ObjectModel
{
if ($id_address_delivery == 0) // The $id_address_delivery is null, get the default customer address
$id_address_delivery = (int)Address::getFirstCustomerAddressId((int)Context::getContext()->customer->id);
else if (!Customer::customerHasAddress(Context::getContext()->customer->id, $id_address_delivery)) // The $id_address_delivery must be linked with customer
$id_address_delivery = 0;
elseif (!Customer::customerHasAddress(Context::getContext()->customer->id, $id_address_delivery)) // The $id_address_delivery must be linked with customer
$id_address_delivery = 0;
}
$quantity = (int)$quantity;
@@ -1019,11 +1019,18 @@ class CartCore extends ObjectModel
public function removeCartRule($id_cart_rule)
{
Cache::clean('Cart::getCartRules'.$this->id);
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'
);
$result = 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');
$cart_rule = new CartRule($id_cart_rule, Configuration::get('PS_LANG_DEFAULT'));
if ((int)$cart_rule->gift_product)
$this->updateQty(1, $cart_rule->gift_product, $cart_rule->gift_product_attribute, null, null, 'down');
return $result;
}
/**
@@ -1051,14 +1058,13 @@ class CartCore extends ObjectModel
AND `id_product_attribute` = '.(int)$id_product_attribute
);
$customization_quantity = (int)Db::getInstance()->getValue(
'SELECT `quantity`
FROM `'._DB_PREFIX_.'customization`
WHERE `id_cart` = '.(int)$this->id.'
AND `id_product` = '.(int)$id_product.'
AND `id_product_attribute` = '.(int)$id_product_attribute.'
AND `id_address_delivery` = '.(int)$id_address_delivery
);
$customization_quantity = (int)Db::getInstance()->getValue('
SELECT `quantity`
FROM `'._DB_PREFIX_.'customization`
WHERE `id_cart` = '.(int)$this->id.'
AND `id_product` = '.(int)$id_product.'
AND `id_product_attribute` = '.(int)$id_product_attribute.'
'.((int)$id_address_delivery ? 'AND `id_address_delivery` = '.(int)$id_address_delivery : ''));
if (!$this->_deleteCustomization((int)$id_customization, (int)$id_product, (int)$id_product_attribute))
return false;
@@ -1091,13 +1097,12 @@ class CartCore extends ObjectModel
);
/* Product deletion */
$result = Db::getInstance()->execute(
'DELETE FROM `'._DB_PREFIX_.'cart_product`
WHERE `id_product` = '.(int)$id_product.
(!is_null($id_product_attribute) ? ' AND `id_product_attribute` = '.(int)$id_product_attribute : '').'
AND `id_cart` = '.(int)$this->id.'
AND `id_address_delivery` = '.(int)$id_address_delivery
);
$result = Db::getInstance()->execute('
DELETE FROM `'._DB_PREFIX_.'cart_product`
WHERE `id_product` = '.(int)$id_product.'
'.(!is_null($id_product_attribute) ? ' AND `id_product_attribute` = '.(int)$id_product_attribute : '').'
AND `id_cart` = '.(int)$this->id.'
'.((int)$id_address_delivery ? 'AND `id_address_delivery` = '.(int)$id_address_delivery : ''));
if ($result)
{
+14 -5
View File
@@ -55,6 +55,7 @@ class CartRuleCore extends ObjectModel
public $reduction_currency;
public $reduction_product;
public $gift_product;
public $gift_product_attribute;
public $active = 1;
public $date_add;
public $date_upd;
@@ -93,6 +94,7 @@ class CartRuleCore extends ObjectModel
'reduction_currency' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'reduction_product' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'gift_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'gift_product_attribute' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
@@ -463,24 +465,31 @@ class CartRuleCore extends ObjectModel
// If a product is given for free in this rule and already in the cart, the price is subtracted
if ($this->gift_product)
{
$in_cart = (bool)Db::getInstance()->getValue('SELECT id_product FROM '._DB_PREFIX_.'cart_product WHERE id_product = '.(int)$this->gift_product.' AND id_cart = '.(int)$context->cart->id);
$in_cart = (bool)Db::getInstance()->getValue('
SELECT id_product
FROM '._DB_PREFIX_.'cart_product
WHERE id_product = '.(int)$this->gift_product.'
AND id_product_attribute = '.(int)$this->gift_product_attribute.'
AND id_cart = '.(int)$context->cart->id);
if ($in_cart)
{
$ref = false;
$product_price = Product::getPriceStatic(
$this->gift_product,
$this->minimum_amount_tax,
null, null, null, null, null, null, null,
$this->gift_product_attribute,
null, null, null, null, null, null,
$context->cart->id_customer ? $context->cart->id_customer : null,
$context->cart->id,
(int)$context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')} ? (int)$context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')} : null,
$ref, null, null,
$context, null
);
$cartTotal -= $product_price;
}
}
if ($cartTotal < $minimum_amount)
return Tools::displayError('You do not reach the minimum amount required to use this voucher');
}
@@ -762,10 +771,10 @@ class CartRuleCore extends ObjectModel
}
// Free gift
if ($this->gift_product)
if ((int)$this->gift_product)
{
foreach ($context->cart->getProducts() as $product)
if ($product['id_product'] == $this->gift_product)
if ($product['id_product'] == $this->gift_product && $product['id_product_attribute'] == $this->gift_product_attribute)
$reduction_value += ($useTax ? $product['price_wt'] : $product['price']);
}