PrestaShop 1.6如何为订单添加折扣?

时间:2015-07-14 17:32:21

标签: plugins module prestashop prestashop-1.6

我正在为PRESTASHOP 1.6编写一个模块。此模块的任务之一是为CART中的整个订单提供客户折扣并显示它。问题是我应该使用哪种方法来获取购物车中所有产品的总价值,然后为整个订单添加一个折扣?

1 个答案:

答案 0 :(得分:2)

检查此代码以创建新的购物车规则:

$cart_rule = new CartRule();
$cart_rule->id_customer = $this->context->cart->id_customer;
$cart_rule->name = array(
    Configuration::get('PS_LANG_DEFAULT') => $this->l('CartRule title')
);
$cart_rule->date_from = date('Y-m-d H:i:s', time());
$cart_rule->date_to = date('Y-m-d H:i:s', time() + 24 * 3600);
$cart_rule->quantity = 1;
$cart_rule->quantity_per_user = 1;
$cart_rule->minimum_amount_currency = $this->context->cart->id_currency;
$cart_rule->reduction_currency = $this->context->cart->id_currency;
$cart_rule->free_shipping = true;
$cart_rule->reduction_amount = 50; #discount value
$cart_rule->active = 1;
$cart_rule->add();

// Add cart rule to cart and in order
$values = array(
    'tax_incl' => $cart_rule->getContextualValue(true),
    'tax_excl' => $cart_rule->getContextualValue(false)
);
$this->context->cart->addCartRule($cart_rule->id, $cart_rule->name[Configuration::get('PS_LANG_DEFAULT')], $values);