$price, 'currency' => x_pixel_wpsc_currency(), 'contents' => x_pixel_wpsc_content( $product_id, get_the_title( $product_id ), $price, 1 ), ); $code = x_pixel_event_code( x_pixel_get_id(), $event_id ); if ( $code === '' ) { return; } x_pixel_print_event_script( $code, $params ); } function x_pixel_wpsc_on_add_to_cart( $product, $cart_item ) { $event_id = get_option( X_PIXEL_OPTION_EVENT_ATC, '' ); if ( $event_id === '' || ! is_object( $cart_item ) || empty( $cart_item->product_id ) ) { return; } $qty = isset( $cart_item->quantity ) ? max( 1, (int) $cart_item->quantity ) : 1; $price = isset( $cart_item->unit_price ) ? (float) $cart_item->unit_price : null; $name = isset( $cart_item->product_name ) ? $cart_item->product_name : get_the_title( $cart_item->product_id ); $conversion_id = 'atc-' . wp_generate_uuid4(); $params = array( 'value' => (float) $price * $qty, 'currency' => x_pixel_wpsc_currency(), 'num_items' => $qty, 'contents' => x_pixel_wpsc_content( $cart_item->product_id, $name, $price, $qty ), 'conversion_id' => $conversion_id, ); x_pixel_wpsc_pending_add_to_cart( array( 'event_id' => $event_id, 'params' => $params, ) ); x_pixel_send_capi( $event_id, $params, x_pixel_request_identifiers(), $conversion_id, x_pixel_event_source_url() ); } function x_pixel_wpsc_add_to_cart_response( $response ) { $pending = x_pixel_wpsc_pending_add_to_cart(); if ( ! is_array( $response ) || ! is_array( $pending ) ) { return $response; } $code = x_pixel_event_code( x_pixel_get_id(), $pending['event_id'] ); if ( $code === '' ) { return $response; } ob_start(); x_pixel_print_event_script( $code, $pending['params'] ); $script = ob_get_clean(); $widget = isset( $response['widget_output'] ) && is_string( $response['widget_output'] ) ? $response['widget_output'] : ''; $response['widget_output'] = $widget . $script; return $response; } function x_pixel_wpsc_on_checkout() { if ( ! function_exists( 'wpsc_is_checkout' ) || ! wpsc_is_checkout() ) { return; } $event_id = get_option( X_PIXEL_OPTION_EVENT_CHECKOUT, '' ); if ( $event_id === '' ) { return; } global $wpsc_cart; if ( ! is_object( $wpsc_cart ) || empty( $wpsc_cart->cart_items ) || ! is_array( $wpsc_cart->cart_items ) ) { return; } $contents = array(); $qty = 0; $lines = array(); foreach ( $wpsc_cart->cart_items as $item ) { if ( ! is_object( $item ) || empty( $item->product_id ) ) { continue; } $item_qty = isset( $item->quantity ) ? max( 1, (int) $item->quantity ) : 1; $price = isset( $item->unit_price ) ? (float) $item->unit_price : null; $name = isset( $item->product_name ) ? $item->product_name : get_the_title( $item->product_id ); $qty += $item_qty; $contents = array_merge( $contents, x_pixel_wpsc_content( $item->product_id, $name, $price, $item_qty ) ); $lines[] = array( (string) $item->product_id, $item_qty, $price ); } if ( $contents === array() ) { return; } $cart_hash = md5( wp_json_encode( $lines ) ); $conversion_id = x_pixel_conversion_id( 'checkout-' . $cart_hash ); $total = method_exists( $wpsc_cart, 'calculate_total_price' ) ? $wpsc_cart->calculate_total_price() : 0; $params = array( 'value' => (float) $total, 'currency' => x_pixel_wpsc_currency(), 'num_items' => $qty, 'contents' => $contents, 'conversion_id' => $conversion_id, ); $code = x_pixel_event_code( x_pixel_get_id(), $event_id ); if ( $code === '' ) { return; } x_pixel_print_event_script( $code, $params ); if ( ! function_exists( 'wpsc_get_customer_meta' ) || ! function_exists( 'wpsc_update_customer_meta' ) ) { return; } if ( wpsc_get_customer_meta( 'x_pixel_checkout_capi_hash' ) === $cart_hash ) { return; } $sent = x_pixel_send_capi( $event_id, $params, x_pixel_request_identifiers(), $conversion_id, x_pixel_current_url() ); if ( $sent ) { wpsc_update_customer_meta( 'x_pixel_checkout_capi_hash', $cart_hash ); } } function x_pixel_wpsc_on_transaction_results( $purchase_log, $session_id, $display_to_screen ) { if ( ! $display_to_screen || ! is_object( $purchase_log ) || ! method_exists( $purchase_log, 'is_transaction_completed' ) ) { return; } $event_id = get_option( X_PIXEL_OPTION_EVENT_PURCHASE, '' ); if ( $event_id === '' || ! $purchase_log->is_transaction_completed() ) { return; } $log_id = (int) $purchase_log->get( 'id' ); if ( $log_id <= 0 || ! function_exists( 'wpsc_get_purchase_meta' ) || ! function_exists( 'wpsc_update_purchase_meta' ) ) { return; } $contents = array(); $qty = 0; $items = $purchase_log->get_items(); if ( ! is_array( $items ) ) { $items = array(); } foreach ( $items as $item ) { if ( ! is_object( $item ) || empty( $item->prodid ) ) { continue; } $item_qty = isset( $item->quantity ) ? max( 1, (int) $item->quantity ) : 1; $price = isset( $item->price ) ? (float) $item->price : null; $name = isset( $item->name ) ? $item->name : get_the_title( $item->prodid ); $qty += $item_qty; $contents = array_merge( $contents, x_pixel_wpsc_content( $item->prodid, $name, $price, $item_qty ) ); } $email = function_exists( 'wpsc_get_buyers_email' ) ? (string) wpsc_get_buyers_email( $log_id ) : ''; $conversion_id = x_pixel_conversion_id( 'wpsc-' . $log_id ); $params = array( 'value' => (float) $purchase_log->get( 'totalprice' ), 'currency' => x_pixel_wpsc_currency(), 'num_items' => $qty, 'contents' => $contents, 'conversion_id' => $conversion_id, 'description' => sprintf( 'WP eCommerce purchase %d', $log_id ), ) + x_pixel_matching_params( $email, '', '', '' ); if ( wpsc_get_purchase_meta( $log_id, '_x_pixel_purchase_browser_sent', true ) !== '1' ) { $code = x_pixel_event_code( x_pixel_get_id(), $event_id ); if ( $code !== '' ) { x_pixel_print_event_script( $code, $params ); wpsc_update_purchase_meta( $log_id, '_x_pixel_purchase_browser_sent', '1' ); } } if ( wpsc_get_purchase_meta( $log_id, '_x_pixel_purchase_sent', true ) === '1' ) { return; } $sent = x_pixel_send_capi( $event_id, $params, x_pixel_matching_identifiers( x_pixel_request_identifiers(), $params ), $conversion_id, x_pixel_current_url() ); if ( $sent ) { wpsc_update_purchase_meta( $log_id, '_x_pixel_purchase_sent', '1' ); } } function x_pixel_register_wpsc_hooks() { if ( ! class_exists( 'WP_eCommerce' ) && ! defined( 'WPSC_VERSION' ) ) { return; } add_action( 'wpsc_add_to_cart', 'x_pixel_wpsc_on_add_to_cart', 40, 2 ); add_filter( 'wpsc_add_to_cart_json_response', 'x_pixel_wpsc_add_to_cart_response', 40 ); add_action( 'wp_footer', 'x_pixel_wpsc_on_product_view', 15 ); add_action( 'wp_footer', 'x_pixel_wpsc_on_checkout', 16 ); add_action( 'wpsc_transaction_results_shutdown', 'x_pixel_wpsc_on_transaction_results', 40, 3 ); } add_action( 'plugins_loaded', 'x_pixel_register_wpsc_hooks' );