Success.phtml - how to trigger affillate script based on product-ID ordered by customer?
-
13-12-2019 - |
Question
First of all - I'm not a developer..I´ve tried google, but can't seem to find out how to do this..
I need to add a function to my success.phtml that triggers a spesific tracking script that I got from my affilate provider. I have two different tracking scripts, where the first script should be triggered if a customer purchased a product where the product ID matches one these ID's "19, 76, 84, 85, 86, 95" , and another script that should be triggered if the customer don't buy any of the mentioned products..
-- I've tried to add this to my success.phtml, but this obviously dosen't work. Could someone be an angel and help me sort this out?
<?php
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$items = $order->getItemsCollection();
$sku = $ids = array();
foreach($items as $item){
$p_ids[] = $item->getProductId();
}
$p_id = 19, 76, 84, 85, 86, 95;
?>
<?php if(in_array($p_id, $p_ids)){
//run script 1
<script type="text/javascript" src="https://track.adtraction.com/t/t?t=1********************************************************"></script>
} else {
//run script 2
<script type="text/javascript" src="https://track.adtraction.com/t/t?2********************************************************"></script>
}
<?php endif;?>
Solution
Try
<?php
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$items = $order->getItemsCollection();
$found = false;
$affiliateProductIds = array(19, 76, 84, 85, 86, 95);
foreach($items as $item){
if(in_array($item->getProductId(), $affiliateProductIds)){
//if you need to send the product ids to the affiliate, they use an array instead of bool ($found)
$found = true;
}
}
?>
<?php if($found) : ?>
<script type="text/javascript" src="https://track.adtraction.com/t/t?t=1********************************************************"></script>
<?php else : ?>
<script type="text/javascript" src="https://track.adtraction.com/t/t?2********************************************************"></script>
<?php endif;?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange