Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonporter007/ad6b923c08672976a0ce05a5fb1a36a0 to your computer and use it in GitHub Desktop.
Save simonporter007/ad6b923c08672976a0ce05a5fb1a36a0 to your computer and use it in GitHub Desktop.
Facebook for WooCommerce: Track AddToCart events even if using third-party add-to-cart redirect plugin
<?php // copy only if required!
/*
* If using the add to cart redirect plugin: https://woocommerce.com/products/woocommerce-add-cart-redirect/
* This will still add the necessary actions to track AddToCart events with the Facebook pixel
*/
function sv_fb_for_woocommerce_add_to_cart_redirect_plugin_compatibility() {
if ( function_exists( 'facebook_for_woocommerce' ) ) {
$events_tracker = facebook_for_woocommerce()->get_integration()->events_tracker;
if ( (bool) get_option( 'woo_cart_redirect_to_page' ) ) {
add_filter( 'woocommerce_add_to_cart_redirect', [ $events_tracker, 'set_last_product_added_to_cart_upon_redirect' ], 10, 2 );
add_action( 'woocommerce_ajax_added_to_cart', [ $events_tracker, 'set_last_product_added_to_cart_upon_ajax_redirect' ] );
add_action( 'woocommerce_after_cart', [ $events_tracker, 'inject_add_to_cart_redirect_event' ], 10, 2 );
}
}
}
add_action( 'init', 'sv_fb_for_woocommerce_add_to_cart_redirect_plugin_compatibility' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment