Search This Blog

Friday 29 June 2012

Magento: Show Optgroup in select box

Here Furniture is OptGroup. Copy and paste in any PHTML file and check the output.

$options = array(0=>array('label' => 'Furniture',
                          'value' => array
                            (
                                0 => array
                                (
                                    'label' => 'Chair',
                                    'value' => 1
                                ),
                                1 => array
                                (
                                    'label' => 'Couch',
                                    'value' => 2
                                )
                            )
                        )
                    );
$tmp = Mage::app()->getLayout()->createBlock('core/html_select')
            ->setName('product_id')
            ->setId('product_id')
            ->setTitle('products')
            ->setClass('validate-select');
$select $tmp->setOptions($options);
echo $select->getHtml();

Magento: Get Child Categories by Category ID

  
$category_model = Mage::getModel('catalog/category'); //get category model
$_category = $category_model->load($categoryid); //$categoryid for which the child categories to be found       
$all_child_categories = $category_model->getResource()->getAllChildren($_category); //array consisting of all child categories id



OR

$children = Mage::getModel('catalog/category')->getCategories(4);
//4 is parent category ID
foreach ($children as $category) {
    echo $category->getName();
}

Wednesday 27 June 2012

Json Encode and Decoding in Magento

Json Encode an Array in Magento
$json = Mage::helper('core')->jsonEncode($array);

Json Decode in Magento
 $array = Mage::helper('core')->jsonDecode($jsonString);

Tuesday 26 June 2012

Transactional Email Preview Showing HTML code in Magento 1.6.2.0

This issue exists in Magento version 1.6.2.0. To fix the issue follow below steps.
  1. Copy app/code/core/Mage/Adminhtml/Block/System/Email/Template/preview.php file to app/code/local/Mage/Adminhtml/Block/System/Email/Template/preview.php
  2. Comment the following lines of code (Starts at line number 47) in protected function _toHtml() method
      $template->setTemplateText(
            $this->escapeHtml($template->getTemplateText())
        );

Friday 22 June 2012

Get first Image from Post content in Wordpress

Add this function to functions.php at bottom
 
function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches[1][0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}
 
Below code will display image URL from content of the post 
<?php echo catch_that_image(); ?>