define( 'MK_KLAVIYO_PRIVATE_KEY', 'pk_TNB9ZT_0c44c062a7a93e85c786c87e8ebccfcbeb' ); define( 'MK_KLAVIYO_REVISION', '2024-10-15' ); function mk_kl_ready() { return MK_KLAVIYO_PRIVATE_KEY && function_exists( 'WC' ) && WC()->cart; } function mk_kl_get_email() { if ( is_user_logged_in() ) { $u = wp_get_current_user(); if ( $u && is_email( $u->user_email ) ) { return $u->user_email; } } if ( function_exists( 'WC' ) && WC()->customer ) { $e = WC()->customer->get_billing_email(); if ( is_email( $e ) ) { return $e; } } return ''; } function mk_kl_profile_attrs() { $attrs = array(); $email = mk_kl_get_email(); if ( $email ) { $attrs['email'] = $email; } if ( ! empty( $_COOKIE['__kla_id'] ) ) { $json = base64_decode( rawurldecode( $_COOKIE['__kla_id'] ), true ); $obj = $json ? json_decode( $json, true ) : null; if ( ! empty( $obj['cid'] ) ) { $uuid = base64_decode( $obj['cid'], true ); if ( $uuid ) { $attrs['anonymous_id'] = $uuid; } } } return $attrs; } function mk_kl_cart_items() { $items = array(); $names = array(); $cats = array(); foreach ( WC()->cart->get_cart() as $ci ) { $p = $ci['data']; if ( ! $p ) { continue; } $parent_id = $p->is_type( 'variation' ) ? $p->get_parent_id() : $p->get_id(); $catnames = wp_get_post_terms( $parent_id, 'product_cat', array( 'fields' => 'names' ) ); $img = wp_get_attachment_url( $p->get_image_id() ); $items[] = array( 'ProductID' => $parent_id, 'SKU' => $p->get_sku(), 'ProductName' => $p->get_name(), 'Quantity' => (int) $ci['quantity'], 'ItemPrice' => (float) wc_get_price_to_display( $p ), 'RowTotal' => isset( $ci['line_total'] ) ? (float) $ci['line_total'] : 0, 'ProductURL' => get_permalink( $parent_id ), 'ImageURL' => $img ? $img : '', 'ProductCategories' => $catnames, ); $names[] = $p->get_name(); $cats = array_merge( $cats, (array) $catnames ); } return array( 'Items' => $items, 'ItemNames' => $names, 'Categories' => array_values( array_unique( $cats ) ) ); } function mk_kl_send( $metric_name, $properties, $value, $profile_attrs, $unique_id = '' ) { if ( empty( $profile_attrs ) ) { return; } $payload = array( 'data' => array( 'type' => 'event', 'attributes' => array( 'properties' => $properties, 'metric' => array( 'data' => array( 'type' => 'metric', 'attributes' => array( 'name' => $metric_name ) ) ), 'profile' => array( 'data' => array( 'type' => 'profile', 'attributes' => $profile_attrs ) ), 'value' => (float) $value, 'time' => gmdate( 'c' ), ) ) ); if ( $unique_id ) { $payload['data']['attributes']['unique_id'] = $unique_id; } wp_remote_post( 'https://a.klaviyo.com/api/events/', array( 'timeout' => 4, 'blocking' => false, 'headers' => array( 'Authorization' => 'Klaviyo-API-Key ' . MK_KLAVIYO_PRIVATE_KEY, 'revision' => MK_KLAVIYO_REVISION, 'accept' => 'application/json', 'content-type' => 'application/json', ), 'body' => wp_json_encode( $payload ), ) ); } add_action( 'woocommerce_add_to_cart', 'mk_kl_added_to_cart', 20, 6 ); function mk_kl_added_to_cart( $key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { if ( ! mk_kl_ready() ) { return; } $profile = mk_kl_profile_attrs(); if ( empty( $profile ) ) { return; } $p = wc_get_product( $variation_id ? $variation_id : $product_id ); if ( ! $p ) { return; } $cart = mk_kl_cart_items(); $catnames = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'names' ) ); $img = wp_get_attachment_url( $p->get_image_id() ); $total = (float) WC()->cart->get_cart_contents_total(); $props = array( '$value' => $total, 'AddedItemProductName' => $p->get_name(), 'AddedItemProductID' => $product_id, 'AddedItemSKU' => $p->get_sku(), 'AddedItemCategories' => $catnames, 'AddedItemImageURL' => $img ? $img : '', 'AddedItemURL' => get_permalink( $product_id ), 'AddedItemPrice' => (float) wc_get_price_to_display( $p ), 'AddedItemQuantity' => (int) $quantity, 'ItemNames' => $cart['ItemNames'], 'Items' => $cart['Items'], 'Categories' => $cart['Categories'], 'CheckoutURL' => wc_get_checkout_url(), ); mk_kl_send( 'Added to Cart', $props, $total, $profile ); } add_action( 'template_redirect', 'mk_kl_started_checkout_pageview' ); function mk_kl_started_checkout_pageview() { if ( ! mk_kl_ready() || is_admin() ) { return; } if ( ! function_exists( 'is_checkout' ) || ! is_checkout() ) { return; } if ( is_wc_endpoint_url( 'order-received' ) || is_wc_endpoint_url( 'order-pay' ) ) { return; } mk_kl_maybe_started_checkout(); } add_action( 'woocommerce_checkout_update_order_review', 'mk_kl_started_checkout_review' ); function mk_kl_started_checkout_review( $post_data ) { if ( ! mk_kl_ready() ) { return; } parse_str( $post_data, $data ); if ( ! empty( $data['billing_email'] ) && is_email( $data['billing_email'] ) ) { WC()->customer->set_billing_email( $data['billing_email'] ); mk_kl_maybe_started_checkout(); } } function mk_kl_maybe_started_checkout() { if ( WC()->cart->is_empty() ) { return; } $profile = mk_kl_profile_attrs(); if ( empty( $profile ) ) { return; } $id_key = isset( $profile['email'] ) ? $profile['email'] : $profile['anonymous_id']; $hash = md5( $id_key . '|' . WC()->cart->get_cart_hash() ); if ( WC()->session && WC()->session->get( 'mk_kl_sc_hash' ) === $hash ) { return; } $cart = mk_kl_cart_items(); $total = (float) WC()->cart->get_cart_contents_total(); $props = array( '$value' => $total, 'ItemNames' => $cart['ItemNames'], 'Items' => $cart['Items'], 'Categories' => $cart['Categories'], 'CheckoutURL' => wc_get_checkout_url(), ); mk_kl_send( 'Started Checkout', $props, $total, $profile, $hash ); if ( WC()->session ) { WC()->session->set( 'mk_kl_sc_hash', $hash ); } }