WooCommerce Ajax添加到购物车facebook像素事件不起作用

时间:2020-05-09 18:08:59

标签: ajax woocommerce facebook-pixel

使用Ajax将产品添加到购物车时,未触发“添加到购物车” Facebook像素事件。

这是我的头一个输出。

    <!-- WooCommerce Facebook Integration Begin -->
    <script <?php echo self::get_script_attributes(); ?>>

        <?php echo $this->get_pixel_init_code(); ?>

        fbq( 'track', 'PageView', <?php echo json_encode( self::build_params( [], 'PageView' ), JSON_PRETTY_PRINT | JSON_FORCE_OBJECT ) ?> );
        document.addEventListener('DOMContentLoaded', function() {
        jQuery && jQuery(function($){
        $('body').on( 'added_to_cart', function()
            {
            // Testing output on browser JS console
            console.log('added_to_cart'); 
        $( 'body' ).append( '<div class=\"wc-facebook-pixel-event-placeholder\"></div>' );


        });
    });
    }, false);
    </script>

将产品添加到购物车时,将在控制台中打印“ added_to_cart”。因此,添加到购物车Ajax的工作正常。

这是未由Ajax触发的功能。

// AddToCart while AJAX is enabled
add_action( 'woocommerce_ajax_added_to_cart', [ $this, 'add_filter_for_add_to_cart_fragments' ] );

// Setups a filter to add an add to cart fragment whenever a product is added to the cart through Ajax.

public function add_filter_for_add_to_cart_fragments() {

            if ( 'no' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
                add_filter( 'woocommerce_add_to_cart_fragments', [ $this, 'add_add_to_cart_event_fragment' ] );
            }
        }

// Adds an add to cart fragment to trigger an AddToCart event.

public function add_add_to_cart_event_fragment( $fragments ) {

            if ( self::$isEnabled ) {

                $script = $this->pixel->get_event_script( 'AddToCart', [
                    'content_ids'  => $this->get_cart_content_ids(),
                    'content_type' => 'product',
                    'contents'     => $this->get_cart_contents(),
                    'value'        => $this->get_cart_total(),
                    'currency'     => get_woocommerce_currency(),
                ] );

                $fragments['div.wc-facebook-pixel-event-placeholder'] = '<div class="wc-facebook-pixel-event-placeholder">' . $script . '</div>';
            }

            return $fragments;
        }

我正在将最新的Wordpress&WooCommerce与Facebook一起用于WooCommerce插件。

0 个答案:

没有答案