I want to add Commission junction to my client site, in that they asked for each product sku\'s and price. After the confirmation page/ success page only we need to pass the
Watch for an event like this:
<config>
<global>
<events>
<sales_order_place_after>
<observers>
<yourmodule_order_place_after>
<class>yourmodule/observer</class>
<method>onSalesOrderPlaceAfter</method>
</yourmodule_order_place_after>
</observers>
</sales_order_place_after>
</events>
</global>
</config>
Next, you need something to handle the event.
app/code/local/Yourcompany/Yourmodule/Model/Observer.php
<?php
class Yourcompany_Yourmodule_Model_Observer {
public function onSalesOrderPlaceAfter($observer) {
$order = $observer->getOrder();
/* @var $item Mage_Sales_Model_Order_Item */
foreach ($order->getItemsCollection() as $item) {
// Do something with $item here...
$name = $item->getName();
$price = $item->getPrice();
$sku = $item->getSku();
}
}
}
See the database table "sales_flat_order_item" or do a var_dump($item->debug())
to see what sort of values are available. As it's a flat table the only way to find more information about a product is like this:
$product = Mage:getModel('catalog/product')->load($item->getProductId());
$product->getDescription();
Yeah you are right @leek
But if you want to add advanced setup with CJ then follow this method.
<!-- Start of CJ Integration Part -->
<?php
$_customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$_totalData =$order->getData();
$_sub = $_totalData['subtotal'];//USD ==> global_currency_code,base_currency_code order_currency_code
// Incase if it is simple do this ==> https://www.emjcd.com/u?AMOUNT= $_sub;
//print_r($order); print_r($_totalData);
$_order = $this->getOrder();
$allitems = $order->getAllItems();
$index = 1;
$cjData = "";//Needed format ==> &ITEM1=3214sku&AMT1=13.49&QTY1=1&ITEM2=6577sku&AMT2=7.99&QTY2=2&
foreach($allitems as $item)
{
$cjData.="&ITEM".$index."=".$item->getSku()."&AMT".$index."=".$item->getPrice()."&QTY".$index."=".$item->getQtyToShip();
$index++;
}
?>
<div style="display:none;">
<img src="https://www.emjcd.com/u?CID=id&OID=<?php echo $this->getOrderId(); ?>&TYPE=type<?php echo $cjData; ?>&CURRENCY=USD&METHOD=IMG" height="1" width="20">
</div>
<!-- End of CJ Integration Part -->
Its worked perfectly.
Note: This is NOT the best method! You should be creating a new block/template for this and pass the data via another means. You should also not ever instantiate a Db connection in the view/template.
With that said - here is how I implemented Commission Junction on a Magento store long before I learned how to override modules/templates/etc.
app\design\frontend\enterprise\default\template\checkout\success.phtml
<?php
$orderId = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$orderTotal = $order->subtotal;
$orderEntityId = $order->entity_id;
$db = Mage::getModel('Core/Mysql4_Config')->getReadConnection();
// Retrieve ordered products
$sql = sprintf("
SELECT *
FROM `sales_flat_order_item`
WHERE (order_id = %d);",
$orderEntityId
);
$orderedProducts = $db->fetchAll($sql);
// Loop through each product in order
foreach ($orderedProducts as $orderedProduct) {
$productId = (int) $orderedProduct['product_id'];
$quantity = (int) $orderedProduct['qty_ordered'];
}
?>
<!-- Commission Junction -->
<img src="https://www.emjcd.com/u?AMOUNT=<?php echo $orderTotal; ?>&CID=<INSERT_CID_HERE>&OID=<?php echo $orderId; ?>&TYPE=339032&CURRENCY=USD&METHOD=IMG" height="1" width="20">
<!-- Commission Junction -->