From 7c901163d1661f1e88c4c879da3f17efe831bb3a Mon Sep 17 00:00:00 2001 From: rockwellll Date: Mon, 24 Feb 2025 08:58:33 +0300 Subject: [PATCH 1/3] update parameter structure to be compatible with new API --- src/Events/CouponRedeemed.php | 2 +- src/Events/OrderPlaced.php | 2 +- src/Events/OrderStatus.php | 6 +++--- src/Events/ProductViewed.php | 2 +- src/Events/RefundReceived.php | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Events/CouponRedeemed.php b/src/Events/CouponRedeemed.php index d244eaf..47afe87 100644 --- a/src/Events/CouponRedeemed.php +++ b/src/Events/CouponRedeemed.php @@ -15,7 +15,7 @@ function hellotext_coupon_redeemed ($code) { if ($valid) { ( new Event() )->track('coupon.redeemed', [ - 'coupon_parameters' => [ + 'object_parameters' => [ 'type' => 'coupon', 'reference' => $coupon->get_id(), 'code' => $code, diff --git a/src/Events/OrderPlaced.php b/src/Events/OrderPlaced.php index 123d5f1..3a7e1c9 100644 --- a/src/Events/OrderPlaced.php +++ b/src/Events/OrderPlaced.php @@ -22,6 +22,6 @@ function hellotext_order_placed ( $order ) { add_post_meta($order->get_id(), 'hellotext_session', $encrypted_session); $event->track('order.placed', array( - 'order_parameters' => $parsedOrder, + 'object_parameters' => $parsedOrder, )); } diff --git a/src/Events/OrderStatus.php b/src/Events/OrderStatus.php index 2d085c3..13f975a 100644 --- a/src/Events/OrderStatus.php +++ b/src/Events/OrderStatus.php @@ -18,19 +18,19 @@ function track_order_status ($order_id, $old_status, $new_status, $order) { switch ($new_status) { case 'processing': $event->track('order.confirmed', array( - 'order_parameters' => $orderAdapter->get(), + 'object_parameters' => $orderAdapter->get(), )); break; case 'cancelled': $event->track('order.cancelled', array( - 'order_parameters' => $orderAdapter->get(), + 'object_parameters' => $orderAdapter->get(), )); break; case 'completed': $event->track('order.delivered', array( - 'order_parameters' => $orderAdapter->get(), + 'object_parameters' => $orderAdapter->get(), )); break; } diff --git a/src/Events/ProductViewed.php b/src/Events/ProductViewed.php index 04da1b7..ac4cf5c 100644 --- a/src/Events/ProductViewed.php +++ b/src/Events/ProductViewed.php @@ -11,6 +11,6 @@ function hellotext_product_viewed() { do_action('hellotext_create_profile'); ( new Event() )->track('product.viewed', array( - 'product_parameters' => ( new ProductAdapter($product) )->get() + 'object_parameters' => ( new ProductAdapter($product) )->get() )); } diff --git a/src/Events/RefundReceived.php b/src/Events/RefundReceived.php index 24ebf02..116b0c5 100644 --- a/src/Events/RefundReceived.php +++ b/src/Events/RefundReceived.php @@ -16,6 +16,6 @@ function hellotext_refund_created ($order_id, $refund_id) { $session = Session::decrypt($encrypted_session); ( new Event($session) )->track('refund.received', array( - 'refund_parameters' => ( new RefundAdapter($refund, $order) )->get(), + 'object_parameters' => ( new RefundAdapter($refund, $order) )->get(), )); } From 7e4a88b9bca03385be7c0fac4a96aa7bd8917c74 Mon Sep 17 00:00:00 2001 From: rockwellll Date: Fri, 28 Feb 2025 14:56:20 +0300 Subject: [PATCH 2/3] update cart updates --- src/Events/CartUpdates.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Events/CartUpdates.php b/src/Events/CartUpdates.php index bc91c15..7f33a7d 100644 --- a/src/Events/CartUpdates.php +++ b/src/Events/CartUpdates.php @@ -77,7 +77,7 @@ function hellotext_cart_updated () { } ( new Event() )->track("cart.{$event}", array( - 'products' => $items + 'items' => $items )); } } From bc31698b82e5737420da4953d714d52da679eb75 Mon Sep 17 00:00:00 2001 From: rockwellll Date: Mon, 3 Mar 2025 09:42:01 +0300 Subject: [PATCH 3/3] update cart API structure --- src/Events/CartUpdates.php | 141 +++++++++++++++++++++---------------- 1 file changed, 79 insertions(+), 62 deletions(-) diff --git a/src/Events/CartUpdates.php b/src/Events/CartUpdates.php index 7f33a7d..d56e59b 100644 --- a/src/Events/CartUpdates.php +++ b/src/Events/CartUpdates.php @@ -18,66 +18,83 @@ function hellotext_trigger_cart_updated () { add_action('hellotext_woocommerce_cart_updated', 'hellotext_cart_updated'); -function hellotext_cart_updated () { - wc_load_cart(); - - $changes = array( - 'added' => array(), - 'removed' => array() - ); - - // Set previous cart items and current cart items - $previous_cart_items = isset($_SESSION['hellotext_cart_items']) - ? json_decode(sanitize_text_field($_SESSION['hellotext_cart_items']), true) - : array(); - - $current_cart_items = WC()->cart->get_cart(); - $cart_items = array(); - - // Parse current cart items with the ProductAdapter - foreach ( $current_cart_items as $key => $cart_item ) { - $cart_items[] = ( new ProductAdapter( $cart_item['product_id'], $cart_item) )->get(); - } - - // Save current cart items to session - $_SESSION['hellotext_cart_items'] = json_encode($cart_items); - - // Add items that were added to the cart - foreach ($cart_items as $cart_item) { - $match = array_filter( - $previous_cart_items, - fn($item) => $item['reference'] == $cart_item['reference'] - ); - - $previous_item = count($match) > 0 ? array_shift($match) : null; - - if (!$previous_item || $previous_item['quantity'] < $cart_item['quantity']) { - $changes['added'][] = $cart_item; - } - } - - // Add items that were removed from the cart - foreach ($previous_cart_items as $previous_item) { - $match = array_filter( - $cart_items, - fn($item) => $item['reference'] == $previous_item['reference'] - ); - - $cart_item = count($match) > 0 ? array_shift($match) : null; - - if (!$cart_item || $previous_item['quantity'] > $cart_item['quantity']) { - $changes['removed'][] = $cart_item; - } - } - - // Trigger events, one for added and one for removed items - foreach ($changes as $event => $items) { - if (0 == count($changes[$event])) { - continue; - } - - ( new Event() )->track("cart.{$event}", array( - 'items' => $items - )); - } +function hellotext_cart_updated() { + wc_load_cart(); + + $changes = array( + 'added' => array(), + 'removed' => array() + ); + + // Set previous cart items and current cart items + $previous_cart_items = isset($_SESSION['hellotext_cart_items']) + ? json_decode(sanitize_text_field($_SESSION['hellotext_cart_items']), true) + : array(); + + $current_cart_items = WC()->cart->get_cart(); + $cart_items = array(); + + foreach ($current_cart_items as $key => $cart_item) { + $product = $cart_item['data']; // WC_Product object + + $cart_items[] = [ + 'product' => (new ProductAdapter($product))->get(), + 'quantity' => $cart_item['quantity'], + ]; + } + + // Save current cart items to session + $_SESSION['hellotext_cart_items'] = json_encode($cart_items); + + // Calculate total cart value + $cart_total = WC()->cart->get_cart_contents_total(); + $currency = get_woocommerce_currency(); + + // Get current page URL + $current_url = home_url(add_query_arg(array(), $GLOBALS['wp']->request)); + + foreach ($cart_items as $cart_item) { + $match = array_filter( + $previous_cart_items, + fn($item) => $item['product']['reference'] == $cart_item['product']['reference'] + ); + + $previous_item = count($match) > 0 ? array_shift($match) : null; + + if (!$previous_item || $previous_item['quantity'] < $cart_item['quantity']) { + $changes['added'][] = $cart_item; + } + } + + // Add items that were removed from the cart + foreach ($previous_cart_items as $previous_item) { + $match = array_filter( + $cart_items, + fn($item) => $item['product']['reference'] == $previous_item['product']['reference'] + ); + + $cart_item = count($match) > 0 ? array_shift($match) : null; + + if (!$cart_item || $previous_item['quantity'] > $cart_item['quantity']) { + $changes['removed'][] = $previous_item; // Use previous_item here as it's the removed item + } + } + + // Trigger events, one for added and one for removed items + foreach ($changes as $event => $items) { + if (0 == count($items)) { + continue; + } + + $event_data = array( + 'amount' => $cart_total, + 'currency' => $currency, + 'url' => $current_url, + 'object_parameters' => array( + 'items' => $items + ) + ); + + (new Event())->track("cart.{$event}", $event_data); + } }