Skip to content
Advertisement

Check if Coupon exists and if it is the first order

In my e-commerce I have implemented coupons, and it is working, but now I introduce “Coupons first order”, but don’t know if I’m doing the best way to check it, because it is not working, my query is ignoring this consult.

How I can check if coupon_tp i’ts first, and If is true, how check if user is first order?

public function verificaCupom($coupon){
    $this->db->select("customer_id, id, value, percent, code, discount_tp, coupon_tp");

//check if is first order (not working)
    $this->db->select("IF(ga845_cupons.coupon_tp = 'first', (SELECT customer_id FROM ga845_pedidos_view WHERE NOT EXISTS 'ga845_pedidos_view.customer_id = ga845_cupons.customer_id'))  as primeira");

//check if coupon exist
    $this->db->where('code', $coupon);
//check valid
    $this->db->where('CURDATE() <= valid');
//check if coupon it's enable
    $this->db->where('status', 0);
    $this->db->limit("1");

    $query = $this->db->get('ga845_cupons');

    return $query->result();
}

Advertisement

Answer

I have adjusted my code to this bellow, and work as I want.

$this->db->select("IF(ga845_cupons.coupon_tp = 'first', (SELECT COUNT(`customers_id`) FROM `ga845_pedidos_view` WHERE `ga845_pedidos_view`.`customers_id` = `ga845_cupons`.`customers_id` ), coupon_tp) as first");
$this->db->having('first <= ' , 0);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement