/** * Handle GET requests. */ public function action_get() { try { if (($coupon_name = $this->request->param('id')) != NULL) { $coupon = new Model_Coupon(); $coupon->where('name', '=', $coupon_name)->where('number_coupons', '>', 0)->where('valid_date', '>', Date::unix2mysql())->where('status', '=', 1); //filter by product if (is_numeric(core::request('id_product'))) { $coupon->where('id_product', '=', core::request('id_product')); } $coupon = $coupon->limit(1)->find(); $this->rest_output(array('coupon' => $coupon->loaded() ? $coupon->as_array() : FALSE)); } else { $this->_error('You need to specify a coupon'); } } catch (Kohana_HTTP_Exception $khe) { $this->_error($khe); } }
/** * get the coupon from the query or from the sesion or the post in paypal * @return Model_Coupon or null if not found */ public static function get_coupon() { $coupon = new Model_Coupon(); /** * Deletes a coupon in use */ if (core::request('coupon_delete') != NULL) { Session::instance()->set('coupon', ''); Alert::set(Alert::INFO, __('Coupon deleted.')); } elseif (core::post('custom') != NULL or core::request('coupon') != NULL or Session::instance()->get('coupon') != '') { $slug_coupon = new Model_Coupon(); $coupon = $slug_coupon->where('name', '=', core::post('custom', core::request('coupon', Session::instance()->get('coupon'))))->where('number_coupons', '>', 0)->where('valid_date', '>', Date::unix2mysql())->where('status', '=', 1)->limit(1)->find(); if ($coupon->loaded()) { //only add it to session if its different than before if (Session::instance()->get('coupon') != $coupon->name) { Alert::set(Alert::SUCCESS, __('Coupon added!')); Session::instance()->set('coupon', $coupon->name); } } else { Alert::set(Alert::INFO, __('Coupon not valid, expired or already used.')); Session::instance()->set('coupon', ''); } } return $coupon; }