You are going to need to either use a module or create one of your own for this:
If you choose to create a module, after you set up the base module. You can add checkout_onepage_success.xml
file under \CompanyName\ModuleName\view\frontend\layout
with the following content:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="order.success.additional.info">
<block class="CompanyName\ModuleName\Block\Checkout\Success\Facebookpixel" name="modulename.checkout.success.facebookpixel" template="checkout/success/facebookpixel.phtml"
after="-"/>
</referenceBlock>
</body>
</page>
Then add facebookpixel.phtml
under \CompanyName\ModuleName\view\frontend\templates\checkout
:
<?php
$order = $this->getOrder();
$pixelValue = number_format($order->getBaseGrandTotal(),2);
$pixelCurrency = $order->getOrderCurrencyCode();
<script>
fbq('track', 'Purchase', {
value: <?php echo $pixelValue;?>,
currency: '<?php echo $pixelCurrency;?>'
});
</script>
Then add success.php file under \CompanyName\ModuleName\Block\Checkout\Success
:
<?php
// @codingStandardsIgnoreFile
namespace CompanyName\ModuleName\Block\Checkout\Success;
/**
* FB Page Block
*/
class Facebookpixel extends \Magento\Framework\View\Element\Template
{
/** @var \Magento\Sales\Model\OrderFactory */
protected $_salesFactory;
/** @var \Magento\Checkout\Model\Session */
protected $_checkoutSession;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Sales\Model\OrderFactory $salesOrderFactory
* @param \Magento\Checkout\Model\Session $checkoutSession
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Sales\Model\Order $salesOrderFactory,
\Magento\Checkout\Model\Session $checkoutSession,
array $data = []
) {
$this->_salesFactory = $salesOrderFactory;
$this->_checkoutSession = $checkoutSession;
parent::__construct($context, $data);
}
/**
* Retrieve current order
*
* @return \Magento\Sales\Model\Order\OrderFactory
*/
public function getOrder()
{
$orderId = $this->_checkoutSession->getLastOrderId();
return $this->_salesFactory->load($orderId);
}
}