Magento 1.9: Print variable value in Mage::Log
-
11-03-2021 - |
Question
My observer :
public function checkproductinorder($Observer)
{
$order = $Observer->getEvent()->getOrder();
$items = $order->getAllItems();
foreach ($items as $item)
{
if($item->getSku() == '600' || '800')
{
Mage::log('The selected product list available in order', null, 'matchorder.log');
}
}
If the SKU's 600 or 800 order how can I print the SKU value in Mage::Log like,
The selected product list available in order. SKU list : 600, 800
Solution
Try below code in your observer, it'll work for you.
public function checkproductinorder($Observer)
{
$order = $Observer->getEvent()->getOrder();
$items = $order->getAllItems();
$skus = array('600','800'); //product SKUs
$skuList = array();
foreach ($items as $item)
{
if(in_array($item->getSku(), $skus))
{
$skuList[] = $item->getSku();
}
}
if(!empty($skuList)) {
Mage::log('The selected product list available in order '.implode(',',$skuList), null, 'matchorder.log');
}
}
OTHER TIPS
Try this code
public function checkproductinorder($Observer)
{
$order = $Observer->getEvent()->getOrder();
$items = $order->getAllItems();
$skus = array();
foreach ($items as $item)
{
if($item->getSku() == '600' || $item->getSku() == '800')
{
$skus[] = $item->getSku();
}
}
Mage::log('The selected product list available in order. SKU list: '.implode(",",$skus), null, 'matchorder.log');
}
It will print the sku list in comma separated way in log as you mentioned in your question. I hope this will help
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange