Jump to content

[SOLVED] Order confirmation product IDs array


Recommended Posts

Hello guys

I need to output a comma separated array of the IDs of the products in the placed order, on the Order Confirmation Page, for Facebook Remarketing.

My current code in OrderConfirmationController.php is:

public function initContent()
	{
		parent::initContent();
		$order = new Order((int)($this->id_order)); $products = $order->getProducts();
		$product_ids = array();
		foreach ($products as $product) 
  		$product_ids[] = (int)$product['id_product'];										
			$this->context->smarty->assign(array(
			'$products' => $products,
			'id_order' => $this->id_order,
			'is_guest' => $this->context->customer->is_guest,
			'HOOK_ORDER_CONFIRMATION' => $this->displayOrderConfirmation(),
			'HOOK_PAYMENT_RETURN' => $this->displayPaymentReturn()
		));

		if ($this->context->customer->is_guest)
		{
			$this->context->smarty->assign(array(
				'id_order' => $this->id_order,
				'$products' => $products,
				'reference_order' => $this->reference,
				'id_order_formatted' => sprintf('#%06d', $this->id_order),
				'email' => $this->context->customer->email
			));
			/* If guest we clear the cookie for security reason */
			$this->context->customer->mylogout();
		}

and on the order-confirmation.tpl I got this:

	{literal}
			<script>
		       
		       fbq('track', 'Purchase',
// {/literal}
        // {foreach from=$products item=product name=item}
                 // {literal}
                         {
		       		content_ids: ['{/literal}{$product_ids}{literal}'],
		       		content_type: 'product',
		       		value: '{/literal}{$_value}{literal}',
		       		currency: 'RON'
		       	});
                 // {/literal}
        // {/foreach}
// {literal}

			</script>
			 
			<!-- End Facebook Pixel Code -->

	{/literal}

As you can see, I commented a {foreach} because I tried A LOT of suggestions here on the forum without good results and on the current setup I have the array generated in the PHP file.

The current output is none. Previous attempts have output either all the IDs united into one, without separation (which actually lead me to this endeavor) or just one product ID.

I am getting errors across all sites using this setup and it impacts my clients.

Please help? :) Thank you!

Edited by Attrexx
solved (see edit history)
Link to comment
Share on other sites

UPDATE:
Latest setup outputs ID's but not comma separated. They appear like 1-item individual arrays 😕 like "ID1""ID2" instead of, "ID1,ID2".

Also, HTML markup updated. Used same syntax for the "contents:..." bit, and here it outputs correctly...

PHP:
 

public function initContent()
	{
		parent::initContent();
		$order = new Order((int)($this->id_order)); $products = $order->getProducts();
		$product_ids = array();
		foreach ($products as $product) 
  		$product_ids[] = (int)$product['id_product'];										
			$this->context->smarty->assign(array(
			'$products' => $products,
			'id_order' => $this->id_order,
			'is_guest' => $this->context->customer->is_guest,
			'HOOK_ORDER_CONFIRMATION' => $this->displayOrderConfirmation(),
			'HOOK_PAYMENT_RETURN' => $this->displayPaymentReturn()
		));
			$array = explode(', ',$product_ids);
			
		if ($this->context->customer->is_guest)
		{
			$this->context->smarty->assign(array(
				'id_order' => $this->id_order,
				'$products' => $products,
				'reference_order' => $this->reference,
				'id_order_formatted' => sprintf('#%06d', $this->id_order),
				'email' => $this->context->customer->email
			));
			/* If guest we clear the cookie for security reason */
			$this->context->customer->mylogout();
		}
...

HTML:

{literal}
			<script>
		       
		       fbq('track', 'Purchase', {
		       	content_ids: {/literal}{foreach from=$products item="product"}{literal}
						       	{/literal}'{$product_ids}'{literal}
						       	{/literal}{/foreach}{literal}, 
		       	content_type: 'product', 
		       	value: {/literal}'{$_value}'{literal}, 
		       	currency: 'RON',
		       	contents: [
			        {/literal}{foreach from=$products item="product"}{literal}
			        {
			            id: {/literal}'{$product.product_id}'{literal},
			            quantity: {/literal}'{$product.product_quantity}'{literal},
			            item_price: {/literal}'{$product.total_wt}'{literal}
			        }
			        {/literal}{/foreach}{literal}
			    ],
		       });
			</script>

 

Edited by Attrexx (see edit history)
Link to comment
Share on other sites

UPDATE:

In the meantime a colleague came up with a solution and has agreed for it to be shared with anyone who would face the same issue.

Below are the modifications he made to the controller and the template files. Needless to say, they work flawlessly now.

OrderConfirmationController.php new code:

$cart = new Cart((int)($this->id_cart));
		    self::$smarty->assign(array(
		        '_order' => $this->id_order,
		        '_value' => $cart->getOrderTotal()
		    ));

		    $order_id = new Order($this->id_order);
			$products = $order_id->getProducts();
			$product_ids = [];
		    foreach ($products as $product) {
  		        $product_ids[] = (int)$product['id_product'];
		    }
		    $this->context->smarty->assign(array(
			    'order_id' => $this->id_order,
			    'products' => $products,
			    'product_ids' => json_encode($product_ids),
			));

and order-confirmation.tpl new code:

<script>
		  fbq('track', 'Purchase', {
		    value: {/literal}'{$_value}'{literal},
		    currency: {/literal}'{$currency->iso_code}'{literal} ,
		    content_ids: {/literal} {$product_ids} {literal},
		    content_type: 'product',
		  });
		</script>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...