Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-mw committed Feb 19, 2022
1 parent 263e4e0 commit c3c25ae
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 30 deletions.
94 changes: 75 additions & 19 deletions src/MicroweberPackages/Cart/CartManager.php
Expand Up @@ -30,8 +30,7 @@ public function __construct($app = null)
$this->app = mw();
}

$coupon_code = $this->app->user_manager->session_get('coupon_code');
$this->coupon_data = coupon_get_by_code($coupon_code);

}

/**
Expand All @@ -41,7 +40,7 @@ public function __construct($app = null)
*/
public function sum($return_amount = true)
{
if($return_amount ){
if ($return_amount) {
return $this->app->cart_repository->getCartAmount();
} else {
return $this->app->cart_repository->getCartItemsCount();
Expand Down Expand Up @@ -182,26 +181,45 @@ public function get_discount()

public function get_discount_type()
{
if (empty($this->coupon_data)) {
$data = $this->couponCodeGetDataFromSession();
if (empty($data)) {
return false;
}
if (isset($data['discount_type'])) {
return $data['discount_type'];
}
return false;
}

return $this->coupon_data['discount_type'];
public function set_coupon_data($data)
{
$this->coupon_data = $data;
}

public function get_discount_value()
{
if (empty($this->coupon_data)) {
$data = $this->couponCodeGetDataFromSession();


if (empty($data)) {
return false;
}

if (!isset($data['discount_value'])) {
return false;
}

if (!isset($data['total_amount'])) {
return false;
}

$apply_code = false;
if ($this->sum() >= $this->coupon_data['total_amount']) {
if ($this->sum() >= $data['total_amount']) {
$apply_code = true;
}

if ($apply_code) {
return floatval($this->coupon_data['discount_value']);
return floatval($data['discount_value']);
}

return false;
Expand Down Expand Up @@ -353,9 +371,9 @@ public function remove_item($data)
$cart = array();
$cart['id'] = intval($data['id']);

// if ($this->app->user_manager->is_admin() == false) {
// if ($this->app->user_manager->is_admin() == false) {
$cart['session_id'] = mw()->user_manager->session_id();
// }
// }

$cart['order_completed'] = 0;
$cart['one'] = 1;
Expand Down Expand Up @@ -574,9 +592,9 @@ public function update_cart($data)
$skip_keys = array();

$content_custom_fields = $this->app->fields_manager->get([
'rel_type'=>$for,
'rel_id'=>$for_id,
'return_full'=>true,
'rel_type' => $for,
'rel_id' => $for_id,
'return_full' => true,
]);

$product_prices = array();
Expand Down Expand Up @@ -738,7 +756,7 @@ public function update_cart($data)
$cart['qty'] = $update_qty_new;
} else {
$cart['qty'] = $check_cart['qty'] + 1;
}
}
}
} else {
if ($update_qty > 0) {
Expand Down Expand Up @@ -911,16 +929,54 @@ public function is_product_in_stock($content_id)
$item = content_data($content_id);
$isInStock = true;
if ($item) {
if (isset($item['qty']) and $item['qty'] != 'nolimit' ) {
$quantity =intval( $item['qty']);
if ($quantity < 1) {
$isInStock = false;
}
if (isset($item['qty']) and $item['qty'] != 'nolimit') {
$quantity = intval($item['qty']);
if ($quantity < 1) {
$isInStock = false;
}
}

}

return $isInStock;
}

public function couponCodeGetDataFromSession()
{
$coupon_code = $this->app->user_manager->session_get('coupon_code');
if ($coupon_code and !$this->couponCodeCheckIfValid($coupon_code)) {
//check if coupon is valid
if (function_exists('coupons_delete_session')) {
coupons_delete_session();
}

$this->coupon_data = false;
} else {
if (function_exists('coupon_get_by_code')) {
$this->coupon_data = coupon_get_by_code($coupon_code);
} else {
$this->coupon_data = false;
}
}
return $this->coupon_data;
}

public function couponCodeCheckIfValid($coupon_code)
{
if (function_exists('coupon_apply')) {
//check if coupon is valid
$coupon_valid = coupon_apply([
'coupon_code' => $coupon_code,
'coupon_check_if_valid' => true
]);
if (!$coupon_valid) {
return false;
}
return true;
}
return false;

}


}
33 changes: 28 additions & 5 deletions src/MicroweberPackages/Checkout/CheckoutManager.php
Expand Up @@ -434,6 +434,32 @@ public function checkout($data)
}
}


// Discount details save
if ($coupon_code) {
$place_order['promo_code'] = $coupon_code;
$place_order['coupon_id'] = $coupon_id;
$place_order['discount_type'] = $discount_type;
$place_order['discount_value'] = $discount_value;


if (!$this->app->cart_manager->couponCodeCheckIfValid($coupon_code)) {
//check if coupon is valid
if(function_exists('coupons_delete_session')){
coupons_delete_session();
}

$place_order['promo_code'] = '';
$place_order['coupon_id'] ='';
$place_order['discount_type'] = '';
$place_order['discount_value'] ='';
}
}





$amount = $this->app->shop_manager->cart_total();
$tax = $this->app->cart_manager->get_tax();

Expand Down Expand Up @@ -498,11 +524,6 @@ public function checkout($data)
}


// Discount details save
$place_order['promo_code'] = $coupon_code;
$place_order['coupon_id'] = $coupon_id;
$place_order['discount_type'] = $discount_type;
$place_order['discount_value'] = $discount_value;


// convert currency to payment provider currency
Expand Down Expand Up @@ -1426,4 +1447,6 @@ private function _build_url(array $elements)
(isset($e['query']) ? '?' . (is_array($e['query']) ? http_build_query($e['query'], '', '&') : $e['query']) : '') .
(isset($e['fragment']) ? "#$e[fragment]" : '');
}


}
24 changes: 18 additions & 6 deletions userfiles/modules/shop/coupons/functions.php
Expand Up @@ -27,14 +27,20 @@ function coupon_apply($params = array())

$customer_ip = user_ip();

$checkout = new MicroweberPackages\Checkout\CheckoutManager();
$getCart = $checkout->app->shop_manager->get_cart(array(
'session_id' => $checkout->app->user_manager->session_id()

));
// $checkout = new MicroweberPackages\Checkout\CheckoutManager();
// $getCart = $checkout->app->shop_manager->get_cart(array(
// 'session_id' => $checkout->app->user_manager->session_id()
//
// ));

$getCart = false;
$coupon['total_amount'] = floatval($coupon['total_amount']);
$cartTotal = floatval(cart_total());
$cartTotal = floatval( \DB::table('cart')->where('session_id', app()->user_manager->session_id())->sum('price'));
$getCartItems = \DB::table('cart')->where('session_id', app()->user_manager->session_id())->get();

if($getCartItems){
$getCart = $getCartItems->toArray();
}

// Check rules
if ($coupon and isset($coupon['uses_per_customer']) and $coupon['uses_per_customer'] > 0) {
Expand Down Expand Up @@ -65,6 +71,11 @@ function coupon_apply($params = array())
$ok = true;
}

if(isset( $params['coupon_check_if_valid'])){
return $ok;
}


if ($ok) {

mw()->user_manager->session_set('coupon_code', $coupon['coupon_code']);
Expand Down Expand Up @@ -294,6 +305,7 @@ function coupons_delete_session()
mw()->user_manager->session_del('coupon_id');
mw()->user_manager->session_del('discount_value');
mw()->user_manager->session_del('discount_type');
mw()->user_manager->session_del('applied_coupon_data');
}


Expand Down

0 comments on commit c3c25ae

Please sign in to comment.