trackingId = Settings::get( 'pixel_id' ); if ( $this->is_activated() ) { // add_action( 'wp_enqueue_scripts', [ &$this, 'enqueueScript' ] ); // add_action( 'wp_head', [ &$this, 'loadBaseScript' ] ); // add_action( 'ctx_after_pixel_init', [ &$this, 'trigger_event' ], 11 ); } // Ajax adds to cart add_action( 'wp_ajax_add_to_cart_facebook_pixel', [ &$this, 'ajax_add_to_cart_data' ] ); add_action( 'wp_ajax_nopriv_add_to_cart_facebook_pixel', [ &$this, 'ajax_add_to_cart_data' ] ); } /** * Is Facebook Pixel Tracking Enabled. * * @return bool */ public function is_activated() { return ! empty( $this->trackingId ) && 'enable' === Settings::get( 'disable_pixel' ); } /** * Enqueue Ajax Add to Cart Event Code. * * @return void */ public function enqueueScript() { wp_enqueue_script( 'woo-feed-facebook-pixel,', WOO_FEED_PLUGIN_URL . 'admin/js/woo-feed-facebook-pixel.min.js', [ 'jquery', 'wp-util' ], '1.0.0', true ); } /** * Load Base Script. * * @return void */ public function loadBaseScript() { // @ToDo Language Code check. Currently passing all to `en_US` ?> ViewContent(); } // Trigger AddToCart event on Add to Cart by form post. if ( isset( $_POST['add-to-cart'] ) ) { $this->AddToCart(); } // Trigger on Cart Page. if ( is_cart() ) { $this->AddToCarts(); } if ( is_wc_endpoint_url( 'order-received' ) ) { $this->Purchase(); } } /** * Get item info by Ids. * * @param $ids * * @return array|false */ private function get_content_info( $ids = [] ) { if ( ! empty( $ids ) ) { $data['content_ids'] = $ids; $data['content_type'] = 'product'; $data['currency'] = get_woocommerce_currency(); $value = 0; foreach ( $ids as $id ) { $product = wc_get_product( $id ); if ( ! is_object( $product ) ) { continue; } $value += (int) $product->get_price(); } $data['value'] = $value; return $data; } return false; } /** * Load ViewContent Event Script. * * @return void */ public function ViewContent() { if ( is_product() ) { global $post; $_product = wc_get_product( (int) $post->ID ); $id = $_product->get_ID(); $ids = [ $id ]; if ( "variable" === $_product->get_type() ) { $ids = $_product->get_children(); } $data = $this->get_content_info( $ids ); if ( $data ) { ?> ttq.track('ViewContent', ); get_content_info( [ $product_id ] ); if ( $data ) { ?> ttq.track('AddToCart', ); cart->is_empty() ) { $ids = []; foreach ( WC()->cart->get_cart() as $cart_item ) { $ids[] = $cart_item['product_id']; } $data = $this->get_content_info( $ids ); if ( $data ) { ?> ttq.track('AddToCart', ); query_vars['order-received'] ) ) { $order = wc_get_order( $wp_query->query_vars['order-received'] ); $ids = []; foreach ( $order->get_items() as $item ) { $ids[] = $item->get_product_id(); } $data = $this->get_content_info( $ids ); if ( $data ) { ?> ttq.track('PlaceAnOrder', ); ttq.track('InitiateCheckout') ttq.track('CompleteRegistration'); ttq.track('CompletePayment', ); get_content_info( [ $product_id ] ); } wp_send_json_success( json_encode( $data ) ); } }