Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 79 additions & 62 deletions src/Events/CartUpdates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'products' => $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);
}
}
2 changes: 1 addition & 1 deletion src/Events/CouponRedeemed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Events/OrderPlaced.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
}
6 changes: 3 additions & 3 deletions src/Events/OrderStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Events/ProductViewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
));
}
2 changes: 1 addition & 1 deletion src/Events/RefundReceived.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
}