Christophe Latour b3a3231f24 #10762
Attente d'un visuel pour la popup.
Attente des règles à mettre en place.
2016-09-08 18:10:47 +02:00

489 lines
22 KiB
PHP

<?php
class CartRule extends CartRuleCore
{
/**
* Check if this cart rule can be applied
*
* @param Context $context
* @param bool $alreadyInCart Check if the voucher is already on the cart
* @param bool $display_error Display error
* @return bool|mixed|string
*/
public function checkValidity(Context $context, $alreadyInCart = false, $display_error = true)
{
#10762 -> Bon de bienvenue
if ($this->id == 50084) {
$date_add = $context->customer->date_add;
$date_to_available = date('Y-m-d', strtotime("+1 month", strtotime($date_add)));
$date_now = date('Y-m-d H:i:s');
if ($date_now > $date_to_available) {
return $display_error ?
Tools::displayError('Ce bon est utilisable uniquement pour les nouveaux utilisateurs (< 1 mois)') : false;
}
}
$res = Parent::checkValidity($context, $alreadyInCart, $display_error);
$success = Parent::checkValidity($context, $alreadyInCart, false);
if (!$success)
return (!$display_error) ? false : $res;
if ($this->getCategoryRestriction()){
$r = $this->checkCategoryRestriction($context, $display_error, $alreadyInCart);
if ($r !== false && $display_error)
return $r;
elseif (!$r && !$display_error)
return false;
}
// Check pour compatibilité Bon LIVRAISON15 et bons générés en masse
/*$step = (int)(Tools::getValue('step'));
if ($step == 0 && ($context->cart->id_customer == 94202 || $context->cart->id_customer == 221)) {
$cartRulesAlreadyInCart = $context->cart->getCartRules(CartRule::FILTER_ACTION_SHIPPING);
if ($cartRulesAlreadyInCart){
$totalWithoutShipping = floatval($context->cart->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING));
$totalWithoutShippingAndTax = floatval($context->cart->getOrderTotal(false, Cart::BOTH_WITHOUT_SHIPPING));
foreach ($cartRulesAlreadyInCart as $key => $cartrule) {
$totalPrice = 0;
$minimum = 0;
if ($this->minimum_amount_tax && $cartrule['minimum_amount_tax'] && $cartrule['minimum_amount']>0) {
$totalPrice = $totalWithoutShipping - $this->minimum_amount;
$minimum = $cartrule['minimum_amount'];
} elseif (!$this->minimum_amount_tax && !$cartrule['minimum_amount_tax'] && $cartrule['minimum_amount']>0) {
$totalPrice = $totalWithoutShippingAndTax - $this->minimum_amount;
$minimum = $cartrule['minimum_amount'];
}
$remaining = $totalPrice - $this->reduction_amount;
if ($remaining < $cartrule['minimum_amount']) {
return (!$display_error) ? false : Tools::displayError('Le montant minimum requis (bons inclus) pour la livraison gratuite doit être de '.Tools::displayPrice($cartrule['minimum_amount']).'. Avec le bon cumulé, le montant serait de '.Tools::displayPrice($remaining));
}
}
}
}*/
return (!$display_error) ? true : false;
}
/**
* Check if this cart rule can be applied
*
* @param Context $context
* @param bool $alreadyInCart Check if the voucher is already on the cart
* @param bool $display_error Display error
* @return bool|mixed|string
*/
public function checkValidityWithoutAmount(Context $context, $alreadyInCart = false, $display_error = true)
{
if (!CartRule::isFeatureActive())
return false;
if (!$this->active)
return (!$display_error) ? false : Tools::displayError('This voucher is disabled');
if (!$this->quantity)
return (!$display_error) ? false : Tools::displayError('This voucher has already been used');
if (strtotime($this->date_from) > time())
return (!$display_error) ? false : Tools::displayError('This voucher is not valid yet');
if (strtotime($this->date_to) < time())
return (!$display_error) ? false : 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_cart_rule 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').' != o.current_state
');
if ($quantityUsed + 1 > $this->quantity_per_user)
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher anymore (usage limit reached)');
}
// 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 (!$display_error) ? false : Tools::displayError('You cannot use this voucher');
}
// Check if the customer delivery address is usable with the cart rule
if ($this->country_restriction)
{
if (!$context->cart->id_address_delivery)
return (!$display_error) ? false : Tools::displayError('You must choose a delivery address before applying this voucher to your order');
$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 (!$display_error) ? false : Tools::displayError('You cannot use this voucher in your country of delivery');
}
// Check if the cart rules appliy to the shop browsed by the customer
if ($this->shop_restriction && $context->shop->id && Shop::isFeatureActive())
{
$id_cart_rule = (int)Db::getInstance()->getValue('
SELECT crs.id_cart_rule
FROM '._DB_PREFIX_.'cart_rule_shop crs
WHERE crs.id_cart_rule = '.(int)$this->id.'
AND crs.id_shop = '.(int)$context->shop->id);
if (!$id_cart_rule)
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher');
}
// Check if the products chosen by the customer are usable with the cart rule
if ($this->product_restriction)
{
$r = $this->checkProductRestrictions($context, false, $display_error, $alreadyInCart);
if ($r !== false && $display_error)
return $r;
elseif (!$r && !$display_error)
return false;
}
// 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 (!$display_error) ? false : (Tools::displayError('You cannot use this voucher').' - '.Tools::displayError('Please log in'));
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher');
}
/* This loop checks:
- if the voucher is already in the cart
- if a non compatible voucher is in the cart
- if there are products in the cart (gifts excluded)
Important note: this MUST be the last check, because if the tested cart rule has priority over a non combinable one in the cart, we will switch them
*/
$nb_products = Cart::getNbProducts($context->cart->id);
$otherCartRules = $context->cart->getCartRules();
if (count($otherCartRules)) {
// check cart_rules restriction
if (!$this->cart_rule_restriction){
return (!$display_error) ? false : Tools::displayError('Ce bon de réduction n\'est pas compatible avec d\'autres bons de réduction.');
}
foreach ($otherCartRules as $otherCartRule)
{
if ($otherCartRule['id_cart_rule'] == $this->id && !$alreadyInCart)
return (!$display_error) ? false : Tools::displayError('This voucher is already in your cart');
if ($otherCartRule['gift_product'])
--$nb_products;
if ($this->cart_rule_restriction && $otherCartRule['cart_rule_restriction'] && $otherCartRule['id_cart_rule'] != $this->id)
{
$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)
{
$cart_rule = new CartRule((int)$otherCartRule['id_cart_rule'], $context->cart->id_lang);
// The cart rules are not combinable and the cart rule currently in the cart has priority over the one tested
if ($cart_rule->priority <= $this->priority)
return (!$display_error) ? false : Tools::displayError('This voucher is not combinable with an other voucher already in your cart:').' '.$cart_rule->name;
// But if the cart rule that is tested has priority over the one in the cart, we remove the one in the cart and keep this new one
else
$context->cart->removeCartRule($cart_rule->id);
}
}
}
}
if (!$nb_products)
return (!$display_error) ? false : Tools::displayError('Cart is empty');
if ($this->getCategoryRestriction()){
$r = $this->checkCategoryRestriction($context, $display_error, $alreadyInCart);
if ($r !== false && $display_error)
return $r;
elseif (!$r && !$display_error)
return false;
}
return (!$display_error) ? true : false;
}
protected function checkCategoryRestriction(Context $context, $display_error = true, $alreadyInCart = false){
if (!isset($context->cart) || !is_object($context->cart) || !is_array($products = $context->cart->getProducts()))
return (!$display_error) ? false : Tools::displayError('Cart is empty');
foreach ($products as $product)
if (in_array($product['id_category_default'], $this->getCategoryRestriction()))
return (!$display_error) ? false : Tools::displayError('Ce bon de réduction n\'est pas valable pour l\'un des produits du panier');
return (!$display_error) ? true : false;
}
public function getAssociatedRestrictions($type, $active_only, $i18n)
{
if (!in_array($type, array('category')))
return parent::getAssociatedRestrictions($type, $active_only, $i18n);
$array = array('selected' => array(), 'unselected' => array());
$shop_list = '';
if (!Validate::isLoadedObject($this))
{
$array['unselected'] = Db::getInstance()->executeS('
SELECT t.*'.($i18n ? ', tl.*' : '').', 1 as selected
FROM `'._DB_PREFIX_.$type.'` t
'.($i18n ? '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_only ? 'AND t.active = 1' : '').
$shop_list.
' ORDER BY name ASC');
}
else
{
$resource = Db::getInstance()->query('
SELECT t.*'.($i18n ? ', tl.*' : '').', IF(crt.id_'.$type.' IS NULL, 0, 1) as selected
FROM `'._DB_PREFIX_.$type.'` t
'.($i18n ? '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 == 'carrier' ? 'reference' : $type).' = crt.id_'.$type.'
WHERE 1 '.($active_only ? ' AND t.active = 1' : '').
$shop_list.
' ORDER BY name ASC',
false);
while ($row = Db::getInstance()->nextRow($resource))
$array[($row['selected']) ? 'selected' : 'unselected'][] = $row;
}
return $array;
}
public function getCategoryRestriction(){
$category_restriction = array();
$restrictions = Db::getInstance()->executeS('SELECT `id_category` FROM `'._DB_PREFIX_.'cart_rule_category` WHERE `id_cart_rule` = '.(int)$this->id);
if (!$restrictions)
return false;
foreach ($restrictions as $row)
$category_restriction[] = (int)$row['id_category'];
return $category_restriction;
}
protected function checkProductRestrictions(Context $context, $return_products = false, $display_error = true, $alreadyInCart = false)
{
$selectedProducts = array();
// Check if the products chosen by the customer are usable with the cart rule
if ($this->product_restriction)
{
$productRuleGroups = $this->getProductRuleGroups();
foreach ($productRuleGroups as $id_product_rule_group => $productRuleGroup)
{
$eligibleProductsList = array();
if (isset($context->cart) && is_object($context->cart) && is_array($products = $context->cart->getProducts()))
foreach ($products as $product)
$eligibleProductsList[] = (int)$product['id_product'].'-'.(int)$product['id_product_attribute'];
if (!count($eligibleProductsList))
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher in an empty cart');
$productRules = $this->getProductRules($id_product_rule_group);
foreach ($productRules as $productRule)
{
switch ($productRule['type'])
{
case 'attributes':
$cartAttributes = Db::getInstance()->executeS('
SELECT cp.quantity, cp.`id_product`, pac.`id_attribute`, cp.`id_product_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` IN ('.implode(',', array_map('intval', $eligibleProductsList)).')
AND cp.id_product_attribute > 0');
$countMatchingProducts = 0;
$matchingProductsList = array();
foreach ($cartAttributes as $cartAttribute)
if (in_array($cartAttribute['id_attribute'], $productRule['values']))
{
$countMatchingProducts += $cartAttribute['quantity'];
if ($alreadyInCart && $this->gift_product == $cartAttribute['id_product'] && $this->gift_product_attribute == $cartAttribute['id_product_attribute'])
--$countMatchingProducts;
$matchingProductsList[] = $cartAttribute['id_product'].'-'.$cartAttribute['id_product_attribute'];
}
if ($countMatchingProducts < $productRuleGroup['quantity'])
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with these products');
$eligibleProductsList = CartRule::array_uintersect($eligibleProductsList, $matchingProductsList);
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.'
AND cp.`id_product` IN ('.implode(',', array_map('intval', $eligibleProductsList)).')');
$countMatchingProducts = 0;
$matchingProductsList = array();
foreach ($cartProducts as $cartProduct)
if (in_array($cartProduct['id_product'], $productRule['values']))
{
$countMatchingProducts += $cartProduct['quantity'];
if ($alreadyInCart && $this->gift_product == $cartProduct['id_product'])
--$countMatchingProducts;
$matchingProductsList[] = $cartProduct['id_product'].'-0';
}
if ($countMatchingProducts < $productRuleGroup['quantity'])
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with these products');
$eligibleProductsList = CartRule::array_uintersect($eligibleProductsList, $matchingProductsList);
break;
case 'categories':
/** @Override */
$rootCategory = Category::getRootCategory();
if (in_array((int)$rootCategory->id, $productRule['values'])) {
$result = Db::getInstance()->ExecuteS('SELECT id_category FROM '._DB_PREFIX_.'category');
$productRule['values'] = array();
foreach ($result as $row)
$productRule['values'][] = $row['id_category'];
}
/** end @Override */
$cartCategories = Db::getInstance()->executeS('
SELECT cp.quantity, cp.`id_product`, cp.`id_product_attribute`, 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.'
AND cp.`id_product` IN ('.implode(',', array_map('intval', $eligibleProductsList)).')
AND cp.`id_product` <> '.(int)$this->gift_product);
$countMatchingProducts = 0;
$matchingProductsList = array();
foreach ($cartCategories as $cartCategory)
if (in_array($cartCategory['id_category'], $productRule['values'])
// We also check that the product is not already in the matching product list, because there are doubles in the query results (when the product is in multiple categories)
&& !in_array($cartCategory['id_product'].'-'.$cartCategory['id_product_attribute'], $matchingProductsList))
{
$countMatchingProducts += $cartCategory['quantity'];
$matchingProductsList[] = $cartCategory['id_product'].'-'.$cartCategory['id_product_attribute'];
}
if ($countMatchingProducts < $productRuleGroup['quantity'])
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with these products');
// Attribute id is not important for this filter in the global list, so the ids are replaced by 0
foreach ($matchingProductsList as &$matchingProduct)
$matchingProduct = preg_replace('/^([0-9]+)-[0-9]+$/', '$1-0', $matchingProduct);
$eligibleProductsList = CartRule::array_uintersect($eligibleProductsList, $matchingProductsList);
break;
case 'manufacturers':
$cartManufacturers = Db::getInstance()->executeS('
SELECT cp.quantity, cp.`id_product`, p.`id_manufacturer`
FROM `'._DB_PREFIX_.'cart_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p ON cp.id_product = p.id_product
WHERE cp.`id_cart` = '.(int)$context->cart->id.'
AND cp.`id_product` IN ('.implode(',', array_map('intval', $eligibleProductsList)).')');
$countMatchingProducts = 0;
$matchingProductsList = array();
foreach ($cartManufacturers as $cartManufacturer)
if (in_array($cartManufacturer['id_manufacturer'], $productRule['values']))
{
$countMatchingProducts += $cartManufacturer['quantity'];
$matchingProductsList[] = $cartManufacturer['id_product'].'-0';
}
if ($countMatchingProducts < $productRuleGroup['quantity'])
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with these products');
$eligibleProductsList = CartRule::array_uintersect($eligibleProductsList, $matchingProductsList);
break;
case 'suppliers':
$cartSuppliers = Db::getInstance()->executeS('
SELECT cp.quantity, cp.`id_product`, p.`id_supplier`
FROM `'._DB_PREFIX_.'cart_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p ON cp.id_product = p.id_product
WHERE cp.`id_cart` = '.(int)$context->cart->id.'
AND cp.`id_product` IN ('.implode(',', array_map('intval', $eligibleProductsList)).')');
$countMatchingProducts = 0;
$matchingProductsList = array();
foreach ($cartSuppliers as $cartSupplier)
if (in_array($cartSupplier['id_supplier'], $productRule['values']))
{
$countMatchingProducts += $cartSupplier['quantity'];
$matchingProductsList[] = $cartSupplier['id_product'].'-0';
}
if ($countMatchingProducts < $productRuleGroup['quantity'])
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with these products');
$eligibleProductsList = CartRule::array_uintersect($eligibleProductsList, $matchingProductsList);
break;
}
if (!count($eligibleProductsList))
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with these products');
}
$selectedProducts = array_merge($selectedProducts, $eligibleProductsList);
}
}
if ($return_products)
return $selectedProducts;
return (!$display_error) ? true : false;
}
public function isEligibleProduct(Context $context, $display_error = true, $id_product){
if (!is_numeric($id_product)) {
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher in an unknown product');
}
$product = new Product((int)$id_product);
$sql = 'SELECT cp.`id_category` FROM `'._DB_PREFIX_.'category_product` cp WHERE cp.`id_product` = '.(int)$id_product;
$productCategories = Db::getInstance()->executeS($sql);
if ($this->product_restriction ){
$productRuleGroups = $this->getProductRuleGroups();
foreach ($productRuleGroups as $id_product_rule_group => $productRuleGroup)
{
$productRules = $this->getProductRules($id_product_rule_group);
foreach ($productRules as $productRule)
{
switch ($productRule['type'])
{
case 'products':
if (!in_array($product->id, $productRule['values'])) {
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with these products');
}
break;
case 'categories':
$rootCategory = Category::getRootCategory();
if (in_array((int)$rootCategory->id, $productRule['values'])) {
$result = Db::getInstance()->ExecuteS('SELECT id_category FROM '._DB_PREFIX_.'category');
$productRule['values'] = array();
foreach ($result as $row) {
$productRule['values'][] = $row['id_category'];
}
}
foreach ($productCategories as $productCategorie) {
if (!in_array($productCategorie['id_category'], $productRule['values'])) {
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with this product');
}
}
break;
/*case 'attributes':
$productAttributes = array():
foreach ($productAttributes as $productAttribute) {
if (in_array($productAttribute['id_attribute'], $productRule['values'])) {
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with this product attribute');
}
}
break;*/
}
}
}
}
foreach ($productCategories as $productCategorie) {
if (in_array($productCategorie['id_category'], $this->getCategoryRestriction())) {
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with this product');
}
}
return true;
}
}