diff --git a/src/MicroweberPackages/Cart/CartManager.php b/src/MicroweberPackages/Cart/CartManager.php index 0a8a03063d5..04940cc2d43 100644 --- a/src/MicroweberPackages/Cart/CartManager.php +++ b/src/MicroweberPackages/Cart/CartManager.php @@ -20,6 +20,7 @@ class CartManager extends Crud public $app; public $table = 'cart'; + public $coupon_data = false; public function __construct($app = null) { @@ -28,6 +29,14 @@ public function __construct($app = null) } else { $this->app = mw(); } + + $coupon_code = $this->app->user_manager->session_get('coupon_code'); + /* $this->coupon_data = db_get("cart_coupons", array( + 'is_active' => 1, + 'coupon_code' => $coupon_code, + 'single' => true, + 'no_cache' => true + ));*/ } /** @@ -178,18 +187,29 @@ public function get_discount() public function get_discount_type() { - return $this->app->user_manager->session_get('discount_type'); + if (empty( $this->coupon_data)) { + return false; + } + + return $this->coupon_data['discount_type']; } public function get_discount_value() { - $discount_value = $this->app->user_manager->session_get('discount_value'); - - if (empty($discount_value)) { + if (empty( $this->coupon_data)) { return false; } - return floatval($discount_value); + $apply_code = false; + if ($this->total() >= $this->coupon_data['total_amount']) { + $apply_code = true; + } + + if ($apply_code) { + return floatval( $this->coupon_data['discount_value']); + } + + return false; } public function get_discount_text() diff --git a/userfiles/modules/shop/coupons/functions.php b/userfiles/modules/shop/coupons/functions.php index 52342d9fce9..3a456059ff5 100644 --- a/userfiles/modules/shop/coupons/functions.php +++ b/userfiles/modules/shop/coupons/functions.php @@ -238,12 +238,14 @@ function coupon_get_by_code($coupon_code) { $table = "cart_coupons"; - return db_get($table, array( + $get = db_get($table, array( 'is_active' => 1, 'coupon_code' => $coupon_code, 'single' => true, 'no_cache' => true )); + + return $get; } api_expose('coupon_delete');