@@ -357,43 +357,54 @@ public function get_items($items)
357357
358358 public function getCartData ()
359359 {
360- $ cart = \WC ()->cart ;
361- $ totals = $ cart ->get_totals ();
362- $ return = [
363- 'total ' => $ totals ['total ' ],
364- 'subtotal ' => $ totals ['subtotal ' ],
365- 'tax ' => $ totals ['total_tax ' ],
366- 'shipping ' => $ totals ['shipping_total ' ],
367- 'currency ' => "USD " ,
368- 'items ' => [],
369- ];
360+ if (!is_null ($ cart )) {
361+ $ cart = \WC ()->cart ;
362+ $ totals = $ cart ->get_totals ();
363+ $ return = [
364+ 'total ' => $ totals ['total ' ],
365+ 'subtotal ' => $ totals ['subtotal ' ],
366+ 'tax ' => $ totals ['total_tax ' ],
367+ 'shipping ' => $ totals ['shipping_total ' ],
368+ 'currency ' => "USD " ,
369+ 'items ' => [],
370+ ];
370371
371- $ cartItems = $ cart ->get_cart_contents ();
372- if (count ($ cartItems ) > 0 ) {
373- foreach ($ cart ->get_cart () as $ key => $ item ) {
374- $ cartItem = $ cart ->get_cart_item ($ key );
375- $ attrs = '' ;
376- if ($ cartItem ['variation_id ' ] > 0 ) {
377- foreach ($ cartItem ['variation ' ] as $ key => $ value ) {
378- if (strpos (strtolower ($ key ), "color " ) !== FALSE ) {
379- $ attrs .= "color: '` {$ value }`, " ;
380- }
381- if (strpos (strtolower ($ key ), "size " ) !== FALSE ) {
382- $ attrs .= "size: ` {$ value }`, " ;
372+ $ cartItems = $ cart ->get_cart_contents ();
373+ if (count ($ cartItems ) > 0 ) {
374+ foreach ($ cart ->get_cart () as $ key => $ item ) {
375+ $ cartItem = $ cart ->get_cart_item ($ key );
376+ $ attrs = '' ;
377+ if ($ cartItem ['variation_id ' ] > 0 ) {
378+ foreach ($ cartItem ['variation ' ] as $ key => $ value ) {
379+ if (strpos (strtolower ($ key ), "color " ) !== FALSE ) {
380+ $ attrs .= "color: '` {$ value }`, " ;
381+ }
382+ if (strpos (strtolower ($ key ), "size " ) !== FALSE ) {
383+ $ attrs .= "size: ` {$ value }`, " ;
384+ }
383385 }
384386 }
387+ $ variationId = $ cartItem ['variation_id ' ] > 0 ? $ cartItem ['variation_id ' ] : $ cartItem ['product_id ' ];
388+ $ product = $ cartItem ['data ' ];
389+ $ return ['items ' ][] = [
390+ "productid " => $ cartItem ['product_id ' ],
391+ "variationid " => $ variationId ,
392+ "sku " => $ product ->get_sku (),
393+ "quantity " => $ cartItem ['quantity ' ],
394+ "price " => $ product ->get_price (),
395+ "{$ attrs }name " => $ product ->get_name ()
396+ ];
385397 }
386- $ variationId = $ cartItem ['variation_id ' ] > 0 ? $ cartItem ['variation_id ' ] : $ cartItem ['product_id ' ];
387- $ product = $ cartItem ['data ' ];
388- $ return ['items ' ][] = [
389- "productid " => $ cartItem ['product_id ' ],
390- "variationid " => $ variationId ,
391- "sku " => $ product ->get_sku (),
392- "quantity " => $ cartItem ['quantity ' ],
393- "price " => $ product ->get_price (),
394- "{$ attrs }name " => $ product ->get_name ()
395- ];
396398 }
399+ } else {
400+ $ return = [
401+ 'total ' => 0 ,
402+ 'subtotal ' => 0 ,
403+ 'tax ' => 0 ,
404+ 'shipping ' => 0 ,
405+ 'currency ' => "USD " ,
406+ 'items ' => [],
407+ ];
397408 }
398409 return $ return ;
399410 }
@@ -421,38 +432,40 @@ public function getConversionTrackingCodes()
421432 }
422433
423434 $ cart = \WC ()->cart ;
424- $ totals = $ cart ->get_totals ();
425- $ code .= 'juapp("cart", {
435+ if (!is_null ($ cart )) {
436+ $ totals = $ cart ->get_totals ();
437+ $ code .= 'juapp("cart", {
426438 total: ' . $ totals ['total ' ] . ',
427439 subtotal: ' . $ totals ['subtotal ' ] . ',
428440 tax: ' . $ totals ['total_tax ' ] . ',
429441 shipping: ' . $ totals ['shipping_total ' ] . ',
430442 currency:"USD",
431443 }
432444); ' ;
433- $ cartItems = $ cart ->get_cart_contents ();
434- if (count ($ cartItems ) > 0 ) {
435- $ code .= "juapp('cartItems', [ " ;
436- foreach ($ cart ->get_cart () as $ key => $ item ) {
437- $ cartItem = $ cart ->get_cart_item ($ key );
438- $ attrs = '' ;
439- if ($ cartItem ['variation_id ' ] > 0 ) {
440- foreach ($ cartItem ['variation ' ] as $ key => $ value ) {
441- if (strpos (strtolower ($ key ), "color " ) !== FALSE ) {
442- $ attrs .= "color: ` {$ value }`, " ;
443- }
444- if (strpos (strtolower ($ key ), "size " ) !== FALSE ) {
445- $ attrs .= "size: ` {$ value }`, " ;
445+ $ cartItems = $ cart ->get_cart_contents ();
446+ if (count ($ cartItems ) > 0 ) {
447+ $ code .= "juapp('cartItems', [ " ;
448+ foreach ($ cart ->get_cart () as $ key => $ item ) {
449+ $ cartItem = $ cart ->get_cart_item ($ key );
450+ $ attrs = '' ;
451+ if ($ cartItem ['variation_id ' ] > 0 ) {
452+ foreach ($ cartItem ['variation ' ] as $ key => $ value ) {
453+ if (strpos (strtolower ($ key ), "color " ) !== FALSE ) {
454+ $ attrs .= "color: ` {$ value }`, " ;
455+ }
456+ if (strpos (strtolower ($ key ), "size " ) !== FALSE ) {
457+ $ attrs .= "size: ` {$ value }`, " ;
458+ }
446459 }
447460 }
448- }
449- $ variationId = $ cartItem ['variation_id ' ] > 0 ? $ cartItem ['variation_id ' ] : $ cartItem ['product_id ' ];
450- $ product = $ cartItem ['data ' ];
451- $ code .= "
461+ $ variationId = $ cartItem ['variation_id ' ] > 0 ? $ cartItem ['variation_id ' ] : $ cartItem ['product_id ' ];
462+ $ product = $ cartItem ['data ' ];
463+ $ code .= "
452464{ productid: ' {$ cartItem ['product_id ' ]}', variationid: ' {$ variationId }', sku:` {$ product ->get_sku ()}`, quantity: {$ cartItem ['quantity ' ]}, price: {$ product ->get_price ()}, {$ attrs }name: ` {$ product ->get_name ()}`}, " ;
465+ }
466+ $ code = substr ($ code , 0 , -1 );
467+ $ code .= "]) " ;
453468 }
454- $ code = substr ($ code , 0 , -1 );
455- $ code .= "]) " ;
456469 }
457470 return $ code ;
458471 }
0 commit comments