Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Dynamic Product Ads Pixels (Add to cart, success) - Magento

I'm trying to set up Facebook Dynamic Product ads with Magento and I'm not sure exactly how to set up the add to cart and success pixels. I've set up the product page view pixel no problem.

On the cart page/ checkout what php code do I replace the following (bolded) with:

fbq('track', 'AddToCart', { content_name: 'Shopping Cart', content_ids: ['1234', '1853', '9386'], content_type: 'product', value: 3.50, currency: 'USD' });

And on the success page what php code do I replace the following (bold) with:

fbq('track', 'AddToCart', { content_name: 'Really Fast Running Shoes', content_category: 'Apparel & Accessories > Shoes', content_ids: ['1234'], content_type: 'product', value: 2.99, currency: 'USD'

Any help is much appreciated!

like image 815
Christian Sculthorp Avatar asked Dec 31 '25 21:12

Christian Sculthorp


1 Answers

For cart page you can use the following piece of code -:

<?php $quote = Mage::getSingleton('checkout/cart')->getQuote();
$productIds = "";
foreach($quote->getAllItems() as $item):
    if($item->getParentItemId()) continue;
    if (strlen($productIds)==0){
        $productIds = "'".$item->getSku()."'";
    }
    else{
        $productIds = $productIds.",'".$item->getSku()."'";
    }
endforeach;?>

<script>
fbq('track', 'AddToCart', {
     content_name: 'Shopping Cart',
     content_ids: [<?php echo $productIds?>],
     content_type: 'product',
     value: <?php echo number_format($quote->getGrandTotal(),2,'.','');?>,
     currency: '<?php echo Mage::app()->getStore()->getCurrentCurrencyCode();?>'
});
</script>

For purchase or order confirmation page you can use the following piece of code -:

<?php $orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
    $order = Mage::getModel('sales/order')->load($orderId);
$productIds = "";
foreach($order->getAllItems() as $item):
    if($item->getParentItemId()) continue;
    if (strlen($productIds)==0){
        $productIds = "'".$item->getSku()."'";
    }
    else{
        $productIds = $productIds.",'".$item->getSku()."'";
    }
endforeach;?>

<script>
fbq('track', 'Purchase', {
     content_name: 'Order Confirmation',
     content_ids: [<?php echo $productIds?>],
     content_type: 'product',
     value: <?php echo number_format($order->getGrandTotal(),2,'.','');?>,
     currency: '<?php echo Mage::app()->getStore()->getCurrentCurrencyCode();?>'
});
</script>
like image 146
stevensagaar Avatar answered Jan 02 '26 11:01

stevensagaar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!