Create Global Function In Magento

This code will allow you to add a function that can be called from anywhere within Magento. It extends the helper class

1) Create a file named ‘Mycode.xml’ and copy it to app/etc/modules/ – it should look like this:

<?xml version="1.0"?>
<config>
    <modules>
		<Mycode_Function>
			<active>true</active>
			<codePool>local</codePool>
		</Mycode_Function>
    </modules>
</config>

2) Create the directory
app/code/local/Mycode/Function/etc
and then create a file named ‘config.xml’
In it copy:

<?xml version="1.0"?>
<config>
 
    <modules>
        <Mycode_Function>
            <version>1.0.0</version>
        </Mycode_Function>
    </modules>
 
    <global>
 
        <helpers>
            <function>
                <class>Mycode_Function_Helper</class>
            </function>
        </helpers> 
 
    </global>
 
</config>

3) Create the directory
app/code/local/Mycode/Function/Helper
and then create a file named ‘Data.php’
In it copy:

<?php
 
class Mycode_Function_Helper_Data extends Mage_Core_Helper_Abstract
{
 
	public function test(){
 
	return 'works';
 
	}
 
}

You can now call this function like so

		<?php 
 
		 echo Mage::helper('function')->test();
 
		 ?>

Magento Set, Retrieve and Unset Session Variables

To set a Magento session variable:

$myValue = 'Hello World';
Mage::getSingleton('core/session')->setMyValue($myValue);

To Retrieve:

$myValue = '';
$myValue=Mage::getSingleton('core/session')->getMyValue();

To Unset:

Mage::getSingleton('core/session')->unsMyValue();

Note that ‘MyValue’ can be any text you want but ‘set’, ‘get’ and ‘uns’ prefixes are required.

Add Linkshare Pixel To Magento

Add this code to:
\app\design\frontend\enterprise\<your theme>\template\checkout\success.phtml

<?php
 
$order_id = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getModel('sales/order')->load($order_id);
$_totalData = $order->getData();
 
/*linkshare vars*/
$skus = array();
$qtys = array();
$amounts = array();
 
foreach ($order->getAllItems() as $item) {
 
        $skus[$item->getProductId()] = $item->getSku();
	$names[$item->getProductId()] = rawurlencode($item->getName());
	$qtys[$item->getProductId()] = $item->getQtyOrdered() * 1;
	$amounts[$item->getProductId()] = $item->getRowTotal() * 100;	
}
 
$order_id = $_totalData['increment_id'];
 
$skuspipe = implode("|", $skus);
$namespipe = implode("|", $names);
$qtyspipe = implode("|", $qtys);
$amountspipe = implode("|", $amounts);
?>
<img src="https://track.linksynergy.com/ep?mid=xxxx&ord=<?php echo $order_id ?>
&skulist=<?php echo $skuspipe ?>&qlist=<?php echo $qtyspipe ?>&amtlist=
<?php echo $amountspipe ?>&cur=USD&namelist=<?php echo $namespipe ?>">

Make sure to replace ‘xxxx’ with your own Merchant Id number

Code modified/corrected from this post .