Christian Sculthorp
Christian Sculthorp

Reputation: 55

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!

Upvotes: 3

Views: 3894

Answers (1)

stevensagaar
stevensagaar

Reputation: 58

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>

Upvotes: 4

Related Questions