Friday 4 October 2013

Display Categories and SubCategories in Magento

Put This code in phtml file.
 

1) Display Top Level Categories Only In Magento


<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

2) Display Top Level Categories and ALL Subcategories In Magento


<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php if (count($_subcategories) > 0): ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

3) Display Top Level Categories and Current Categories SubCategories In Magento


<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
                    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                    <?php $_subcategories = $_category->getChildrenCategories() ?>
                    <?php if (count($_subcategories) > 0): ?>
                        <ul>
                            <?php foreach($_subcategories as $_subcategory): ?>
                                <li>
                                    <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                        <?php echo $_subcategory->getName() ?>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Code For Add Custom Field In From In Magento

Put this code in your module form.php file

1) Text Field


    $fieldset->addField('title', 'text', array(
    'label'     => $this->__('Title'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'title',
    'value'     => 'This is text...',
    'disabled'  => false,
    'readonly'  => false,
    'onclick'   => "alert('On Click');",
    'onchange'  => "alert('On Change');",
    'style'     => "border:5px",
    'after_element_html' => '<small>Comment<small>',
    'tabindex'  => 1
));
       

2) Text Area


    $fieldset->addField('textarea', 'textarea', array(
    'label'     => $this->__('TextArea'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'title',
    'value'     => '',
    'disabled'  => false,
    'readonly'  => false,
    'onclick'   => "",
    'onchange'  => "",
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 1
));

3) Password


     $fieldset->addField('password', 'password', array(
    'label'     => $this->__('Password'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'title',
    'disabled'  => false,
    'readonly'  => false,
    'onclick'   => "",
    'onchange'  => "",
    'style'     => "",
    'value'     => 'mojojojo',
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 1
));

4) Obscure Password


    $fieldset->addField('obscure', 'obscure', array(
    'label'     => $this->__('Obscure'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'obscure',
    'value'     => '04021993',
    'onclick'   => "",
    'onchange'  => "",
    'style'     => "",
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 1
));

5) Radio Button


    $fieldset->addField('radio', 'radio', array(
    'label'     => $this->__('Radio'),
    'name'      => 'title',
    'value'     => '1',
    'disabled'  => false,
    'readonly'  => false,
    'onclick'   => "",
    'onchange'  => "",        
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 1
));

    $fieldset->addField('radio2', 'radios', array(
    'label'     => $this->__('Radios'),
    'name'      => 'title',
    'values'    => array(
                      array('value'=>'1','label'=>'Radio1'),
                      array('value'=>'2','label'=>'Radio2'),
                      array('value'=>'3','label'=>'Radio3'),
                 ),
    'disabled'  => false,
    'readonly'  => false,
    'onclick'   => "",
    'onchange'  => "",
    'value'     => '2',
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 1
));

6) Checkbox


    $fieldset->addField('checkbox', 'checkbox', array(
    'label'     => $this->__('Checkbox'),
    'name'      => 'Checkbox',
    'checked'   => false,
    'value'     => '1',
    'disabled'  => false,
    'onclick'   => "",
    'onchange'  => "",
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 3
));

    $fieldset->addField('checkboxes', 'checkboxes', array(
    'label'     => $this->__('Checkboxes'),
    'name'      => 'Checkbox',
    'value'     => '2',
    'values'    => array(
                      array('value'=>'1','label'=>'Checkbox1'),
                      array('value'=>'2','label'=>'Checkbox2'),
                      array('value'=>'3','label'=>'Checkbox3'),
                 ),
    'onclick'   => "",
    'onchange'  => "",
    'disabled'  => false,
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 4
));

7) Dropdown


    $fieldset->addField('select', 'select', array(
    'label'     => $this->__('Select'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'title',
    'value'     => '1',
    'values'    => array('-1'=>'Please Select','1' => 'Option1', '2' => 'Option2', '3' => 'Option3'),
    'disabled'  => false,
    'readonly'  => false,
    'onclick'   => "",
    'onchange'  => "",
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 1
));

8) Dropdown v2


    $fieldset->addField('select2', 'select', array(
    'label'     => $this->__('Select v2'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'title',
    'disabled'  => false,
    'readonly'  => false,
    'onclick'   => "",
    'onchange'  => "",
    'value'     => '4',
    'values'    => array(
                          '-1'=>'Please Select..',
                          '1' => array(
                                   'value'=> array(array('value'=>'2' , 'label' => 'Option2') , array('value'=>'3' , 'label' =>'Option3') ),
                                   'label' => 'Size'  
                                 ),
                          '2' => array(
                                   'value'=> array(array('value'=>'4' , 'label' => 'Option4') , array('value'=>'5' , 'label' =>'Option5') ),
                                   'label' => 'Color' 
                                 ),                                        
                     ),
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 1
));

9) Multiselect


    $fieldset->addField('multiselect2', 'multiselect', array(
    'label'     => $this->__('Select Type2'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'title',
    'onclick'   => "return false;",
    'onchange'  => "return false;",
    'disabled'  => false,
    'readonly'  => false,
    'value'     => '3',
    'values'    => array(
                          '-1'=> array( 'label' => 'Please Select..', 'value' => '-1'),
                          '1' => array(
                                   'value'=> array(array('value'=>'2' , 'label' => 'Option2') , array('value'=>'3' , 'label' =>'Option3') ),
                                   'label' => 'Color'  
                                 ),
                          '2' => array(
                                   'value'=> array(array('value'=>'4' , 'label' => 'Option4') , array('value'=>'5' , 'label' =>'Option5') ),
                                   'label' => 'Size' 
                                 ),                                        
                     ),
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 10
));

10) Multiline


    $fieldset->addField('multiline', 'multiline', array(
    'label'     => $this->__('Multi Line'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'title',
    'value'     => 'Hello galaxy!',
    'disabled'  => false,
    'readonly'  => false,
    'onclick'   => "",
    'onchange'  => "",
    'style'     => "border:5px",
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 1
));

11) Label


    $fieldset->addField('label', 'label', array(
    'value'     => $this->__('Label Text'),
));

12) Link


    $fieldset->addField('link', 'link', array(
    'label'     => $this->__('Link'),
    'style'     => "",
    'href'      => 'http://www.brymayor.com/',
    'value'     => 'My Blog',
    'after_element_html' => '<small>Comment</small>',
));

13) Note


    $fieldset->addField('note', 'note', array(
    'text'     => $this->__('Hello galaxy!'),
));

14) Submit Button


    $fieldset->addField('submit', 'submit', array(
    'label'     => $this->__('Submit'),
    'required'  => true,
    'value'     => 'Submit',
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 15
));

15) Time


    $fieldset->addField('time', 'time', array(
    'label'     => $this->__('Time Title'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'title',
    'value'     => '04,00,00',
    'onclick'   => "",
    'disabled'  => false,
    'readonly'  => false,
    'onchange'  => "",
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 3
));

16) File Upload


    $fieldset->addField('file', 'file', array(
    'label'     => $this->__('Uploader'),
    'value'     => 'Uploader',
    'disabled'  => false,
    'readonly'  => false,
    'after_element_html' => '<small>Comment</small>',
    'tabindex'  => 5
));

17) Image Upload


    $fieldset->addField('image', 'image', array(
    'value'     => 'http://brymayor.com/wp-content/uploads/2013/02/default-logo.png',
));

18) Date


    $fieldset->addField('date', 'date', array(
    'label'     => $this->__('Date'),
    'after_element_html' => '<small>Comment</small>',
    'image'     => $this->getSkinUrl('images/grid-cal.gif'),
    'format'    => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM),
    'tabindex'  => 2
));

Get Current Module Name, Route Name, Controller Name And Action Name In Magento

Mage::app()->getRequest()->getControllerName(); // return controller name

Mage::app()->getRequest()->getActionName(); // return action name

Mage::app()->getRequest()->getRouteName(); // return routes name

Mage::app()->getRequest()->getModuleName(); // return module name

Thursday 3 October 2013

Add Back To Top Button in Magento

Step-1 Add this Div in Footer.phtml


<div id="back_top" style="display: block;"></div>

Step-2 Add this Java script in footer.phtml


<script type="text/javascript">
    jQuery(function() {
        jQuery(window).scroll(function() {
        if(jQuery(this).scrollTop() > 300) {
            jQuery('#back_top').fadeIn();   
        } else {
            jQuery('#back_top').fadeOut();
        }
    });
       jQuery('#back_top').click(function() {
        jQuery('body,html').animate({scrollTop:0},500);
    });   
    });
 </script>

Step-3 Add this CSS in styles.css


<style type="text/css">
    #back_top {
    background-color: red;
    bottom: 22px;
    cursor: pointer;
    display: none;
    height: 44px;
    position: fixed;
    right: 200px;
    width: 54px;
}
</style>

Monday 30 September 2013

Create a Dynamic Shopping cart in Magento

This code is useful for carete a dynamic shopping cart in magento. Past this is code in header.phtml  This code display total number item in cart and subtotal.

<?php
$count = $this->helper('checkout/cart')->getSummaryCount(); //get Total Items In Cart
$total = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); //get Total Price
If($Count==0)
{
Echo $this->__('Items: %s',$count);
}
If($Count==1)
{
Echo $this->__(' Item: %s',$count);
}
If($Count>1)
{
Echo $this->__(' Items: %s',$count);
}
Echo $this->__(' Total: %s', $this->helper('core')->formatPrice($total, False));
?>

Put social networking site button in product view page magento

Past this code in product.phtml file This code  dipslay all social netwoking site share button.

<div class="addthis_toolbox addthis_default_style ">
     <a class="addthis_button_preferred_1"></a>
     <a class="addthis_button_preferred_2"></a>
     <a class="addthis_button_preferred_3"></a>
     <a class="addthis_button_preferred_4"></a>
     <a class="addthis_button_compact"></a>
     <a class="addthis_counter addthis_bubble_style"></a>
</div>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4ecde5167f228697"></script>