Q:

PrestaShop 1.6 How to add a discount to order?

I'm writing a module for PRESTASHOP 1.6. One of the task of this module is to give a customer DISCOUNT for whole order in the CART and display it. The question is which method should I use to get total value of all products in the cart and then add a single DISCOUNT for whole order?

A:

Check this code to create a new cart rule:
$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);

