PixelYourSite – Facebook Pixel (Events, WooCommerce & Easy Digital Downloads) - Version 5.2.0

Version Description

Download this release

Release Info

Developer PixelYourSite
Plugin Icon 128x128 PixelYourSite – Facebook Pixel (Events, WooCommerce & Easy Digital Downloads)
Version 5.2.0
Comparing to
See all releases

Code changes from version 5.1.0 to 5.2.0

facebook-pixel-master.php CHANGED
@@ -5,17 +5,17 @@
5
  Plugin URI: http://www.pixelyoursite.com/facebook-pixel-plugin-help
6
  Author: PixelYourSite
7
  Author URI: http://www.pixelyoursite.com
8
- Version: 5.1.0
9
  License: GPLv3
10
  WC requires at least: 2.6.0
11
- WC tested up to: 3.0.0
12
  */
13
 
14
  if ( ! defined( 'ABSPATH' ) ) {
15
  exit; // Exit if accessed directly.
16
  }
17
 
18
- define( 'PYS_FREE_VERSION', '5.1.0' );
19
 
20
  if ( ! function_exists( 'pys_is_pixelyoursite_pro_active' ) ) {
21
 
5
  Plugin URI: http://www.pixelyoursite.com/facebook-pixel-plugin-help
6
  Author: PixelYourSite
7
  Author URI: http://www.pixelyoursite.com
8
+ Version: 5.2.0
9
  License: GPLv3
10
  WC requires at least: 2.6.0
11
+ WC tested up to: 3.3.0
12
  */
13
 
14
  if ( ! defined( 'ABSPATH' ) ) {
15
  exit; // Exit if accessed directly.
16
  }
17
 
18
+ define( 'PYS_FREE_VERSION', '5.2.0' );
19
 
20
  if ( ! function_exists( 'pys_is_pixelyoursite_pro_active' ) ) {
21
 
inc/common.php CHANGED
@@ -1292,10 +1292,11 @@ if( ! function_exists( 'pys_woocommerce_events' ) ) {
1292
  if ( pys_get_option( 'woo', 'variation_id' ) != 'main' && isset( $_REQUEST['variation_id'] ) ) {
1293
  $product_id = $_REQUEST['variation_id'];
1294
  } else {
1295
- $product_id = isset( $_REQUEST['add-to-cart'] ) ? $_REQUEST['add-to-cart'] : null;
1296
  }
1297
 
1298
- $params = pys_get_woo_ajax_addtocart_params( $product_id );
 
1299
 
1300
  pys_add_event( 'AddToCart', $params );
1301
 
@@ -1305,59 +1306,6 @@ if( ! function_exists( 'pys_woocommerce_events' ) ) {
1305
 
1306
  }
1307
 
1308
- if( ! function_exists( 'pys_output_woo_ajax_events_code' ) ) {
1309
-
1310
- function pys_output_woo_ajax_events_code() {
1311
- global $pys_woo_ajax_events;
1312
-
1313
- if( empty( $pys_woo_ajax_events ) ) {
1314
- return;
1315
- }
1316
-
1317
- $events = array();
1318
-
1319
- foreach ( $pys_woo_ajax_events as $id => $event ) {
1320
-
1321
- $params = pys_clean_system_event_params( $event['params'] );
1322
-
1323
- // sanitize params
1324
- $sanitized = array();
1325
- foreach ( $params as $name => $value ) {
1326
-
1327
- // skip empty but not zero values
1328
- if ( empty( $value ) && ! is_numeric( $value ) ) {
1329
- continue;
1330
- }
1331
-
1332
- $key = esc_js( $name );
1333
- $sanitized[ $key ] = $value;
1334
-
1335
- }
1336
-
1337
- $name = $event['name'];
1338
-
1339
- $events[ $id ] = array(
1340
- 'type' => pys_is_standard_event( $name ) ? 'track' : 'trackCustom',
1341
- 'name' => $name,
1342
- 'params' => $sanitized
1343
- );
1344
-
1345
- }
1346
-
1347
- ?>
1348
-
1349
- <script type="text/javascript">
1350
- /* <![CDATA[ */
1351
- var pys_woo_ajax_events = <?php echo json_encode( $events ); ?>;
1352
- /* ]]> */
1353
- </script>
1354
-
1355
- <?php
1356
-
1357
- }
1358
-
1359
- }
1360
-
1361
  if( ! function_exists( 'pys_output_options' ) ) {
1362
 
1363
  function pys_output_options() {
1292
  if ( pys_get_option( 'woo', 'variation_id' ) != 'main' && isset( $_REQUEST['variation_id'] ) ) {
1293
  $product_id = $_REQUEST['variation_id'];
1294
  } else {
1295
+ $product_id = isset( $_REQUEST['add-to-cart'] ) ? (int) $_REQUEST['add-to-cart'] : null;
1296
  }
1297
 
1298
+ $qty = isset( $_REQUEST['quantity'] ) ? (int) $_REQUEST['quantity'] : 1;
1299
+ $params = pys_get_woo_product_addtocart_params( $product_id, $qty );
1300
 
1301
  pys_add_event( 'AddToCart', $params );
1302
 
1306
 
1307
  }
1308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1309
  if( ! function_exists( 'pys_output_options' ) ) {
1310
 
1311
  function pys_output_options() {
inc/core.php CHANGED
@@ -4,7 +4,7 @@ if ( ! defined( 'ABSPATH' ) ) {
4
  exit; // Exit if accessed directly.
5
  }
6
 
7
- function pys_get_woo_product_price_to_display( $product_id ) {
8
 
9
  if ( ! $product = wc_get_product( $product_id ) ) {
10
  return 0;
@@ -12,21 +12,21 @@ function pys_get_woo_product_price_to_display( $product_id ) {
12
 
13
  if ( pys_is_wc_version_gte( '2.7' ) ) {
14
 
15
- return wc_get_price_to_display( $product );
16
 
17
  } else {
18
 
19
  return 'incl' === get_option( 'woocommerce_tax_display_shop' )
20
- ? $product->get_price_including_tax()
21
- : $product->get_price_excluding_tax();
22
 
23
  }
24
 
25
  }
26
 
27
- if ( ! function_exists( 'pys_get_woo_ajax_addtocart_params' ) ) {
28
 
29
- function pys_get_woo_ajax_addtocart_params( $product_id ) {
30
 
31
  $content_id = pys_get_product_content_id( $product_id );
32
 
@@ -44,7 +44,7 @@ if ( ! function_exists( 'pys_get_woo_ajax_addtocart_params' ) ) {
44
  break;
45
 
46
  case 'price':
47
- $value = pys_get_product_price( $product_id );
48
  break;
49
 
50
  default:
@@ -60,7 +60,7 @@ if ( ! function_exists( 'pys_get_woo_ajax_addtocart_params' ) ) {
60
  $params['contents'] = json_encode( array(
61
  array(
62
  'id' => (string) reset( $content_id ),
63
- 'quantity' => 1,
64
  'item_price' => pys_get_woo_product_price_to_display( $product_id ),
65
  )
66
  ) );
@@ -185,7 +185,7 @@ if ( ! function_exists( 'pys_get_woo_code' ) ) {
185
  }
186
 
187
  // AddToCart Cart Page Event
188
- if ( pys_get_option( 'woo', 'on_add_to_cart_page' ) && is_cart() ) {
189
 
190
  $ids = array(); // cart items ids or sku
191
  $contents = array();
@@ -391,92 +391,6 @@ if ( ! function_exists( 'pys_get_woo_code' ) ) {
391
 
392
  }
393
 
394
- if ( ! function_exists( 'pys_add_code_to_woo_cart_link' ) ) {
395
-
396
- /**
397
- * Adds data-pixelcode attribute to "add to cart" buttons in the WooCommerce loop.
398
- *
399
- * @param string $tag
400
- * @param WC_Product $product
401
- *
402
- * @return string
403
- */
404
- function pys_add_code_to_woo_cart_link( $tag, $product ) {
405
- global $pys_woo_ajax_events;
406
-
407
- // do not add code if AJAX is disabled. event will be processed by another function
408
- if ( 'yes' !== get_option( 'woocommerce_enable_ajax_add_to_cart' ) ) {
409
- return $tag;
410
- }
411
-
412
- if ( false == pys_woo_product_is_type( $product, 'simple' ) ) {
413
- return $tag;
414
- }
415
-
416
- /**
417
- * @since 5.0.1
418
- */
419
- if ( pys_is_wc_version_gte( '2.6' ) ) {
420
- $product_id = $product->get_id();
421
- } else {
422
- $product_id = $product->post->ID;
423
- }
424
-
425
- $content_id = pys_get_product_content_id( $product_id );
426
-
427
- // common params
428
- $params = array();
429
- $params['content_type'] = 'product';
430
- $params['content_ids'] = json_encode( $content_id );
431
-
432
- // currency, value
433
- if ( pys_get_option( 'woo', 'enable_add_to_cart_value' ) ) {
434
-
435
- $option = pys_get_option( 'woo', 'view_content_value_option' );
436
- switch ( $option ) {
437
- case 'global':
438
- $value = pys_get_option( 'woo', 'view_content_global_value' );
439
- break;
440
-
441
- case 'price':
442
- $value = pys_get_product_price( $product_id );
443
- break;
444
-
445
- default:
446
- $value = null;
447
-
448
- }
449
-
450
- $params['value'] = $value;
451
- $params['currency'] = get_woocommerce_currency();
452
-
453
- }
454
-
455
- // contents
456
- $params['contents'] = json_encode( array(
457
- array(
458
- 'id' => (string) reset( $content_id ),
459
- 'quantity' => 1,
460
- 'item_price' => pys_get_woo_product_price_to_display( $product_id ),
461
- )
462
- ) );
463
-
464
- $params = apply_filters( 'pys_event_params', $params, 'AddToCart' );
465
-
466
- $event_id = 'pys_' . md5( serialize( $params ) );
467
- $tag = pys_insert_attribute( 'data-pys-event-id', $event_id, $tag, true, 'any' );
468
-
469
- $pys_woo_ajax_events[ $event_id ] = array(
470
- 'name' => 'AddToCart',
471
- 'params' => $params
472
- );
473
-
474
- return $tag;
475
-
476
- }
477
-
478
- }
479
-
480
  if ( ! function_exists( 'pys_get_additional_matching_code' ) ) {
481
 
482
  /**
@@ -555,7 +469,7 @@ if ( ! function_exists( 'pys_get_product_price' ) ) {
555
  *
556
  * @return null|int Product price
557
  */
558
- function pys_get_product_price( $product_id ) {
559
 
560
  $product = wc_get_product( $product_id );
561
 
@@ -566,17 +480,17 @@ if ( ! function_exists( 'pys_get_product_price' ) ) {
566
  if ( $product->is_taxable() ) {
567
 
568
  if ( pys_is_wc_version_gte( '3.0' ) ) {
569
- $value = wc_get_price_including_tax( $product, array( 'price' => $product->get_price() ) );
570
  } else {
571
- $value = $product->get_price_including_tax( 1, $product->get_price() );
572
  }
573
 
574
  } else {
575
 
576
  if ( pys_is_wc_version_gte( '3.0' ) ) {
577
- $value = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_price() ) );
578
  } else {
579
- $value = $product->get_price_excluding_tax( 1, $product->get_price() );
580
  }
581
 
582
  }
@@ -613,87 +527,33 @@ if ( ! function_exists( 'pys_get_order_total' ) ) {
613
  }
614
 
615
  function pys_pixel_options() {
616
- global $post;
617
-
618
- $options = array(
619
- 'woo' => array(),
620
- );
621
-
622
- if ( pys_get_option( 'woo', 'enabled' ) && pys_is_woocommerce_active() ) {
623
 
624
- $options['woo']['is_shop'] = is_shop();
625
- $options['woo']['is_cat'] = is_product_category();
626
- $options['woo']['is_product'] = is_product();
627
- $options['woo']['add_to_cart_enabled'] = (bool) pys_get_option( 'woo', 'on_add_to_cart_btn' );
628
-
629
- if ( is_product() ) {
630
-
631
- $product = wc_get_product( $post );
632
-
633
- if ( pys_woo_product_is_type( $product, 'simple' ) ) {
634
- $options['woo']['single_product']['type'] = 'simple';
635
- $options['woo']['single_product']['add_to_cart_params'] = pys_get_woo_ajax_addtocart_params(
636
- $post->ID );
637
- } elseif ( pys_woo_product_is_type( $product, 'variable' ) ) {
638
- $options['woo']['single_product']['type'] = 'variable';
639
- $options['woo']['single_product']['add_to_cart_params'] = pys_woo_product_variations_add_to_cart_params( $product );
640
- }
641
-
642
- }
643
-
644
- }
645
-
646
- $options['ajax_url'] = admin_url( 'admin-ajax.php' );
647
 
648
  return $options;
649
 
650
  }
651
 
652
- function pys_woo_product_variations_add_to_cart_params( $product ) {
653
-
654
- $params = array();
655
- $product = wc_get_product( $product );
656
-
657
- if ( false == $product || false == pys_woo_product_is_type( $product, 'variable' ) ) {
658
- return array();
659
- }
660
-
661
- foreach ( $product->get_available_variations() as $variation ) {
662
- $params[ $variation['variation_id'] ] = pys_get_woo_ajax_addtocart_params( $variation['variation_id'] );
663
- }
664
-
665
 
666
- return $params;
667
-
668
- }
669
 
670
- function pys_woo_product_is_type( $product, $type ) {
671
-
672
- if ( pys_is_wc_version_gte( '2.7' ) ) {
673
- return $type == $product->is_type( $type );
674
- } else {
675
- return $product->product_type == $type;
676
  }
677
-
678
- }
679
 
680
- // front-end ajax handler
681
- add_action( 'wp_ajax_pys_fb_ajax', 'pys_ajax_handler' );
682
- add_action( 'wp_ajax_nopriv_pys_fb_ajax', 'pys_ajax_handler' );
683
 
684
- function pys_ajax_handler() {
685
-
686
- if ( empty( $_GET['sub_action'] ) ) {
687
- wp_die();
688
- }
689
-
690
- switch ( $_GET['sub_action'] ) {
691
- case 'get_woo_product_addtocart_params':
692
- pys_get_woo_ajax_addtocart_params( absint( $_GET['product_id'] ) );
693
- break;
694
 
695
- default:
696
- wp_die( 'Unknown sub action.' );
697
- }
698
 
699
  }
4
  exit; // Exit if accessed directly.
5
  }
6
 
7
+ function pys_get_woo_product_price_to_display( $product_id, $qty = 1 ) {
8
 
9
  if ( ! $product = wc_get_product( $product_id ) ) {
10
  return 0;
12
 
13
  if ( pys_is_wc_version_gte( '2.7' ) ) {
14
 
15
+ return wc_get_price_to_display( $product, array( 'qty' => $qty ) );
16
 
17
  } else {
18
 
19
  return 'incl' === get_option( 'woocommerce_tax_display_shop' )
20
+ ? $product->get_price_including_tax( $qty )
21
+ : $product->get_price_excluding_tax( $qty );
22
 
23
  }
24
 
25
  }
26
 
27
+ if ( ! function_exists( 'pys_get_woo_product_addtocart_params' ) ) {
28
 
29
+ function pys_get_woo_product_addtocart_params( $product_id, $qty = 1 ) {
30
 
31
  $content_id = pys_get_product_content_id( $product_id );
32
 
44
  break;
45
 
46
  case 'price':
47
+ $value = pys_get_product_price( $product_id, $qty );
48
  break;
49
 
50
  default:
60
  $params['contents'] = json_encode( array(
61
  array(
62
  'id' => (string) reset( $content_id ),
63
+ 'quantity' => $qty,
64
  'item_price' => pys_get_woo_product_price_to_display( $product_id ),
65
  )
66
  ) );
185
  }
186
 
187
  // AddToCart Cart Page Event
188
+ if ( is_cart() && ( pys_get_option( 'woo', 'on_add_to_cart_page' ) || get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes' ) ) {
189
 
190
  $ids = array(); // cart items ids or sku
191
  $contents = array();
391
 
392
  }
393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  if ( ! function_exists( 'pys_get_additional_matching_code' ) ) {
395
 
396
  /**
469
  *
470
  * @return null|int Product price
471
  */
472
+ function pys_get_product_price( $product_id, $qty = 1 ) {
473
 
474
  $product = wc_get_product( $product_id );
475
 
480
  if ( $product->is_taxable() ) {
481
 
482
  if ( pys_is_wc_version_gte( '3.0' ) ) {
483
+ $value = wc_get_price_including_tax( $product, array( 'price' => $product->get_price(), 'qty' => $qty ) );
484
  } else {
485
+ $value = $product->get_price_including_tax( $qty, $product->get_price() );
486
  }
487
 
488
  } else {
489
 
490
  if ( pys_is_wc_version_gte( '3.0' ) ) {
491
+ $value = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_price(), 'qty' => $qty ) );
492
  } else {
493
+ $value = $product->get_price_excluding_tax( $qty, $product->get_price() );
494
  }
495
 
496
  }
527
  }
528
 
529
  function pys_pixel_options() {
 
 
 
 
 
 
 
530
 
531
+ $options['ajax_url'] = admin_url( 'admin-ajax.php' );
532
+ $options['woo']['addtocart_enabled'] = pys_is_woocommerce_active() && pys_get_option( 'woo', 'enabled' )
533
+ && (bool) pys_get_option( 'woo', 'on_add_to_cart_btn' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
534
 
535
  return $options;
536
 
537
  }
538
 
539
+ add_action( 'wp_ajax_pys_woo_addtocart_params', 'pys_woo_addtocart_params_ajax_handler' );
540
+ add_action( 'wp_ajax_nopriv_pys_woo_addtocart_params', 'pys_woo_addtocart_params_ajax_handler' );
 
 
 
 
 
 
 
 
 
 
 
541
 
542
+ function pys_woo_addtocart_params_ajax_handler() {
 
 
543
 
544
+ if ( empty( $_GET['product_id'] ) ) {
545
+ wp_send_json_error();
 
 
 
 
546
  }
 
 
547
 
548
+ $qty = empty( $_GET['quantity'] ) ? 1 : (int) $_GET['quantity'];
 
 
549
 
550
+ if ( pys_get_option( 'woo', 'variation_id' ) != 'main' && isset( $_REQUEST['variation_id'] ) ) {
551
+ $product_id = (int) $_GET['variation_id'];
552
+ } else {
553
+ $product_id = (int) $_GET['product_id'];
554
+ }
 
 
 
 
 
555
 
556
+ $params = pys_get_woo_product_addtocart_params( $product_id, $qty );
557
+ wp_send_json_success( $params );
 
558
 
559
  }
inc/general.php CHANGED
@@ -182,14 +182,8 @@ function pys_free_init() {
182
  add_action( 'wp_head', 'pys_output_custom_events_code', 4 );
183
 
184
  add_action( 'wp_footer', 'pys_output_noscript_code', 10 );
185
- add_action( 'wp_footer', 'pys_output_woo_ajax_events_code', 10 );
186
  add_action( 'wp_footer', 'pys_output_edd_ajax_events_code', 10 );
187
 
188
- // woocommerce shop page add_to_cart
189
- if ( pys_get_option( 'woo', 'enabled' ) && pys_get_option( 'woo', 'on_add_to_cart_btn' ) ) {
190
- add_filter( 'woocommerce_loop_add_to_cart_link', 'pys_add_code_to_woo_cart_link', 10, 2 );
191
- }
192
-
193
  ## add pixel code to EDD add_to_cart buttons
194
  if ( pys_get_option( 'edd', 'enabled' ) && pys_get_option( 'edd', 'on_add_to_cart_btn', false ) ) {
195
  add_filter( 'edd_purchase_link_args', 'pys_edd_purchase_link_args', 10, 1 );
182
  add_action( 'wp_head', 'pys_output_custom_events_code', 4 );
183
 
184
  add_action( 'wp_footer', 'pys_output_noscript_code', 10 );
 
185
  add_action( 'wp_footer', 'pys_output_edd_ajax_events_code', 10 );
186
 
 
 
 
 
 
187
  ## add pixel code to EDD add_to_cart buttons
188
  if ( pys_get_option( 'edd', 'enabled' ) && pys_get_option( 'edd', 'on_add_to_cart_btn', false ) ) {
189
  add_filter( 'edd_purchase_link_args', 'pys_edd_purchase_link_args', 10, 1 );
inc/html-tab-woo-general.php CHANGED
@@ -191,9 +191,20 @@ if ( ! defined( 'ABSPATH' ) ) {
191
 
192
  <tr>
193
  <td colspan="2" class="narrow">
 
 
 
 
 
 
194
  <input type="checkbox" name="pys[woo][on_add_to_cart_page]" value="1" class="woo-option"
195
- <?php pys_checkbox_state( 'woo', 'on_add_to_cart_page' ); ?> >
196
  <strong>Enable AddToCart on cart page</strong>
 
 
 
 
 
197
  </td>
198
  </tr>
199
 
191
 
192
  <tr>
193
  <td colspan="2" class="narrow">
194
+
195
+ <?php
196
+ $checked = get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes' || pys_get_option( 'woo', 'on_add_to_cart_page' ) == "1";
197
+ $disabled = get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes';
198
+ ?>
199
+
200
  <input type="checkbox" name="pys[woo][on_add_to_cart_page]" value="1" class="woo-option"
201
+ <?php checked( $checked ); disabled( $disabled ); ?> >
202
  <strong>Enable AddToCart on cart page</strong>
203
+ <?php if ( $disabled ) : ?>
204
+ <span class="help">In order to disable the event you need to turn off the "Redirect to the cart
205
+ page after successful addition" option from <a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=products' ); ?>"
206
+ target="_blank">WooCommerce</a> settings</span>
207
+ <?php endif; ?>
208
  </td>
209
  </tr>
210
 
js/public.js CHANGED
@@ -4,8 +4,6 @@ jQuery(document).ready(function( $ ) {
4
  return;
5
  }
6
 
7
- var options = pys_fb_pixel_options; // variable shorthand
8
-
9
  // load FB pixel
10
  !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
11
  n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
@@ -13,98 +11,63 @@ jQuery(document).ready(function( $ ) {
13
  t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
14
  document,'script','https://connect.facebook.net/en_US/fbevents.js');
15
 
16
- /**
17
- * Setup Events Handlers
18
- */
19
- !function setupEventHandlers() {
20
-
21
- /**
22
- * WooCommerce Events
23
- */
24
- if (options.hasOwnProperty('woo')) {
25
-
26
- // WooCommerce Single Product AJAX AddToCart handler
27
- if (options.woo.is_product && options.woo.add_to_cart_enabled ) {
28
 
29
- $(document).on('added_to_cart', function () {
 
 
 
 
 
30
 
31
- var params = {};
 
 
32
 
33
- if (options.woo.single_product.type === 'variable') {
 
34
 
35
- var $form = $('form.variations_form.cart'),
36
- variation_id = false,
37
- qty;
38
-
39
- if ($form.length === 1) {
40
- variation_id = $form.find('input[name="variation_id"]').val();
41
- }
42
-
43
- if (false === variation_id || false === options.woo.single_product.add_to_cart_params.hasOwnProperty(variation_id)) {
44
- console.error('PYS PRO: product variation ID not found in available product variants.');
45
- return;
46
- }
47
-
48
- params = clone(options.woo.single_product.add_to_cart_params[variation_id], {});
49
- qty = parseInt($form.find('input[name="quantity"]').val());
50
- params.value = params.value * qty;
51
 
52
- } else {
53
- params = clone(options.woo.single_product.add_to_cart_params, {});
54
- }
 
 
 
55
 
56
- fbq('track', 'AddToCart', params);
 
 
57
 
58
- });
59
 
60
  }
 
61
 
62
- // WooCommerce Shop and Product Category AJAX AddToCart handler
63
- if ( ( options.woo.is_shop || options.woo.is_cat ) && options.woo.add_to_cart_enabled ) {
64
-
65
- $(document).on('adding_to_cart', function ( e, $button, data ) {
66
-
67
- data.action = 'pys_fb_ajax';
68
- data.sub_action = 'get_woo_product_addtocart_params';
69
-
70
- $.get( options.ajax_url, data, function( response ) {
71
-
72
- if ( ! response ) {
73
- return;
74
- }
75
-
76
- if ( response.error ) {
77
- return;
78
- }
79
-
80
- fbq('track', 'AddToCart', response.data);
81
-
82
- });
83
-
84
- });
85
 
86
- }
 
 
 
 
87
 
88
- }
 
 
89
 
90
- }();
91
 
92
  regularEvents();
93
  customCodeEvents();
94
 
95
- // AddToCart button
96
- $(".ajax_add_to_cart").click(function(e){
97
-
98
- var attr = $(this).attr('data-pys-event-id');
99
-
100
- if( typeof attr == 'undefined' || typeof pys_woo_ajax_events == 'undefined' ) {
101
- return;
102
- }
103
-
104
- evaluateEventByID( attr.toString(), pys_woo_ajax_events );
105
-
106
- });
107
-
108
  // EDD AddToCart
109
  $('.edd-add-to-cart').click(function () {
110
 
@@ -210,11 +173,4 @@ jQuery(document).ready(function( $ ) {
210
 
211
  }
212
 
213
- var clone = function (src, dest) {
214
- for (var key in src) {
215
- dest[key] = src[key];
216
- }
217
- return dest;
218
- };
219
-
220
  });
4
  return;
5
  }
6
 
 
 
7
  // load FB pixel
8
  !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
9
  n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
11
  t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
12
  document,'script','https://connect.facebook.net/en_US/fbevents.js');
13
 
14
+ var fbAddToCart = function ($button) {
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ var data = {
17
+ action: 'pys_woo_addtocart_params',
18
+ product_id: undefined,
19
+ variation_id: undefined,
20
+ quantity: 1
21
+ };
22
 
23
+ $.each($button.data(), function (key, value) {
24
+ data[key] = value;
25
+ });
26
 
27
+ var $variations_form = $button.closest('form.variations_form.cart'),
28
+ $single_form = $button.closest('form.cart');
29
 
30
+ if ($variations_form.length === 1) {
31
+ data.variation_id = parseInt($variations_form.find('input[name="variation_id"]').val());
32
+ data.quantity = parseInt($variations_form.find('input[name="quantity"]').val());
33
+ } else if ($single_form.length === 1) {
34
+ data.product_id = parseInt($single_form.find('input[name="add-to-cart"]').val());
35
+ data.quantity = parseInt($single_form.find('input[name="quantity"]').val());
36
+ }
 
 
 
 
 
 
 
 
 
37
 
38
+ $.ajax({
39
+ url: pys_fb_pixel_options.ajax_url,
40
+ data: data,
41
+ dataType: 'json',
42
+ async: false,
43
+ success: function (response) {
44
 
45
+ if (!response || response.error) {
46
+ return;
47
+ }
48
 
49
+ fbq('track', 'AddToCart', response.data);
50
 
51
  }
52
+ });
53
 
54
+ };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ /**
57
+ * WooCommerce AddToCart on button
58
+ */
59
+ if (pys_fb_pixel_options.woo.addtocart_enabled && typeof wc_add_to_cart_params !== 'undefined'
60
+ && wc_add_to_cart_params.cart_redirect_after_add !== 'yes') {
61
 
62
+ $(document.body).on('added_to_cart', function (e, fragments, cart_hash, $button) {
63
+ fbAddToCart($button);
64
+ });
65
 
66
+ }
67
 
68
  regularEvents();
69
  customCodeEvents();
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  // EDD AddToCart
72
  $('.edd-add-to-cart').click(function () {
73
 
173
 
174
  }
175
 
 
 
 
 
 
 
 
176
  });
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: Facebook Pixel, New Facebook Pixel, Facebook Conversion Pixel, Facebook Pi
4
  Requires at least: 3.0.1
5
  Requires PHP: 5.3
6
  Tested up to: 4.9
7
- Stable tag: 5.1.0
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -300,6 +300,14 @@ We have an article about how to create Custom Reports:
300
 
301
  == Changelog ==
302
 
 
 
 
 
 
 
 
 
303
  = PixelYourSite 5.1.0 =
304
 
305
  * Fixing possible compatibility problem with Facebook for WooCommerce extension
4
  Requires at least: 3.0.1
5
  Requires PHP: 5.3
6
  Tested up to: 4.9
7
+ Stable tag: 5.2.0
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
300
 
301
  == Changelog ==
302
 
303
+ = PixelYourSite 5.2.0 =
304
+
305
+ * Fixing a possible error with the add to cart button when WooCommerce and WPML are installed on some hostings (we were able to replicate it on SiteGround servers only)
306
+
307
+ * Enabling the AddToCart event on the Cart page when WooCommerce "Redirect to the cart page after successful addition" option is active. This will ensure that the events are captured.
308
+
309
+ * Other small improvements to the code related to the AddToCart event
310
+
311
  = PixelYourSite 5.1.0 =
312
 
313
  * Fixing possible compatibility problem with Facebook for WooCommerce extension