Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-mw committed Jun 7, 2023
1 parent 098f23e commit eee0c67
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/MicroweberPackages/Cart/CartManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function get($params = false)
$params = $params2;
}

$table = 'cart';
$table = 'cart';
$params['table'] = $table;
$skip_sid = false;
if (!defined('MW_API_CALL')) {
Expand Down Expand Up @@ -292,10 +292,36 @@ public function get($params = false)
if (is_array($get)) {
foreach ($get as $k => $item) {
if (is_array($item)) {


if (isset($item['rel_id']) and isset($item['rel_type']) and $item['rel_type'] == 'content') {
$item['content_data'] = $this->app->content_manager->data($item['rel_id']);
$item['url'] = $this->app->content_manager->link($item['rel_id']);
$item['picture'] = $this->app->media_manager->get_picture($item['rel_id']);

if (isset($params['for_checkout']) and $params['for_checkout']) {
// check if content is unpublished or deleted
$checkContent = get_content_by_id($item['rel_id']);
$removeFromCart = false;
if ($checkContent == false) {
$removeFromCart = true;
}

if ($checkContent and isset($checkContent['is_deleted']) and $checkContent['is_deleted'] == 1) {
$removeFromCart = true;
}
if ($checkContent and isset($checkContent['is_active']) and $checkContent['is_active'] == 0) {
$removeFromCart = true;
}

if ($removeFromCart) {
$this->remove_item($item['id']);
unset($get[$k]);
continue;
}
}


}
if (isset($item['custom_fields_data']) and $item['custom_fields_data'] != '') {
$item = $this->app->format->render_item_custom_fields_data($item);
Expand Down Expand Up @@ -330,7 +356,7 @@ public function get_by_order_id($order_id = false)
return;
}
$params = array();
$table = 'cart';
$table = 'cart';
$params['table'] = $table;
$params['order_id'] = $order_id;
$get = $this->app->database_manager->get($params);
Expand Down Expand Up @@ -455,7 +481,7 @@ public function update_item_qty($data)
$cart_return = $check_cart;


$table = 'cart';
$table = 'cart';
$cart_data_to_save = array();
$cart_data_to_save['qty'] = $cart['qty'];
$cart_data_to_save['id'] = $cart['id'];
Expand All @@ -477,7 +503,7 @@ public function update_item_qty($data)
public function empty_cart()
{
$sid = mw()->user_manager->session_id();
$cart_table = 'cart';
$cart_table = 'cart';

Cart::where('order_completed', 0)->where('session_id', $sid)->delete();
$this->no_cache = true;
Expand Down Expand Up @@ -659,10 +685,10 @@ public function update_cart($data)
$found = false;
foreach ($content_custom_fields as $cf) {
if (isset($cf['type']) and isset($cf['name']) and $cf['type'] != 'price') {
if(isset($data[$cf['name_key']])){
if (isset($data[$cf['name_key']])) {
$cf['name'] = $data[$cf['name_key']];
}
} elseif (isset($cf['type']) and $cf['type'] == 'price' and isset($cf['name']) and isset($cf['value'])) {
} elseif (isset($cf['type']) and $cf['type'] == 'price' and isset($cf['name']) and isset($cf['value'])) {
if ($cf['value'] != '') {
if (isset($product_prices[$cf['name']])) {
$prices[$cf['name']] = $product_prices[$cf['name']];
Expand Down Expand Up @@ -737,7 +763,7 @@ public function update_cart($data)
ksort($add);
asort($add);
$add = mw()->format->clean_xss($add);
$table = 'cart';
$table = 'cart';


$cart = array();
Expand Down Expand Up @@ -873,7 +899,7 @@ public function recover_cart($sid = false, $ord_id = false)

if ($cur_sid != false) {
$c_id = $sid;
$table = 'cart';
$table = 'cart';
$params = array();
// $params['order_completed'] = 0;
$params['session_id'] = $c_id;
Expand Down Expand Up @@ -942,7 +968,7 @@ public function recover_cart($sid = false, $ord_id = false)

public function table_name()
{
return 'cart';
return 'cart';
}


Expand Down
1 change: 1 addition & 0 deletions src/MicroweberPackages/Checkout/CheckoutManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function checkout($data)
$table_orders = 'cart_orders';
$cart['session_id'] = $sid;
$cart['order_completed'] = 0;
$cart['for_checkout'] = true;
$cart['limit'] = 1;
$mw_process_payment = true;
$mw_process_payment_success = false;
Expand Down
110 changes: 110 additions & 0 deletions src/MicroweberPackages/Shop/tests/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,114 @@ public function testCheckoutQtyUpdate()
}





public function testCheckoutDeletedProduct()
{
mw()->database_manager->extended_save_set_permission(true);

$productPrice = rand(1, 9999);
$title = 'test testCheckoutDeletedProduct prod ' . $productPrice;
$params = array(
'title' => $title,
'content_type' => 'product',
'subtype' => 'product',
'custom_fields_advanced' => array(
array('type' => 'dropdown', 'name' => 'Color', 'value' => array('Purple', 'Blue')),
array('type' => 'price', 'name' => 'Price', 'value' => '9.99'),

),
'data_fields_qty' => 10,
'is_deleted' => 1,
'is_active' => 1,);


$saved_id = save_content($params);
$get = get_content_by_id($saved_id);


$add_to_cart = array(
'content_id' => $saved_id,
'price' => $productPrice,
);
$cart_add = update_cart($add_to_cart);


$checkoutDetails = array();
$checkoutDetails['email'] = 'client@microweber.com';
$checkoutDetails['is_paid'] = 1;
$checkoutDetails['order_completed'] = 1;


$checkoutStatus = app()->order_manager->place_order($checkoutDetails);
$content_data_after_order = content_data($saved_id);
$this->assertEquals(10, $content_data_after_order['qty']);

$productQuery = \MicroweberPackages\Product\Models\Product::query();
$productQuery = $productQuery->whereHas('orders');
$products = $productQuery->get();
$this->assertTrue($products->isEmpty());

$order = get_order_by_id($checkoutStatus);
$this->assertNotNull($order);
$this->assertNull($order['amount']);

}




public function testCheckoutUnpublishedProduct()
{
mw()->database_manager->extended_save_set_permission(true);

$productPrice = rand(1, 9999);
$title = 'test testCheckoutUnpublishedProduct prod ' . $productPrice;
$params = array(
'title' => $title,
'content_type' => 'product',
'subtype' => 'product',
'custom_fields_advanced' => array(
array('type' => 'dropdown', 'name' => 'Color', 'value' => array('Purple', 'Blue')),
array('type' => 'price', 'name' => 'Price', 'value' => '9.99'),

),
'data_fields_qty' => 11,
'is_deleted' => 0,
'is_active' => 0
);


$saved_id = save_content($params);
$get = get_content_by_id($saved_id);


$add_to_cart = array(
'content_id' => $saved_id,
'price' => $productPrice,
);
$cart_add = update_cart($add_to_cart);


$checkoutDetails = array();
$checkoutDetails['email'] = 'client@microweber.com';
$checkoutDetails['is_paid'] = 1;
$checkoutDetails['order_completed'] = 1;


$checkoutStatus = app()->order_manager->place_order($checkoutDetails);
$content_data_after_order = content_data($saved_id);
$this->assertEquals(11, $content_data_after_order['qty']);

$productQuery = \MicroweberPackages\Product\Models\Product::query();
$productQuery = $productQuery->whereHas('orders');
$products = $productQuery->get();
$this->assertTrue($products->isEmpty());

$order = get_order_by_id($checkoutStatus);
$this->assertNotNull($order);
$this->assertNull($order['amount']);

}
}

0 comments on commit eee0c67

Please sign in to comment.