diff --git a/composer.json b/composer.json index c76633c..36ebdf7 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "anttiviljami/wc-fb-conversion-tracking", + "name": "anttiviljami/woocommerce-facebook-conversion-pixel", "description": "Set up the Facebook conversion pixel and event tracking for WooCommerce", "type": "wordpress-plugin", "license": "GPLv3", diff --git a/inc/class-wc-integration-fb-conversion-tracking.php b/inc/class-wc-integration-fb-conversion-pixel.php similarity index 78% rename from inc/class-wc-integration-fb-conversion-tracking.php rename to inc/class-wc-integration-fb-conversion-pixel.php index af6cd26..c17f39d 100644 --- a/inc/class-wc-integration-fb-conversion-tracking.php +++ b/inc/class-wc-integration-fb-conversion-pixel.php @@ -1,13 +1,13 @@ id = 'wc-fb-conversion-tracking'; - $this->method_title = __( 'Facebook', 'wc-fb-conversion-tracking' ); - $this->method_description = __( 'Set up the Facebook conversion pixel and event tracking for WooCommerce.', 'wc-fb-conversion-tracking' ); + $this->id = 'wc-fb-conversion-pixel'; + $this->method_title = __( 'Facebook', 'wc-fb-conversion-pixel' ); + $this->method_description = __( 'Set up the Facebook conversion pixel and event tracking for WooCommerce.', 'wc-fb-conversion-pixel' ); // Load the settings. $this->init_form_fields(); @@ -36,35 +36,35 @@ public function __construct() { * User configurable settings */ public function init_form_fields() { - $this->form_fields = apply_filters( 'wc_integration_fb_tracking_fields', array( + $this->form_fields = apply_filters( 'wc_integration_fb_conversion_pixel_fields', array( 'fbid' => array( - 'title' => __( 'Facebook Tracking ID', 'wc-fb-conversion-tracking' ), + 'title' => __( 'Facebook Tracking ID', 'wc-fb-conversion-pixel' ), 'type' => 'text', - 'description' => __( "The numerical unique ID from your Facebook Pixel Tracking Code. Copied from this line: fbq('init', '');", 'wc-fb-conversion-tracking' ), + 'description' => __( "The numerical unique ID from your Facebook Pixel Tracking Code. Copied from this line: fbq('init', '');", 'wc-fb-conversion-pixel' ), 'placeholder' => '123456789', ), 'fb_event_viewcontent' => array( - 'title' => __( 'Track ViewContent Events', 'wc-fb-conversion-tracking' ), + 'title' => __( 'Track ViewContent Events', 'wc-fb-conversion-pixel' ), 'type' => 'checkbox', - 'label' => __( 'Enable event tracking for when a user views a product page.', 'wc-fb-conversion-tracking' ), + 'label' => __( 'Enable event tracking for when a user views a product page.', 'wc-fb-conversion-pixel' ), 'default' => 'yes', ), 'fb_event_addtocart' => array( - 'title' => __( 'Track AddToCart Events', 'wc-fb-conversion-tracking' ), + 'title' => __( 'Track AddToCart Events', 'wc-fb-conversion-pixel' ), 'type' => 'checkbox', - 'label' => __( 'Enable event tracking for when a user adds a product to their shopping cart.', 'wc-fb-conversion-tracking' ), + 'label' => __( 'Enable event tracking for when a user adds a product to their shopping cart.', 'wc-fb-conversion-pixel' ), 'default' => 'yes', ), 'fb_event_checkout' => array( - 'title' => __( 'Track InitiateCheckout Events', 'wc-fb-conversion-tracking' ), + 'title' => __( 'Track InitiateCheckout Events', 'wc-fb-conversion-pixel' ), 'type' => 'checkbox', - 'label' => __( 'Enable event tracking for when a user enters the Checkout page.', 'wc-fb-conversion-tracking' ), + 'label' => __( 'Enable event tracking for when a user enters the Checkout page.', 'wc-fb-conversion-pixel' ), 'default' => 'yes', ), 'fb_event_purchase' => array( - 'title' => __( 'Track Purchase Events', 'wc-fb-conversion-tracking' ), + 'title' => __( 'Track Purchase Events', 'wc-fb-conversion-pixel' ), 'type' => 'checkbox', - 'label' => __( 'Enable event tracking for when a user has succesfully made an order.', 'wc-fb-conversion-tracking' ), + 'label' => __( 'Enable event tracking for when a user has succesfully made an order.', 'wc-fb-conversion-pixel' ), 'default' => 'yes', ), )); @@ -121,7 +121,7 @@ public function fb_tracking_pixel() { global $product; $params = array(); $params['content_name'] = $product->get_title(); - $params['content_ids'] = array( $product->id ); + $params['content_ids'] = array( $product->get_sku() ? $product->get_sku() : $product->id ); $params['content_type'] = 'product'; $params['value'] = floatval( $product->get_price() ); $params['currency'] = get_woocommerce_currency(); @@ -129,13 +129,38 @@ public function fb_tracking_pixel() { fbq('track', 'ViewContent', ); -event_checkout ) : ?> +event_purchase ) : ?> +query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0; + if( $order_id ) { + $order = new WC_Order( $order_id ); + if( $order->get_items() ) { + $productids = array(); + foreach ( $order->get_items() as $item ) { + $product = $order->get_product_from_item( $item ); + $productids[] = $product->get_sku() ? $product->get_sku() : $product->id; + } + $params['content_ids'] = $productids; + } + $params['content_type'] = 'product'; + $params['value'] = $order->get_total(); + $params['currency'] = get_woocommerce_currency(); + } +?> +fbq('track', 'Purchase', ); + +event_checkout ) : ?> cart->get_cart(); $productids = array(); - foreach($cart as $id => $product) { - $productids[] = $product['product_id']; + foreach($cart as $id => $item) { + $product_id = $item['variation_id'] ? $item['variation_id'] : $item['product_id']; + $product = new WC_Product( $product_id ); + $productids[] = $product->get_sku() ? $product->get_sku() : $product->id; } $params = array(); $params['num_items'] = WC()->cart->cart_contents_count; @@ -146,14 +171,6 @@ public function fb_tracking_pixel() { fbq('track', 'InitiateCheckout', ); -event_purchase ) : ?> - -fbq('track', 'Purchase', ); -