@@ -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}
0 commit comments