Search This Blog

Tuesday, 29 January 2013

Auto Pre-pend a file when any file is accessed from the folder

Usually it helps to authenticate the user for the static website in a sub folder. You can include the check.php file on the server and write logic for authentication.

php_value auto_prepend_file "DOCUMENT_ROOT/check.php"

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

Monday, 28 January 2013

Get and Set Config data

$lastUpdatedDate = Mage::getStoreConfig('test/newarrivals/updateddate');

Mage::getModel('core/config')->saveConfig('test/newarrivals/updateddate', $lastUpdatedDate );

Wednesday, 23 January 2013

Log all SQL queries in Magento

In Varien_Db_Adapter_Pdo_Mysql

Magento : lib/varien/Db/Adapter/Pdo/Mysql.php
set
protected $_debug               = true;
protected $_logAllQueries       = true;
 
and (if nor already there) create the folder defined in

protected $_debugFile           = 'var/debug/sql.txt';
 
Give read / write permission

Script to Unzip the GZipped file in PHP

function uncompress($srcName, $dstName) {
    $sfp = gzopen($srcName, "rb");
    $fp = fopen($dstName, "w");

    while ($string = gzread($sfp, 4096)) {
        fwrite($fp, $string, strlen($string));
    }
    gzclose($sfp);
    fclose($fp);
}
uncompress($srcName, $dstName);

Include a block from a module in any PHTML file

<?php 
echo $this->getLayout()->createBlock('newmodule/newblock')
->setTemplate('newmodule/newblock.phtml')->toHtml();
?>

Tuesday, 22 January 2013

Save customer address programatically

                ### Save Address Info for the Customer ###
                $customerData = $this->getRequest()->getPost();
                $address = Mage::getModel("customer/address");
                if($customer->getDefaultBilling())
                {
                    $address->load($customer->getDefaultBilling()); // Updating the default billing address if customer already have
                }
                $address->setCustomerId($customer->getId());
                $address->setData($customerData);
                $address->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false)); // Make it as default billing address
                $address->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false)); // Make it as default shipping address
                $address->save();
                $customer->default_billing = $address->getId();  // Make it as default billing address
                $customer->default_shipping = $address->getId(); // Make it as default shipping address
                $customer->save();
                ### Save Address Info for the Customer ###