Skip to content

Commit f890252

Browse files
Taras KubivIgor Liubarskyi
authored andcommitted
Merged in APITOCART-16379-demo-module-abandoned-cart (pull request #42)
APITOCART-16379-demo-module-abandoned-cart [MASTER] Approved-by: Oleg Syvak
2 parents 1d7f6c3 + 86f2f41 commit f890252

File tree

6 files changed

+193
-153
lines changed

6 files changed

+193
-153
lines changed

src/app/Http/Controllers/BusinessCases/AbandonedCartRecoveryController.php

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,57 +21,64 @@ public function __construct(Api2Cart $api2Cart)
2121

2222
public function index()
2323
{
24-
// dd( $this->api2cart->getAbandonedCart('1316ad9a66ac871ce46a3d59005acc9c') );
2524
return view('business_cases.abandoned_cart_recovery.index');
2625
}
2726

2827
public function send(Request $request)
2928
{
3029

31-
$abandoned = [];
30+
$abandoneds = [];
31+
$countMail = 0;
3232

33-
foreach ($request->get('items') as $item){
33+
foreach ($request->get('items') as $item) {
3434
$pid = explode(':', $item);
35-
$cart = collect( $this->api2cart->getAbandonedCart( $pid[0] ) );
36-
$storeInfo = $this->api2cart->getCart( $pid[0] );
35+
$cart = collect($this->api2cart->getAbandonedCart($pid[0]));
36+
$storeInfo = $this->api2cart->getCart($pid[0]);
3737

3838
// adding selected abandoned carts
39-
foreach ($cart as $ci){
40-
if ( isset($ci['customer']['id']) && $ci['customer']['id'] == $pid[1] ) {
39+
foreach ($cart as $ci) {
40+
if (isset($ci['customer']['id']) && $ci['id'] == $pid[1] && !empty($ci['customer']['email'])) {
4141
$ci['store_info'] = $storeInfo;
4242
$ci['store_key'] = $pid[0];
43-
$abandoned[] = $ci;
43+
$abandoneds[($pid[2] ?? $ci['customer']['email'] ?? $pid[0] . $pid[1])][] = $ci;
4444
}
4545
}
4646
}
4747

48-
foreach ($abandoned as $k=>$cart){
48+
foreach ($abandoneds as $abandoned) {
49+
foreach ($abandoned as $k => $cart) {
50+
foreach ($cart['order_products'] as $op) {
51+
if ($productInfo = $this->api2cart->getProductInfo($cart['store_key'], $op['product_id'])) {
52+
$abandoned[$k]['products'][] = $productInfo;
53+
}
54+
}
4955

50-
foreach ($cart['order_products'] as $op){
51-
$abandoned[$k]['products'][] = $this->api2cart->getProductInfo( $cart['store_key'], $op['product_id']);
56+
if (isset($cart['customer']['email'])) {
57+
try {
58+
Mail::to($cart['customer']['email'])->send(new AbandonedCartRecovery([$abandoned[$k]]));
59+
$countMail++;
60+
} catch (Throwable $e) {
61+
return response()->json([ 'log' => $this->api2cart->getLog(), 'success' => false, 'errormessage' => $e->getMessage()]);
62+
}
63+
}
5264
}
53-
54-
Mail::to( $request->get('email') )->send( new AbandonedCartRecovery( $abandoned[$k] ) );
55-
5665
}
5766

58-
67+
return response()->json([ 'log' => $this->api2cart->getLog(), 'success' => true, 'countMail' => $countMail]);
5968
}
6069

6170
public function compose(Request $request)
6271
{
63-
// Log::debug( $request->all() );
64-
6572
$abandoned = [];
6673

67-
foreach ($request->get('items') as $item){
74+
foreach ($request->get('items') as $item) {
6875
$pid = explode(':', $item);
69-
$cart = collect( $this->api2cart->getAbandonedCart( $pid[0] ) );
70-
$storeInfo = $this->api2cart->getCart( $pid[0] );
76+
$cart = collect($this->api2cart->getAbandonedCart($pid[0]));
77+
$storeInfo = $this->api2cart->getCart($pid[0]);
7178

7279
// adding selected abandoned carts
73-
foreach ($cart as $ci){
74-
if ( isset($ci['customer']['id']) && $ci['customer']['id'] == $pid[1] ) {
80+
foreach ($cart as $ci) {
81+
if (isset($ci['customer']['id']) && $ci['id'] == $pid[1]) {
7582
$ci['store_info'] = $storeInfo;
7683
$ci['store_key'] = $pid[0];
7784
$abandoned[] = $ci;
@@ -80,17 +87,15 @@ public function compose(Request $request)
8087
}
8188

8289

83-
foreach ($abandoned[0]['order_products'] as $op){
84-
$abandoned[0]['products'][] = $this->api2cart->getProductInfo( $abandoned[0]['store_key'], $op['product_id']);
90+
foreach ($abandoned as $key => $op) {
91+
foreach ($op['order_products'] as $item) {
92+
if ($productInfo = $this->api2cart->getProductInfo($op['store_key'], $item['product_id'])) {
93+
$abandoned[$key]['products'][] = $productInfo;
94+
}
8595
}
96+
}
8697

87-
// Log::debug( print_r($abandoned[0],1) );
88-
89-
$data = $abandoned[0];
90-
91-
return view('emails.abandoned', compact('data') );
92-
93-
98+
return view('emails.abandoned', compact('abandoned'));
9499
}
95100

96101
}

src/app/Http/Controllers/OrdersController.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,51 @@ public function orderProducts($store_id=null,$order_id=null,Request $request)
164164

165165
public function abandoned($store_id=null, Request $request)
166166
{
167-
$items = collect( $this->api2cart->getAbandonedCart( $store_id ) );
168167
$data = [
169-
"recordsTotal" => (is_array($items)) ? count($items) : 0,
170-
"recordsFiltered" => (is_array($items)) ? count($items) : 0,
171-
"start" => 0,
172-
"length" => 10,
173-
"data" => collect($items),
174-
'log' => $this->api2cart->getLog(),
168+
"recordsTotal" => 0,
169+
"recordsFiltered" => 0,
170+
"start" => 0,
171+
"length" => 10,
172+
"data" => collect([]),
173+
'log' => $this->api2cart->getLog(),
175174
];
176175

176+
if ($store_id) {
177+
$items = collect($this->api2cart->getAbandonedCart($store_id));
178+
$data = [
179+
"recordsTotal" => (is_array($items)) ? count($items) : 0,
180+
"recordsFiltered" => (is_array($items)) ? count($items) : 0,
181+
"start" => 0,
182+
"length" => 10,
183+
"data" => collect($items),
184+
'log' => $this->api2cart->getLog(),
185+
];
186+
} elseif ($storeIds = $request->get('storeKeys', [])) {
187+
$orders = collect([]);
188+
189+
foreach ($storeIds as $store_id) {
190+
$items = collect($this->api2cart->getAbandonedCart($store_id));
191+
192+
if ($items->count()) {
193+
foreach ($items as $item) {
194+
$newItem = $item;
195+
$newItem['cart_id'] = $store_id;
196+
$newItem['created_at']['value'] = Carbon::parse($item['created_at']['value'])->setTimezone('UTC')->format("Y-m-d\TH:i:sO");
197+
$orders->push($newItem);
198+
}
199+
}
200+
}
201+
202+
$data = [
203+
"recordsTotal" => $orders->count(),
204+
"recordsFiltered" => $orders->count(),
205+
"start" => 0,
206+
"length" => 10,
207+
"data" => $orders->sortBy('created_at.value', null, true)->toArray(),
208+
'log' => $this->api2cart->getLog(),
209+
];
210+
}
211+
177212
return response()->json($data);
178213
}
179214

src/app/Mail/AbandonedCartRecovery.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class AbandonedCartRecovery extends Mailable
1111
{
1212
use Queueable, SerializesModels;
1313

14-
public $data;
14+
public $abandoned;
1515

1616
/**
1717
* Create a new message instance.
@@ -20,7 +20,7 @@ class AbandonedCartRecovery extends Mailable
2020
*/
2121
public function __construct($data)
2222
{
23-
$this->data = $data;
23+
$this->abandoned = $data;
2424
}
2525

2626
/**
8.6 KB
Loading

0 commit comments

Comments
 (0)