// Temp fix for cart rule and multi-shipping - not fully functionnal yet but still better than before
git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@13771 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
1 parent
a87dbfc405
commit
d5c2094706
3 files changed
+93
-20
No files matched your search
+14
-3
@@ -27,6 +27,11 @@
|
|||||||
|
|
||||||
class CartRuleCore extends ObjectModel
|
class CartRuleCore extends ObjectModel
|
||||||
{
|
{
|
||||||
|
const FILTER_ACTION_ALL = 1;
|
||||||
|
const FILTER_ACTION_SHIPPING = 2;
|
||||||
|
const FILTER_ACTION_REDUCTION = 3;
|
||||||
|
const FILTER_ACTION_GIFT = 4;
|
||||||
|
|
||||||
public $id;
|
public $id;
|
||||||
public $name;
|
public $name;
|
||||||
public $id_customer;
|
public $id_customer;
|
||||||
@@ -630,17 +635,19 @@ class CartRuleCore extends ObjectModel
|
|||||||
* @param Context $context
|
* @param Context $context
|
||||||
* @return float|int|string
|
* @return float|int|string
|
||||||
*/
|
*/
|
||||||
public function getContextualValue($useTax, Context $context = null)
|
public function getContextualValue($useTax, Context $context = null, $filter = null)
|
||||||
{
|
{
|
||||||
if (!CartRule::isFeatureActive())
|
if (!CartRule::isFeatureActive())
|
||||||
return 0;
|
return 0;
|
||||||
if (!$context)
|
if (!$context)
|
||||||
$context = Context::getContext();
|
$context = Context::getContext();
|
||||||
|
if (!$filter)
|
||||||
|
$filter = CartRule::FILTER_ACTION_ALL;
|
||||||
|
|
||||||
$reduction_value = 0;
|
$reduction_value = 0;
|
||||||
|
|
||||||
// Free shipping on selected carriers
|
// Free shipping on selected carriers
|
||||||
if ($this->free_shipping)
|
if ($this->free_shipping && ($filter == CartRule::FILTER_ACTION_ALL || $filter == CartRule::FILTER_ACTION_SHIPPING))
|
||||||
{
|
{
|
||||||
if (!$this->carrier_restriction)
|
if (!$this->carrier_restriction)
|
||||||
$reduction_value += $context->cart->getTotalShippingCost(null, $useTax = true, $context->country);
|
$reduction_value += $context->cart->getTotalShippingCost(null, $useTax = true, $context->country);
|
||||||
@@ -658,6 +665,9 @@ class CartRuleCore extends ObjectModel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($filter == CartRule::FILTER_ACTION_ALL || $filter == CartRule::FILTER_ACTION_REDUCTION)
|
||||||
|
{
|
||||||
|
|
||||||
// Discount (%) on the whole order
|
// Discount (%) on the whole order
|
||||||
if ($this->reduction_percent && $this->reduction_product == 0)
|
if ($this->reduction_percent && $this->reduction_product == 0)
|
||||||
$reduction_value += $context->cart->getOrderTotal($useTax, Cart::ONLY_PRODUCTS) * $this->reduction_percent / 100;
|
$reduction_value += $context->cart->getOrderTotal($useTax, Cart::ONLY_PRODUCTS) * $this->reduction_percent / 100;
|
||||||
@@ -769,9 +779,10 @@ class CartRuleCore extends ObjectModel
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Free gift
|
// Free gift
|
||||||
if ((int)$this->gift_product)
|
if ((int)$this->gift_product && ($filter == CartRule::FILTER_ACTION_ALL || $filter == CartRule::FILTER_ACTION_GIFT))
|
||||||
{
|
{
|
||||||
foreach ($context->cart->getProducts() as $product)
|
foreach ($context->cart->getProducts() as $product)
|
||||||
if ($product['id_product'] == $this->gift_product && $product['id_product_attribute'] == $this->gift_product_attribute)
|
if ($product['id_product'] == $this->gift_product && $product['id_product_attribute'] == $this->gift_product_attribute)
|
||||||
|
|||||||
+78
-16
@@ -224,6 +224,8 @@ abstract class PaymentModuleCore extends Module
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Next !
|
// Next !
|
||||||
|
$only_one_gift = false;
|
||||||
|
$cart_rule_used = array();
|
||||||
foreach ($order_detail_list as $key => $order_detail)
|
foreach ($order_detail_list as $key => $order_detail)
|
||||||
{
|
{
|
||||||
$order = $order_list[$key];
|
$order = $order_list[$key];
|
||||||
@@ -298,23 +300,77 @@ abstract class PaymentModuleCore extends Module
|
|||||||
} // end foreach ($products)
|
} // end foreach ($products)
|
||||||
|
|
||||||
$cart_rules_list = '';
|
$cart_rules_list = '';
|
||||||
$result = $cart->getCartRules();
|
$cart_rules = $cart->getCartRules();
|
||||||
foreach ($result as $cart_rule)
|
foreach ($cart_rules as $cart_rule)
|
||||||
{
|
{
|
||||||
$cart_rule_obj = $cart_rule['obj'];
|
$values = array('tax_incl' => 0, 'tax_excl' => 0);
|
||||||
$values = array(
|
|
||||||
'tax_incl' => $cart_rule_obj->getContextualValue(true),
|
|
||||||
'tax_excl' => $cart_rule_obj->getContextualValue(false)
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($values['tax_incl'] > $order->total_products_wt && $cart_rule_obj->partial_use == 1 && $cart_rule_obj->reduction_amount > 0)
|
// If the cart is split in multiple orders, the cart rule must be split too
|
||||||
|
if (count($order_list) > 1)
|
||||||
|
{
|
||||||
|
// If the cart rule is a fee gift, then add the free gift value only if the gift is in this order
|
||||||
|
if ((int)$cart_rule['obj']->gift_product && !$only_one_gift)
|
||||||
|
{
|
||||||
|
$in_order = (bool)Db::getInstance()->getValue('
|
||||||
|
SELECT product_id
|
||||||
|
FROM '._DB_PREFIX_.'order_detail
|
||||||
|
WHERE product_id = '.(int)$cart_rule['obj']->gift_product.'
|
||||||
|
AND product_attribute_id = '.(int)$cart_rule['obj']->gift_product_attribute.'
|
||||||
|
AND id_order = '.(int)$order->id);
|
||||||
|
|
||||||
|
if ($in_order)
|
||||||
|
{
|
||||||
|
$values['tax_incl'] += $cart_rule['obj']->getContextualValue(true, null, CartRule::FILTER_ACTION_GIFT);
|
||||||
|
$values['tax_excl'] += $cart_rule['obj']->getContextualValue(false, null, CartRule::FILTER_ACTION_GIFT);
|
||||||
|
$only_one_gift = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the cart rule offers free shipping, add the shipping cost
|
||||||
|
if ($cart_rule['obj']->free_shipping)
|
||||||
|
{
|
||||||
|
$values['tax_incl'] += $order->total_shipping_tax_incl;
|
||||||
|
$values['tax_excl'] += $order->total_shipping_tax_excl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the cart rule offers a reduction, the amount is prorated
|
||||||
|
if ($cart_rule['obj']->reduction_amount || $cart_rule['obj']->reduction_percent)
|
||||||
|
{
|
||||||
|
$prorata = $order->total_paid_tax_incl / $cart_total_paid;
|
||||||
|
$values['tax_incl'] += Tools::ps_round($prorata * $cart_rule['obj']->getContextualValue(true, null, CartRule::FILTER_ACTION_REDUCTION), 2);
|
||||||
|
$values['tax_excl'] += Tools::ps_round($prorata * $cart_rule['obj']->getContextualValue(false, null, CartRule::FILTER_ACTION_REDUCTION), 2);
|
||||||
|
|
||||||
|
file_put_contents(dirname(__FILE__).'/../ploplop.txt', print_r(array($order->total_paid_tax_incl, $cart_total_paid, $prorata, $values, $order->id), true), FILE_APPEND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$values = array(
|
||||||
|
'tax_incl' => $cart_rule['obj']->getContextualValue(true),
|
||||||
|
'tax_excl' => $cart_rule['obj']->getContextualValue(false)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the reduction is not applicable to this order (in a multi-shipping case), then try the next
|
||||||
|
if (!$values['tax_excl'])
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/* IF
|
||||||
|
** - This is not multi-shipping
|
||||||
|
** - The value of the voucher is greater than the total of the order
|
||||||
|
** - Partial use is allowed
|
||||||
|
** - This is an "amount" reduction, not a reduction in % or a gift
|
||||||
|
** THEN
|
||||||
|
** The voucher is cloned with a new value corresponding to the remainder
|
||||||
|
*/
|
||||||
|
if (count($order_list) == 1 && $values['tax_incl'] > $order->total_products_wt && $cart_rule['obj']->partial_use == 1 && $cart_rule['obj']->reduction_amount > 0)
|
||||||
{
|
{
|
||||||
// Create a new voucher from the original
|
// Create a new voucher from the original
|
||||||
$voucher = clone $cart_rule_obj;
|
$voucher = clone $cart_rule['obj'];
|
||||||
unset($voucher->id);
|
unset($voucher->id);
|
||||||
|
|
||||||
// Set a new voucher code
|
// Set a new voucher code
|
||||||
$voucher->code = empty($voucher->code) ? substr(md5($order->id.'-'.$order->id_customer.'-'.$cart_rule_obj->id), 0, 16) : $voucher->code.'-2';
|
$voucher->code = empty($voucher->code) ? substr(md5($order->id.'-'.$order->id_customer.'-'.$cart_rule['obj']->id), 0, 16) : $voucher->code.'-2';
|
||||||
if (preg_match('/\-([0-9]{1,2})\-([0-9]{1,2})$/', $voucher->code, $matches) && $matches[1] == $matches[2])
|
if (preg_match('/\-([0-9]{1,2})\-([0-9]{1,2})$/', $voucher->code, $matches) && $matches[1] == $matches[2])
|
||||||
$voucher->code = preg_replace('/'.$matches[0].'$/', '-'.(intval($matches[1]) + 1), $voucher->code);
|
$voucher->code = preg_replace('/'.$matches[0].'$/', '-'.(intval($matches[1]) + 1), $voucher->code);
|
||||||
|
|
||||||
@@ -329,7 +385,7 @@ abstract class PaymentModuleCore extends Module
|
|||||||
if ($voucher->add())
|
if ($voucher->add())
|
||||||
{
|
{
|
||||||
// If the voucher has conditions, they are now copied to the new voucher
|
// If the voucher has conditions, they are now copied to the new voucher
|
||||||
CartRule::copyConditions($cart_rule_obj->id, $voucher->id);
|
CartRule::copyConditions($cart_rule['obj']->id, $voucher->id);
|
||||||
|
|
||||||
$params = array(
|
$params = array(
|
||||||
'{voucher_amount}' => Tools::displayPrice($voucher->reduction_amount, $currency, false),
|
'{voucher_amount}' => Tools::displayPrice($voucher->reduction_amount, $currency, false),
|
||||||
@@ -342,19 +398,25 @@ abstract class PaymentModuleCore extends Module
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$order->addCartRule($cart_rule_obj->id, $cart_rule_obj->name, $values);
|
$order->addCartRule($cart_rule['obj']->id, $cart_rule['obj']->name, $values);
|
||||||
|
|
||||||
if ($id_order_state != Configuration::get('PS_OS_ERROR') && $id_order_state != Configuration::get('PS_OS_CANCELED'))
|
$order->total_discounts = $order->total_discounts_tax_incl = $values['tax_incl'];
|
||||||
|
$order->total_discounts_tax_excl = $values['tax_excl'];
|
||||||
|
$order->update();
|
||||||
|
|
||||||
|
if ($id_order_state != Configuration::get('PS_OS_ERROR') && $id_order_state != Configuration::get('PS_OS_CANCELED') && !in_array($cart_rule['obj']->id, $cart_rule_used))
|
||||||
{
|
{
|
||||||
|
$cart_rule_used[] = $cart_rule['obj']->id;
|
||||||
|
|
||||||
// Create a new instance of Cart Rule without id_lang, in order to update its quantity
|
// Create a new instance of Cart Rule without id_lang, in order to update its quantity
|
||||||
$cart_rule_to_update = new CartRule($cart_rule_obj->id);
|
$cart_rule_to_update = new CartRule($cart_rule['obj']->id);
|
||||||
$cart_rule_to_update->quantity = $cart_rule_to_update->quantity - 1;
|
$cart_rule_to_update->quantity = max(0, $cart_rule_to_update->quantity - 1);
|
||||||
$cart_rule_to_update->update();
|
$cart_rule_to_update->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
$cart_rules_list .= '
|
$cart_rules_list .= '
|
||||||
<tr style="background-color:#EBECEE;">
|
<tr style="background-color:#EBECEE;">
|
||||||
<td colspan="4" style="padding:0.6em 0.4em;text-align:right">'.$this->l('Voucher name:').' '.$cart_rule_obj->name.'</td>
|
<td colspan="4" style="padding:0.6em 0.4em;text-align:right">'.$this->l('Voucher name:').' '.$cart_rule['obj']->name.'</td>
|
||||||
<td style="padding:0.6em 0.4em;text-align:right">'.($values['tax_incl'] != 0.00 ? '-' : '').Tools::displayPrice($values['tax_incl'], $currency, false).'</td>
|
<td style="padding:0.6em 0.4em;text-align:right">'.($values['tax_incl'] != 0.00 ? '-' : '').Tools::displayPrice($values['tax_incl'], $currency, false).'</td>
|
||||||
</tr>';
|
</tr>';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -366,7 +366,7 @@ var ajaxCart = {
|
|||||||
|
|
||||||
//refresh display of vouchers (needed for vouchers in % of the total)
|
//refresh display of vouchers (needed for vouchers in % of the total)
|
||||||
refreshVouchers : function (jsonData) {
|
refreshVouchers : function (jsonData) {
|
||||||
if (jsonData.discounts.length == 0)
|
if (jsonData.discounts = undefined || jsonData.discounts.length == 0)
|
||||||
$('#vouchers').remove();
|
$('#vouchers').remove();
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user