Wednesday, 5 March 2014

Magento Get products id, name, price, quantity, etc. present in your cart.

<?php
    $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
   
    foreach($items as $item) {
        echo 'ID: '.$item->getProductId().'<br />';
        echo 'Name: '.$item->getName().'<br />';
        echo 'Sku: '.$item->getSku().'<br />';
        echo 'Quantity: '.$item->getQty().'<br />';
        echo 'Price: '.$item->getPrice().'<br />';
        echo "<br />";
    }
?>

Magento Load Products by Category ID

<?php
$_category = Mage::getModel('catalog/category')->load(47);
$_productCollection = $_category->getProductCollection();
if($_productCollection->count()) {
    foreach( $_productCollection as $_product ):
        echo $_product->getProductUrl();
        echo $this->getPriceHtml($_product, true);
        echo $this->htmlEscape($_product->getName());
    endforeach;
}
?>

Magento Get Current Url

<?php echo Mage::helper('core/url')->getCurrentUrl(); ?>

Tuesday, 18 February 2014

Magento Add wysiwyg editor in custom module.

Step-1

Go to following path and open Edit.php
app\code\local\Namespace\Modulename\Block\Adminhtml\Modulename\Edit.php
and Add this Function


protected function _prepareLayout()
{
    // Load Wysiwyg on demand and Prepare layout
    if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled() && ($block = $this->getLayout()->getBlock('head'))) {
    $block->setCanLoadTinyMce(true);
    }
    parent::_prepareLayout();

}

Step-2

Go to following path and open Form.php
app\code\local\Namespace\Modulename\Block\Adminhtml\Modulename\Edit\Tab\Form.php
and find function protected function_prepareForm() and add


$form = new Varien_Data_Form();
$this->setForm($form);

$form->setHtmlIdPrefix('yourmodulename');
$wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config')->getConfig(array('tab_id' => 'form_section'));
$wysiwygConfig["files_browser_window_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg_images/index');
$wysiwygConfig["directives_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive');
$wysiwygConfig["directives_url_quoted"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive');
$wysiwygConfig["widget_window_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/widget/index');
$wysiwygConfig["files_browser_window_width"] = (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_width');
$wysiwygConfig["files_browser_window_height"] = (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_height');
$plugins = $wysiwygConfig->getData("plugins");
$plugins[0]["options"]["url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/system_variable/wysiwygPlugin');
$plugins[0]["options"]["onclick"]["subject"] = "MagentovariablePlugin.loadChooser('".Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/system_variable/wysiwygPlugin')."', '{{html_id}}');";
$plugins = $wysiwygConfig->setData("plugins",$plugins);

and add this code to display wysiwyg editor

$fieldset->addField('content', 'editor', array(
'name' => 'content',
'label' => Mage::helper('news')->__('Content'),
'title' => Mage::helper('news')->__('Content'),
'style' => 'width:700px; height:500px;',
'wysiwyg' => true,
'required' => true,
'state' => 'html',
'config' => $wysiwygConfig,
));

Step-3

Go to following path and open yourmodulename.xml if its not available than create xml file
app\design\adminhtml\default\default\layout\yourmodulename.xml
and add

<?xml version="1.0"?>
<layout>
  <news_adminhtml_news_edit>
    <update handle="editor"/>
  </news_adminhtml_news_edit>
</layout>

Tuesday, 4 February 2014

Magento Add Custom Breadcrumbs In custom Module.

Add following code in you modules block file.

public function _prepareLayout()
{
    $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
    $breadcrumbs->addCrumb('home', array('label'=>Mage::helper('cms')->__('Home'), 'title'=>Mage::helper('cms')->__('Home Page'), 'link'=>Mage::getBaseUrl()));
    $breadcrumbs->addCrumb('an_alias', array('label'=>'Your label', 'title'=>'Your title', 'link'=>Mage::getUrl("your_module/your_block")));

    return parent::_prepareLayout();
}

Friday, 31 January 2014

Magento Disable Cache For Particular Block Useing XML.

In my case i have disable cache for footer.

<block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml">

    <action method="unsetData"><key>cache_lifetime</key></action>
    <action method="unsetData"><key>cache_tags</key></action>
               
</block>

Tuesday, 7 January 2014

Magento Add Rel Canonical Url

To apply simply click on System > Configuration > Catalog > Search Engine Optimizations Section

Then simply change from no to yes and then click on the “Save Config” button in the top right hand corner.