Plugin Directory

Changeset 2309723


Ignore:
Timestamp:
05/21/2020 08:21:22 PM (6 years ago)
Author:
xevibrainity
Message:

Imported changes from Git repository

File:
1 edited

Legend:

Unmodified
Added
Removed
  • brainity-boost-ads/trunk/pixel/FacebookWordpressPixelInjection.php

    r2309641 r2309723  
    99    const FB_PRIORITY_LOW = 11;
    1010    const FB_RETAILER_ID_PREFIX = 'wc_post_id_';
     11    public $isviewcontentRender = 0;
    1112
    1213    public function __construct()
     
    8384    public function inject_view_content_event()
    8485    {
    85         global $post;       
    86         if (
    87             empty(FacebookPixel::getPixelId() || !isset($post->ID))
    88         ) {
    89             return;
    90         }
    9186       
    92         $product = wc_get_product($post->ID);
    93         $content_type = 'product_group';
    94         if (!$product) {
    95             return;
    96         }
    97 
    98         // if product is a variant, fire the pixel with content_type: product_group
    99         if (self::is_variation_type($product->get_type())) {
     87        global $post;
     88        if ( empty(FacebookPixel::getPixelId() || !isset($post->ID)) ) {
     89            return;
     90        }
     91        $product = wc_get_product( $post->ID );
     92        if ( ! $product instanceof \WC_Product ) {
     93            return;
     94        }
     95
     96        // if product is variable or grouped, fire the pixel with content_type: product_group
     97        if ( $product->is_type( [ 'variable', 'grouped' ] ) ) {
     98            $content_type = 'product_group';
     99        } else {
    100100            $content_type = 'product';
    101101        }
    102102
    103103        $content_ids = self::get_fb_content_ids($product);
    104         FacebookPixel::inject_event(
    105             'ViewContent',
    106             array(
    107                 'content_name' => $product->get_title(),
    108                 'content_ids' => json_encode($content_ids),
    109                 'content_type' => $content_type,
    110                 'value' => $product->get_price(),
    111                 'currency' => get_woocommerce_currency()
    112             ));
     104        if($this->isviewcontentRender == wp_json_encode($content_ids)) {
     105            return;
     106        }
     107        $this->isviewcontentRender = wp_json_encode($content_ids);
     108        $this->pixel->inject_event( 'ViewContent', [
     109            'content_name' => $product->get_title(),
     110            'content_ids'  => $this->isviewcontentRender,
     111            'content_type' => $content_type,
     112            'value'        => $product->get_price(),
     113            'currency'     => get_woocommerce_currency(),
     114        ] );
     115
    113116    }
    114117
     
    172175            return;
    173176        }
     177
     178        $last_product_id = WC()->session->get( 'facebook_for_woocommerce_last_product_added_to_cart', 0 );
     179
    174180        $redirect_checked = get_option('woocommerce_cart_redirect_after_add', 'no');
    175         if ($redirect_checked == 'yes') {
     181        if ($redirect_checked == 'yes' && $last_product_id > 0) {
    176182            $this->inject_add_to_cart_event();
     183            WC()->session->set( 'facebook_for_woocommerce_last_product_added_to_cart', 0 );
    177184        }
    178185    }
     
    243250
    244251        return $fragments;
     252    }
     253
     254    /**
     255     * Sets last product added to cart to session when adding a product to cart from an archive page and both AJAX adding and redirection to cart are enabled.
     256     *
     257     * @internal
     258     *
     259     * @since 1.10.2
     260     *
     261     * @param null|int $product_id the ID of the product just added to the cart
     262     */
     263    public function set_last_product_added_to_cart_upon_ajax_redirect( $product_id = null ) {
     264        if ( ! $product_id ) {
     265            return;
     266        }
     267        $product = wc_get_product( $product_id );
     268        if ( $product instanceof \WC_Product ) {
     269            WC()->session->set( 'facebook_for_woocommerce_last_product_added_to_cart', $product->get_id() );
     270        }
    245271    }
    246272
Note: See TracChangeset for help on using the changeset viewer.