Friday 24 May 2013

Get Page detail by page id in magento

Get Page tilte


echo Mage::getModel('cms/page')->load(3)->getTitle();  // Here 3 is page id


Get Page content


echo Mage::getModel('cms/page')->load(3)->getContent(); // Here 3 is page id


Get Page Url


echo Mage::helper('cms/page')->getPageUrl(3) ; // Here 3 is page id


Get excerpt


$full_content = Mage::getModel('cms/page')->load(3)->getContent(); // Here 3 is page id
$excerpt = strip_tags(substr($full_content,0,200));  // Here 200 is character length.
echo $excerpt;

Friday 17 May 2013

Get All Product Collection in magento

Get All Product Collection
 
<?php
       $product_collection = Mage::getModel('catalog/product')->getCollection()->setOrder('name', 'asc'); 
      
       //getting the product collection, results are ordered by product name
       
      foreach($product_collection as $_product)
       {
           echo $_product->getId() . "<br />";
       }
?>


Get Limitation product


       <?php
       $limit = 5;
       $starting_from = 0;
       $product_collection = Mage::getModel('catalog/product')->getCollection()->setOrder('name', 'asc'); 
       //getting the product collection, results are ordered by product name

       $product_collection->getSelect()->limit($limit,$starting_from);   
       
       //where $limit will be the number of results we want, $starting_from will be the index of the result set to be considered as starting point (note 0 is the index of first row)
       //this will ouptput 5 records starting from 1rd record

       foreach($product_collection as $_product)
       {
           echo $_product->getName().'<br/>';
           echo $_product->getId() . "<br />";
       }
       ?>


Get Latest Product Collection


       <?php
          $_productCollection = Mage::getResourceModel('reports/product_collection')
                           ->addAttributeToSelect('*')
                           ->setOrder('created_at', 'desc')
                           ->setPage(1, 5);
       ?>
       <h2>Latest Products</h2>
       <?php foreach($_productCollection as $_product) : ?>
        <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a></br>
       <?php endforeach; ?>


Tuesday 14 May 2013

Get excerpt by post id in wordpress

// Function add in function.php

function get_excerpt_by_id($post_id){
   $the_post
= get_post($post_id);
   $the_excerpt
= $the_post->post_content;
   $excerpt_length
= 35;    //Sets excerpt length by word count
   $the_excerpt
= strip_tags(strip_shortcodes($the_excerpt));
   $words
= explode(' ', $the_excerpt, $excerpt_length + 1);

   
if(count($words) > $excerpt_length) :
       array_pop
($words);
       array_push
($words, '…');
       $the_excerpt
= implode(' ', $words);
   endif
;

   $the_excerpt
= '<p>' . $the_excerpt . '</p>';

   
return $the_excerpt;
}

// Call in template files

echo get_excerpt_by_id($post_id);   // you will get excerpt by post id

Get Top Parent category id from sub-child category in magento

// Get Parent Category Id

<?php
$_cat = new Mage_Catalog_Block_Navigation();
$curent_cat = $_cat->getCurrentCategory();
$curent_cat_id = $curent_cat->getId();?>


<?php $parentId=Mage::getModel('catalog/category')->load($curent_cat_id)->getParentId();
echo $parentId; // $parentId will print your current category's parent Id
?>




   // Get Top Parent Category Id

    $_cat = new Mage_Catalog_Block_Navigation();
    $curent_cat = $_cat->getCurrentCategory();  // Get Current Category
    $category_ids = $_cat->getCurrentCategory()->getParentCategory()->getPath(); 
    // Get Parent Category detail
    $cat_id = explode('/',$category_ids);   
    $top_parent_id = $cat_id[2];     // Get Parent Category Id
    echo  $top_parent_id; // $top_parent_id will print your current category's top parent Id.

   Suppose you click on Submenu 2.2.3 page and you want to find Top parent category id (Get Category Id)