Facebook Meta ads issue

Open to all! Ask other shopowners for help.
User avatar
Pierre_P
Contributor
Posts: 144
Joined: Fri Mar 12, 2021 5:06 am
Phoenix Version: v1.1.0.6
Has thanked: 21 times
Been thanked: 11 times

Facebook Meta ads issue

Post by Pierre_P »

I am trying for days now to figure out facebook meta ads itself - and here is where i am getting stuck.

I did add pixel and meta is showing me in my catalog events that i am missing 3 items - "Product adds to cart, Product purchases and Product views"

I made a header tag which contains the PageView, InitiateCheckout and Purchase showing fine

How to add the "Product adds to cart" as it seems it needs to work with the add to cart button?

Code required is in this format:

Code: Select all

fbq('track', 'AddToCart', {
  content_ids: ['123'], // 'REQUIRED': array of product IDs
  content_type: 'product', // RECOMMENDED: Either product or product_group based on the content_ids or contents being passed.
});
Any ideas?


Join The Code Co-op to get access to your library in the Code Co-op Forum
BrockleyJohn
Certified Developer
Posts: 173
Joined: Mon Mar 01, 2021 5:37 pm
Phoenix Version:
Has thanked: 2 times
Been thanked: 30 times

Re: Facebook Meta ads issue

Post by BrockleyJohn »

There are two actions which add products to the cart after a button has been pressed; add_product and buy_now.
They add to the cart and then redirect, so you need to pass the data on to the next page so it can be fed to the pixel.

An approach would be to override each of these classes and set a session variable for which you can test in a header tags module on the next displayed page.
User avatar
Pierre_P
Contributor
Posts: 144
Joined: Fri Mar 12, 2021 5:06 am
Phoenix Version: v1.1.0.6
Has thanked: 21 times
Been thanked: 11 times

Re: Facebook Meta ads issue

Post by Pierre_P »

@BrockleyJohn
Thanks for this, i am stuck to adding the product details to a session in both files as you mentioned for add_product and buy_now. Example for includes/actions/add_product.php

My code that i wanted to add for example:

Code: Select all

           if (defined('MODULE_HEADER_TAGS_META_PIXEL_STATUS') && MODULE_HEADER_TAGS_META_PIXEL_STATUS == 'True' ) {
                // Store event data in session
                $_SESSION['facebook_pixel_add_to_cart'] = [
                    'content_name' => $product_name,
                    'content_ids' => [$pid],
                    'content_type' => 'product',
                    'value' => (float)$product_price * $qty,
                    'currency' => $currency,
                    'quantity' => $qty,
                ];
            }
How to get the product data to populate correctly for $product_name, $product_price and $qty?
Omar_one
Senior Contributor
Posts: 677
Joined: Fri Oct 25, 2019 5:06 pm
Phoenix Version: v1.0.8.16
Has thanked: 100 times
Been thanked: 56 times

Re: Facebook Meta ads issue

Post by Omar_one »

maybe something like this ( add_product )

Code: Select all

// Facebook Pixel Event - Add to Cart
            if (defined('MODULE_HEADER_TAGS_META_PIXEL_STATUS') && MODULE_HEADER_TAGS_META_PIXEL_STATUS == 'True') {
                $product_name = \Product::fetch_name($pid);
                $product_price = \Product::fetch_price($pid);
                $value = (float)$product_price * $qty;

                $_SESSION['facebook_pixel_add_to_cart'] = [
                    'content_name' => $product_name,
                    'content_ids' => [$pid],
                    'content_type' => 'product',
                    'value' => $value,
                    'currency' => $_SESSION['currency'],
                    'quantity' => $qty
                ];
            }
this for buy_now

Code: Select all

  // Facebook Pixel Event - Buy Now
            if (defined('MODULE_HEADER_TAGS_META_PIXEL_STATUS') && MODULE_HEADER_TAGS_META_PIXEL_STATUS == 'True') {
                $product_name = \Product::fetch_name($pid);
                $product_price = \Product::fetch_price($pid);
                $value = (float)$product_price;

                $_SESSION['facebook_pixel_add_to_cart'] = [
                    'content_name' => $product_name,
                    'content_ids' => [$pid],
                    'content_type' => 'product',
                    'value' => $value,
                    'currency' => $_SESSION['currency'],
                    'quantity' => $qty
                ];
            }
User avatar
Pierre_P
Contributor
Posts: 144
Joined: Fri Mar 12, 2021 5:06 am
Phoenix Version: v1.1.0.6
Has thanked: 21 times
Been thanked: 11 times

Re: Facebook Meta ads issue

Post by Pierre_P »

@Omar_one
Thanks Omar
The Product::fetch_price class is no where declared
I am also maybe, maybe seeing issue with wholesale for my file includes/system/override/wholesale/product.php
The wholesale overides some classes.

I logged this values for

Code: Select all

error_log("Meta Pixel Debug (buy_now.php): instance of \Product for PID: " . $pid . " Name: " . $product_name . " Currency: " .  $_SESSION['currency'] . " Qty: " . $qty." ");
Meta Pixel Debug (buy_now.php): instance of \Product for PID: 3071 Name: M25 LTE Console Currency: ZAR Qty: 1
Omar_one
Senior Contributor
Posts: 677
Joined: Fri Oct 25, 2019 5:06 pm
Phoenix Version: v1.0.8.16
Has thanked: 100 times
Been thanked: 56 times

Re: Facebook Meta ads issue

Post by Omar_one »

we are still on version 1.0.8.16 and I installed there long time ago module called (Facebook Pixel) which Compatible with any osCommerce 2.3 version including 2.3.4BS Gold and Edge and it's created by @BrockleyJohn . and I have it working on version 1.0.8.16, I don't remember if I have been test it with Purchase Events on.. so @BrockleyJohn he will be the best who's will give advice ;)
BrockleyJohn
Certified Developer
Posts: 173
Joined: Mon Mar 01, 2021 5:37 pm
Phoenix Version:
Has thanked: 2 times
Been thanked: 30 times

Re: Facebook Meta ads issue

Post by BrockleyJohn »

I would be inclined to minimise duplication by recording only the product id and quantity of the event in the session data and then build the rest of what's needed in the header_tags module; you already have code there to build the pixel data for a product view event and this is similar.

Don't forget to remove the session data when you process it.
User avatar
Pierre_P
Contributor
Posts: 144
Joined: Fri Mar 12, 2021 5:06 am
Phoenix Version: v1.1.0.6
Has thanked: 21 times
Been thanked: 11 times

Re: Facebook Meta ads issue

Post by Pierre_P »

@Omar_one and @BrockleyJohn
I think i reach the end of the rabbit hole and found a princess telling me it goes futher..

When in the product_info.php pages this code below works correct with /actions/add_product.php.
My Meta Pixel helper shows the AddToCart event + 2 others i added after adding item to cart.

For index.php pages showing add to cart buttons for products it does not work, example i cannot get $product->get('name') from any index pages though $pid and qty =1 is passed through the session (buy_now.php button)

Extract of my header tag:

Code: Select all

        if (isset($_SESSION['facebook_pixel_add_to_cart']) && !empty($_SESSION['facebook_pixel_add_to_cart']['products_id'])) {
            $pid = (int)$_SESSION['facebook_pixel_add_to_cart']['products_id'];
            $qty = (int)$_SESSION['facebook_pixel_add_to_cart']['quantity'];

            if (isset($pid)) {
                $product_id = $pid;
                $product_name = $product->get('name');

                if ($product->get('is_special') == 1) {
                    $facebook_price = $product->format_raw('specials_new_products_price');
                } else {
                    $facebook_price = $product->format_raw();
                }
                $currency_code = $_SESSION['currency'];
                $category_name  = $category_tree->get($current_category_id, 'name');

                $pixel .= <<<EOD
<script>
  fbq('track', 'AddToCart', {
    content_name: '{$product_name}',
    content_category: '{$category_name}',
    content_ids: ['{$product_id}'],
    content_type: 'product',
    value: {$facebook_price},
    currency: '{$currency_code}',
    quantity: {$qty}
  });
 </script>
EOD;
            }
            unset($_SESSION['facebook_pixel_add_to_cart']);
        }
Do we need to manually do the additional sql for index pages or am i missing something?
BrockleyJohn
Certified Developer
Posts: 173
Joined: Mon Mar 01, 2021 5:37 pm
Phoenix Version:
Has thanked: 2 times
Been thanked: 30 times

Re: Facebook Meta ads issue

Post by BrockleyJohn »

Presumably $product is not set on those pages...

Code: Select all

$product = product_by_id::build($pid)
User avatar
Pierre_P
Contributor
Posts: 144
Joined: Fri Mar 12, 2021 5:06 am
Phoenix Version: v1.1.0.6
Has thanked: 21 times
Been thanked: 11 times

Re: Facebook Meta ads issue

Post by Pierre_P »

BrockleyJohn wrote: Mon Jul 14, 2025 5:44 pm Presumably $product is not set on those pages...

Code: Select all

$product = product_by_id::build($pid)
The index pages is example featured product cards or latest products showing on index.php page or where we have product cards in example index.php?cPath=2

All of them have Buy Now buttons on the cards.
It appears the global $product cannot be set for these pages if i am correct.


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply