Tuesday, 25 June 2013

Add Multiple Items in cart in Magento


$product_id = 32;  //Here is first Product id
$warranty_id = 35;  //Here is second Product id
//You can also pass multiple product id.
$someQty = 1; //Here is quantity
//You can add different quantity for different product.


$cart = Mage::getModel("checkout/cart");
$cart->addProduct($product_id, $someQty);
$cart->addProduct($warranty_id, $someQty);
            $cart->save();

Monday, 24 June 2013

Get Realted, Upsell, Cross sell Product collection in magento

Get Related Product Collection


$related_product_collection = $_product->getRelatedProductCollection();
$related_product_collection->AddStoreFilter();
foreach($related_product_collection as $pdt)
{
    $pdt_id=$pdt->getId();
    $model_rel = Mage::getModel('catalog/product'); //getting product model
    $_product_rel = $model_rel->load($pdt_id); //getting product object for particular product id
    $rel_name= $_product_rel->getName();
    $rel_price= number_format($_product_rel->getPrice(),2);
    $rel_img_url = $this->helper('catalog/image')->init($_product_rel, 'image')->keepFrame(false)->resize(156,107);   //Image resize code
?>


Get Upsell Product Collection


<?php
$upsell_product_collection = $_product->getUpSellProductCollection();
$upsell_product_collection->AddStoreFilter();
foreach($upsell_product_collection as $pdt)
{
    $pdt_id=$pdt->getId();
    $model_upsell = Mage::getModel('catalog/product');
    $_product_upsell = $model_upsell->load($pdt_id);
    $upsell_name= $_product_upsell->getName();
    $upsell_price= number_format($_product_upsell->getPrice(),2);
    $upsell_img_url = $this->helper('catalog/image')->init($_product_upsell, 'image')->keepFrame(false)->resize(100,100);
?>


Get Cross Sell Product Collection


<?php
$crossselll_product_collection = $_product->getCrossSellProducts(); $crossselll_product_collection>AddStoreFilter();
foreach($crossselll_product_collection as $pdt)
{
    $pdt_id=$pdt->getId();
    $model_crosssell = Mage::getModel('catalog/product');
    $_product_crosssell = $model_crosssell>load($pdt_id);
    $crosssell_name= $_product_crosssell->getName();
    $crosssell_price= number_format($_product_crosssell->getPrice(),2);
    $crosssell_img_url = $this->helper('catalog/image')->init($_product_crosssell, 'image')->keepFrame(false)->resize(100,100);
?>

Get Current Page Detail in magento

Get title
<?=Mage::getSingleton(‘cms/page’)->getTitle()?>
Get Identifier
<? echo Mage::getBlockSingleton(‘cms/page’)->getPage()->getIdentifier()?>
Get Page ID
<? echo Mage::getBlockSingleton(‘cms/page’)->getPage()->getId();  ?>

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)