Saturday 18 June 2011

Disabling Magento Extension Manually

Some time we need to disable extension manually, its easy to do as as every magento extension create its XML file in 'app\etc\modules' folder like our extension name is 'AdvLay_Nav' so it will create file named AdvLay_Nav.xml when u open it you will see code like this

<?xml version="1.0"?>
<config>
    <modules>
        <AdvLay_Nav>
            <active>true</active>
            <codePool>local</codePool>
            <self_name>Advance Layed Navigation</self_name>
            <priority>10120</priority>
        </AdvLay_Nav>
    </modules>
</config>

go to <active> tag edit it to 'false' and clear your magento cache in var folder and you are done.

Friday 17 June 2011

Get Current Category In Magento

<?php Mage::registry('current_category')->getName();?>

Running script out side Magento Folder and Updating Project Database

Place this script 'image.php' out side magento folder and run this with http://yoursite/image.php, this will update all product images if there is no small or thumbnail image.
 It will make first image as thumb and small image.

An example to run script out side magento folder.

<?php
require 'app/Mage.php';
Mage::app();

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
foreach ($products as $product) {
 if (!$product->hasImage()) continue;
 if (!$product->hasSmallImage()) $product->setSmallImage($product->getImage());
 if (!$product->hasThumbnail()) $product->setThumbnail($product->getImage());
 $product->save();
}

Creating Product Maually in Magento

<?php

//$product = Mage::getModel('catalog/product');
$product = new Mage_Catalog_Model_Product();

// Build the product
$product->setSku('sku');
$product->setAttributeSetId('Attribute_id');
$product->setTypeId('simple');
$product->setName('product name');
$product->setCategoryIds(array(9)); # category id's
$product->setWebsiteIDs(array(1)); # Website id
$product->setDescription('Long description');
$product->setShortDescription('Short description');
$product->setPrice(19.99); # producte price   

# Custom created and assigned attributes
$product->setHeight('custom_attribute1_val');
$product->setWidth('custom_attribute2_val');
$product->setDepth('custom_attribute3_val');
$product->setType('custom_attribute4_val');

//Default Magento attribute
$product->setWeight(2.0000);

$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(1);
$product->setTaxClassId(0); # My default tax class
$product->setStockData(array(
    'is_in_stock' => 1,
    'qty' => 20
));

$product->setCreatedAt(strtotime('now'));

try {
    $product->save();
}
catch (Exception $ex) { 
    //Handle the error
}

?>

How to Edit Thank you message for Contact page in Magento

Some time we need to change message on magento contact success, we can do so by following steps go to
'app\code\core\Mage\Contacts\controllers'

search for 'Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));'

or go to line no 112. and change it to according to your requirment...

Limit Number of Images Per Product In Magento

Some time we need to limit number of images for a product in our magento project we can do it
easily with some lines of code. Go to 'app\code\core\Mage\Adminhtml\controllers\Catalog\ProductController.php' and in saveAction find $data = $this->getRequest()->getPost(); under it put these line. and you are done.

$images = Mage::helper('core')->jsonDecode($data['product']['media_gallery']['images']);
       
        if($totalImages = count($images)) {

        $maxPhotoPerProduct = 3;

        if($totalImages > $maxPhotoPerProduct) {
            Mage::getSingleton('core/session')->addError($this->__('Max. number of images per product reached, extra images have been removed'));
        }

        $_allowedImages = array_slice($images, 0, $maxPhotoPerProduct);
        $_POST['product']['media_gallery']['images'] = Mage::helper('core')->jsonEncode($_allowedImages);
    }

Thursday 16 June 2011

Display Static .phtml page from cms backend

First u need to create .phtml file in the folder like 
in example 'catalog/product' then go to magento backend 
and create CMS page set title set layout then in 
content paste the following code "replace your .phtml file path" .
 
{{block type="catalog/product_featured" name="product_featured"
 as="product_featured" template="catalog/product/featured.phtml"}}

Display Image in Magento From Skin Folder

<img src="<?php echo $this->getSkinUrl('images/sitelogo.jpg');?>" 
alt="sitename" />

Magento Check if Customer Loged in or Not

<?php
$_customer = Mage::getSingleton('customer/session')->isLoggedIn();

if ($_customer) {
// do stuff
}
?>

Remove SID from Magento URL

Open file "app\code\core\Mage\Core\Model\Session\Abstract.php"
find this code 'protected $_skipSessionIdFlag = true;'
replace it with this 'protected $_skipSessionIdFlag = false,'

Get The productID On The Frontend In Magento

<?php echo $_helper->productAttribute($_product$_product->getId(), \'id\'?>

Wednesday 15 June 2011

Displaying Currency in magento with php number_format()

<?php echo number_format($_product->getFinalPrice(),2);?>

Get productID from specific order in Magento

$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{
    $name[] = $item->getName();
    $unitPrice[]=$item->getPrice();
    $sku[]=$item->getSku();
    $ids[]=$item->getProductId();
    $qty[]=$item->getQtyToInvoice();
}

base url in magento

we can get the base url of magento site with the code below, 
just place this code anywhere in .phtml file and you will 
get the base url of your magento site.
<?php echo Mage::getBaseUrl(); ?>