Wednesday, June 6, 2012

Create review like module in magento


I have created a module by which we can marked product review helpful or not
Create a table
CREATE TABLE IF NOT EXISTS `review_like` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `review_id` int(11) NOT NULL,
  `customer_id` int(11) NOT NULL,
  `like` int(11) NOT NULL,
  `notlike` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `product_id` (`product_id`,`review_id`,`customer_id`)
)

Modify review_detail  add two field like, notlike
Add the below code in the app\code\core\Mage\Review\etc\config.xml
<review_like>
    <table>review_like</table>
 </review_like>

Add the following code in your review page after the review description
app\design\frontend\base\default\template\review\product\view\list.phtml

<?php
      $like_url = $this->getUrl('review/product/list').'id/'.$_review->getEntityPkValue().'/rev/'.$_review->getReviewId().'/lik/';                                                                     
 ?>
               <p class="rev_h"> Was this review helpful?
               <?php if(!Mage::getSingleton('customer/session')->isLoggedIn()):?> <a href="<?php echo $this->getSkinUrl(''); ?>customer/account/login/"><?php else: ?>             
               <a href="<?php echo $like_url; ?>1"><?php endif?>Yes</a>(<?php echo $this->htmlEscape($_review->getLike()) ?>) /
               <?php if(!Mage::getSingleton('customer/session')->isLoggedIn()):?> <a href="<?php echo $this->getSkinUrl(''); ?>customer/account/login/"><?php else: ?>
               <a href="<?php echo $like_url; ?>2"><?php endif?>No</a>(<?php echo $this->htmlEscape($_review->getNotlike()) ?>)  </p>    
After that go to app\code\core\Mage\Review\controllers\ProductController.php
Add   
$customer_id = Mage::getSingleton('customer/session')->getCustomerId();
                                                $rev_id = Mage::app()->getRequest()->getParam('rev');                                                             
                                                $lik = Mage::app()->getRequest()->getParam('lik');                                        
                                                if($lik>0){                                                                            
                                                                Mage::getModel('review/resource_review')->saveLikereview($product->getId(),$rev_id,$customer_id,$lik);     
                                                }

After
public function listAction()    {                    
                                if ($product = $this->_initProduct()) {
            Mage::register('productId', $product->getId());
Now open  app\code\core\Mage\Review\Model\Resource\Review.php  and past this
public function saveLikereview($product_id,$rev_id,$customer_id,$lik)
                {
                                $adapter = $this->_getWriteAdapter();
                                if($lik==1){ $like=1; $notlike=0; }
                                elseif($lik==2){ $like=0; $notlike=1; }
                                $data = array(
                                                    'product_id'  =>  $product_id,
                                                     'review_id'        =>  $rev_id,
                                                     'customer_id'  =>           $customer_id,
                                                   'like'                     =>           $like,
                                                    'notlike'                              =>           $notlike
                                      );
                                try{
                                                $res = $adapter->insert($this->_reviewLikeTable, $data);
                                                if($res)
                                                {
                                                                $adapter1 = $this->_getReadAdapter();
                                                                $select = $adapter->select()
                                                                                ->from($this->_reviewDetailTable, array('like','notlike'))
                                                                                ->where('review_id = :review_id');
                                                                $no_rev = $adapter1->fetchAll($select, array(':review_id' => $rev_id));
                                                               
                                                                                $condition = "review_id =".$rev_id;
                                                                                $data = array(                                                                                                   
                                                                                                                  'like'                     =>           ($like + $no_rev[0]['like']),
                                                                                                                  'notlike'                              =>           ($notlike + $no_rev[0]['notlike'])
                                                                                                                  );
                    $adapter->update($this->_reviewDetailTable, $data, $condition);      
                                                }
                                }
                                catch(Exception $e){      }             
Also past  $this->_reviewLikeTable   = $this->getTable('review/review_like');  under protected function _construct()
Now open app\code\core\Mage\Review\Model\Resource\Review\Collection.php
And add $this->_reviewLikeTable     = $this->getTable('review/review_like'); under  protected function _construct()
And change array('detail_id', 'title', 'detail', 'nickname', 'customer_id')); in to
array('detail_id', 'title', 'detail', 'nickname', 'customer_id','like','notlike'));             


Tuesday, May 22, 2012

Get catagory name from product id in cart page

          echo Mage::getModel('catalog/category')->load($this->getProduct()->
getCategoryIds($_item->getProductId()))->getName();



This is the code to get category name in cart page

Friday, May 18, 2012

Register , Login , Forget password code for magento

Create user
$customer = Mage::getModel('customer/customer');
                  $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
                  $customer->loadByEmail($create['email']);
                  if(!$customer->getId())
                  {
                      $customer->setEmail($create['email']);
                      $customer->setFirstname($create['firstname']);
                      $customer->setLastname($create['lastname']);
                      $customer->setPassword($create['password']);               
                      $customer->save();
                      $customer->setConfirmation(null);
                      $customer->save();
                      $customer->sendNewAccountEmail();
                      $session->login($create['email'], $create['password']);
                   }

Login user
          $session->login($login['username'], $login['password']);

Forget password
$customer = Mage::getModel('customer/customer');
                  $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
                  $customer->loadByEmail($forget['email']);
                  if($customer->getId())
                  {
                      $customer->sendPasswordReminderEmail();
                  }

Wednesday, May 16, 2012

Autocomplete ajax search in magento

Hi all,
I had a problem with Ajax search auto suggest in magento. The top search in magento i want to make it as a ajax search. so i did some change in
app\code\core\Mage\CatalogSearch\Block\Autocomplete.php
under the "getSuggestData" function
just comment the    foreach ($collection as $item) {     start to end
and past the this code

$resource = Mage::getSingleton('core/resource');
            $conn = $resource->getConnection('core_read');
            $results = $conn->query("SELECT pv.value,pv.entity_id FROM catalog_product_entity_varchar pv,catalog_product_entity pe where pv.attribute_id=65 and pv.value like '%".$query."%' and pv.entity_id=pe.entity_id and pe.type_id='configurable'")->fetchAll();       
            //this custom query add by sourav for autocomplet product search
           
            $counter = 0;
            $data = array();
            foreach ($results as $item) {
                $_data = array(
                    'title' => $item['value'],
                    'row_class' => (++$counter)%2?'odd':'even',
                    'num_of_results' => ''
                );
       
                if ($item['value'] == $query) {
                    array_unshift($data, $_data);
                }
                else {
                    $data[] = $_data;
                }
               
            }

Execute custom SQL query in magento

This is the code to run custom sql in magento.

$resource = Mage::getSingleton('core/resource');
$conn = $resource->getConnection('core_read');
$results = $conn->query("SELECT pv.value,pv.entity_id FROM catalog_product_entity_varchar pv,catalog_product_entity pe where pv.attribute_id=65 and pv.value like '%".$query."%' and pv.entity_id=pe.entity_id and pe.type_id='configurable'")->fetchAll();   

Monday, May 14, 2012

How to list all special price products according the category

Hi all,
I have a part in my project that i have to show all special price products in a page for different category and i have done it. It\’s very simple just go to your admin and create a static page \"Sale\". Now add
{{block type=\"catalog/product_list\" name=\"home.catalog.product.list\" alias=\"product_homepage\" template=\"catalog/product/sale.phtml\"}}
and save the page.
after that go to your app\\code\\core\\Mage\\Catalog\\Block\\Product and create a file sale.phtml and pest the following code in to your file and run it. 
<?php if(Mage::app()->getRequest()->getParam(\'category_id\')==\'\'){ ?><h2 class=\"subtitle\"><?php echo $this->__(\'Sale\'?></h2><?php
    $category_model 
Mage::getModel(\'catalog/category\'); //get category model
    
$_category $category_model->load(2);  
//$categoryid for which the child categories to be found    
    
$all_child_categories $category_model->getResource()
->getAllChildren($_category);
   
    foreach(
$all_child_categories as $category)
        
{
            
if($category!=2){
                $cur_category 
Mage::getModel(\'catalog/category\')->load($category);
                if(
$cur_category->getIsActive()){  ?>
                    
<a href=\"<?php echo $this->getBaseUrl(\'all-new-arrival\').\'sale/?category_id=\'.$category; ?>\"><img src=\"<?php echo $cur_category->getImageUrl(); ?>\" />
<div><?php echo $cur_category->getName(); ?></div></a>                   
            
<?php    }
            }
        }
?>   
<?php }else{ ?>

<?php
    $todayDate  
Mage::app()->getLocale()->date(
)->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
    
$categoryId Mage::app()->getRequest()->getParam(\'category_id\');
    
$catagory_model Mage::getModel(\'catalog/category\')->load($categoryId); //where $category_id is the id of the category
    
$collection Mage::getResourceModel(\'catalog/product_collection\');
    
$collection->addCategoryFilter($catagory_model); //category filter
    
$collection->addAttributeToFilter(\'status\',1); //only enabled product
    
$collection->addAttributeToFilter(\'special_from_date\'
      array(\'date\' => true\'to\' => $todayDate))
       ->
addAttributeToFilter(\'special_to_date\', array(\'or\'=> 
        array( => array(\'date\' => true\'from\' => $todayDate),
                    
=> array(\'is\' => new Zend_Db_Expr(\'null\')))
                ), 
\'left\');
    
$collection->addAttributeToSelect(array(\'name\',\'url\',\'small_image\',\'price\',\'special_price\')); //add product attribute to be fetched
         
    
$collection->addStoreFilter();   
    if(!empty(
$collection))
    
{ ?>
    
<div class=\"category_products\">
      
<?php  foreach ($collection as $_product):  ?>
          
<div class=\"product\">
            <
a href=\"<?php echo $_product->getProductUrl(); ?>\">
            <
img src=\"<?php echo $this->helper(\'catalog/image\')->init($_product, \'small_image\')->resize(172,235); ?>\" width=\"172\" height=\"235\" />
            <
div class=\"product-name\">
                <
a href=\"<?php echo $_product->getProductUrl() ?>\" >
                
<?php echo substr($_product->getName(),0,40).\' ...\' ?> 
                </
a>
            </
div><br/>
            <
div class=\"price\">
            
<?php echo $this->getPriceHtml($_producttrue?>
            
</div>           
            </
a>
         </
div>  
      
<?php endforeach;   ?>
     
</div>      
 
<?php   }else{ ?>          
        
<class=\"note-msg\"><?php echo \'No products exists\';  ?>  </p>
 
<?php   }  ?>
<?php } ?>
 

Category wise New product

If you want to show new product Category wise then go to the app\code\core\Mage\Catalog\Block\Product\New.php add
if($categoryId = Mage::app()->getRequest()->getParam('category_id')){           
        $category = Mage::getModel('catalog/category')->load($categoryId);
        $collection->addCategoryFilter($category);
        }          
before
        $this->setProductCollection($collection);

also open app\design\frontend\base\default\template\catalog\product\new.phtml

<?php if(Mage::app()->getRequest()->getParam('category_id')==''){ ?>
<h2 class="subtitle"><?php echo $this->__('New Arrivals') ?></h2>
<?php
    $category_model = Mage::getModel('catalog/category'); //get category model
    $_category = $category_model->load(2); //$categoryid for which the child categories to be found    
    $all_child_categories = $category_model->getResource()->getAllChildren($_category);
   
    foreach($all_child_categories as $category)
        {
            if($category!=2){
                $cur_category = Mage::getModel('catalog/category')->load($category);
                if($cur_category->getIsActive()){  ?>
                    <a href="<?php echo $this->getBaseUrl('all-new-arrival').'all-new-arrival/?category_id='.$category; ?>"><img src="<?php echo $cur_category->getImageUrl(); ?>" /><div><?php echo $cur_category->getName(); ?></div></a>                   
            <?php    }
            }
        }
?>   
<?php }else{  ?>
before
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
and close the bracket end of the page.