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

PrestaShop 1.6 How to add a discount to order?

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

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

$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);