Question

I'm begginer at Prestashop and i'm programming a module that allows the costumer to apply a discount to the current cart.

Trying to realice how to do it using Cart and Discount clases. I found that Cart class has the addDiscount method, and Discount class has de createOrderDiscount method.

How can I create a discount to a cart before been ordered?

Am I in the right aproach, or there is other better way?

Thanks a lot.

Was it helpful?

Solution

Finally, I made muy own add function, called, for this case, from within a hook, and providing id_customer and value in $params parameter:

public function addDiscount($params = array()) {
        try {
            $params['description'] = "Discount description";

            $d = array(
                'id_discount_type' => 2,
                'behavior_not_exhausted' => 1,
                'id_customer' => $params['id_customer'],
                'id_group' => 0,
                'id_currency' => 1,
                'name' => "discount_name",
                'value' => $params['value'],
                'quantity' => 1,
                'quantity_per_user' => 1,
                'cumulable' => 1,
                'cumulable_reduction' => 1,
                'date_from' => date("Y-m-d H:i:s", time()),
                'date_to' => date("Y-m-d H:i:s", time() + 86400),
                'minimal' => (float) 0.00,
                'include_tax' => 1,
                'active' => 1,
                'cart_display' => 1,
                'date_add' => date("Y-m-d H:i:s"),
                'date_upd' => date("Y-m-d H:i:s")
            );

            $this->db->autoExecute('ps_discount', $d, 'INSERT');

            $discount_id = $this->db->Insert_ID();

//            /* insertar dicount_category */
            $this->db->autoExecute('ps_discount_category', array(
                'id_category' => 1,
                'id_discount' => $discount_id
                    ), 'INSERT');

//            /* insertar dicount_lang */
            if ($rs_langs = $this->db->executeS("select id_lang from ps_lang")) {
                foreach ($rs_langs as $lang) {
                    $this->db->autoExecute('ps_discount_lang', array(
                        'id_discount' => $discount_id,
                        'id_lang' => $lang['id_lang'],
                        'description' => $params['description']
                            ), 'INSERT');
                }
            }
        } catch (Exception $e) {
            $this->Log($e->getTraceAsString());
        }
    }

The function basically does one insertion in ps_discount table, one in ps_discount_category table, and one for each language in ps_discount_lang table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top