Search This Blog

Tuesday, 29 January 2013

Display Country warehouse flag using Multi warehouse extension

Script to display contry flags for the available warehouses using Multi warehouse extension.


$countryStocks = array(); $productId = array(9736, 9737, 9738);
$collection = Mage::getModel('warehouse/warehouse')->getCollection();
if($collection->count() > 0)
{
    $innoStockObj = new Innoexts_Warehouse_Model_Cataloginventory_Stock_Item_Api();
    foreach($collection as $warehouse)
    {
        if($warehouse->getId())
        {
            $stockId = $warehouse->getStockId();
            $countryId = strtolower($warehouse->getOriginCountryId());
            $result = $innoStockObj->itemsByStock($productId, $stockId);
            foreach($result as $resultRow)
            {
                if(isset($resultRow['qty']) and $resultRow['qty'] > 0 and isset($resultRow['is_in_stock']) and $resultRow['is_in_stock'] == 1)
                {
                    $countryStocks[$resultRow['product_id']][$countryId]['description'] = '';
                    if(isset($countryStocks[$countryId]))
                        $countryStocks[$resultRow['product_id']][$countryId] = array('qty' => $countryStocks[$countryId]['qty'] + $resultRow['qty'], 'description' => $warehouse->getDescription());
                    else
                        $countryStocks[$resultRow['product_id']][$countryId] = array('qty' => $resultRow['qty'], 'description' => $warehouse->getDescription());
                    $filename = Mage::getBaseDir('media').DS.'flags'.DS.$countryId.".png";
                    if(file_exists($filename))
                    {
                        $countryStocks[$resultRow['product_id']][$countryId]['countryFlag'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'flags/'.$countryId.".png";
                    }
                }
            }
        }   
    }
}
//print_r($countryStocks);
?>
<?php $productId = 9736; ?>
<?php if(count($countryStocks) > 0 and count($countryStocks[$productId]) > 0): ?>
<?php foreach($countryStocks[$productId] as $countryStock): ?>
    <?php if(isset($countryStock['countryFlag'])): ?>
        <img src="<?php echo $countryStock['countryFlag']; ?>" title="<?php echo $countryStock['description']; ?>" alt="<?php echo $countryStock['description']; ?>" />
    <?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>

Make sure to place country flag images in /media/flags folder with naming country code.png

No comments:

Post a Comment