Follow by Email

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.

Friday, May 11, 2012

Add configurable product with size to wishlist

Hi all,
I have found some solution. I am using magento 1.6.2
if you go through the wishlist link in the product details page you will find the link like
/index.php/wishlist/index/add/product/47/
where product id is 47
Now you want to add a size with this configurable product. suppose your size attribute id is 128 and the value is 6 it's located the size M
now you can modify the wishlist url with
index.php/wishlist/index/add/product/47/?super_attribute[128]=6
you can change it using jQuery also.
when this url is open in the browser the product which id 47 will add in the wishlist with the size M.

Thursday, May 10, 2012

How to echo the number of reviews on the product page?

I have found out that this code finaly works when inserting into catalog/product/view.phtml
<?php
   $reviewData 
Mage::getModel('review/review/summary');
   echo 
'number of reviews: ' $reviewData->getTotalReviews($_product->getId());

   $reviews = Mage::getModel('review/review')->getCollection()
     ->addStoreFilter(Mage::app()->getStore()->getId())
     ->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
     ->addEntityFilter('product', $_product->getId())
     ->setDateOrder();


     foreach ($reviews->getItems() as $review){
     echo $review->getTitle().'<br>';
     echo $review->getNickname().'<br>';
     echo $review->getDetail().'<br>';

     }
?>


                           
                           
                             

Wednesday, May 2, 2012

Product stock back alerts

Magento having a good process That if a product is out of stock then there should be a link for product notification. when the product back in stock it will send a mail to user.
if you want to activate it. go to System > Configuration > Catalog > Catalog > Product Alerts
and configure it.
clear magento cash. and it's work.

Sunday, April 29, 2012

Add Google plus one Button in product details page

Past the following code in your magento product details page and you will see your Google plus one Button in your product page.
<div id="google-plusone" style="float: left;position: relative;top: 23px;left: -9px;">
<script type="text/javascript" src="https://apis.google.com/js/plusone.js">
{"parsetags": "explicit"},
{"lang": "<?php echo $this->__('en-GB') ?>"}
</script>
    <g:plusone size="medium" count="true" href="<?php echo trim(Mage::registry('current_product')->getProductUrl()) ?>"></g:plusone>
</div>
<script type="text/javascript">
gapi.plusone.go("google-plusone");
</script>

Wednesday, April 25, 2012

Magento onestep checkout remove shipping method step

In this blog, we will see how to remove the shipping method step from magento onepage checkout
The source code of this module has been tested in magento 1.6 version but should work fine 1.4+.
Removing the shipping method step is pretty simple, here are the steps to do it. I am going to set the ‘freeshipping’ shipping method as the default shipping method so that the checkout process goes smoothly. Also for this code to work make sure the freeshipping method is enabled from admin. Attached is the source code for this module
open app\code\core\Mage\Checkout\Block\Onepage.php
change
$stepCodes = array('billing', 'shipping', 'shipping_method', 'payment', 'review');
$stepCodes = array('billing', 'shipping', 'payment', 'review');  

also open app\code\core\Mage\Checkout\controllers\OnepageController.php
find function saveBillingAction()
 add
 if (!isset($result['error'])) {

                $method = 'freeshipping_freeshipping';
                $result = $this->getOnepage()->saveShippingMethod($method);
                Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress()-> setShippingMethod($method)->save();

before
if (!isset($result['error'])) {
                    if ($this->getOnepage()->getQuote()->isVirtual()) {
also change
elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
                    $result['goto_section'] = 'shipping_method';
                    $result['update_section'] = array(
                        'name' => 'shipping-method',
                        'html' => $this->_getShippingMethodsHtml()
                    );
into
elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
                        $result['goto_section'] = 'payment';
                        $result['update_section'] = array(
                        'name' => 'payment-method',
                        'html' => $this->_getPaymentMethodsHtml()
                        );

find function saveShippingAction()
look on
 if (!isset($result['error'])) {
                $result['goto_section'] = 'shipping_method';
                $result['update_section'] = array(
                    'name' => 'shipping-method',
 change
if (!isset($result['error'])) {
                    $result['goto_section'] = 'payment';
                    $result['update_section'] = array(
                        'name' => 'payment-method',

And also go to the app\code\core\Mage\Checkout\Model\Type\Onepage.php
add    if(empty($shippingMethod))
          $shippingMethod = 'freeshipping_freeshipping';
after starting of   public function saveShippingMethod($shippingMethod)    {

SQL Query to insert State/Region for India

I have made a sql file which can insert all the state of India into magento  please click here to download it.

Tuesday, April 24, 2012

Insert rupee symbol instead of Rs in magento

We have previously posted the solution for INR symbol. But that was playing with code, finally we were able to solve that problem in easy way. Here it goes:

Step 1: Goto: lib/Zend/Locale/Data/root.xml
Make changes in root.xml
find <currency type=”INR”> replace next line with this <symbol>Rs. </symbol>

Step 2: Goto: http://cdn.webrupee.com/js  or click
save the page as rupee.js and save it inside /js folder of the root.

Step 3: Goto: \app\design\frontend\default\your-theme\layout edit page.xml
After this <block type=”page/html_head” name=”head” as=”head”>
add <action method=”addJs”><script>rupee.js</script></action>

Step 4 : Clear cache by deleting all the files and folder inside /var/cache folder. Clear cache from back-end as well.

Sunday, April 22, 2012

Show recent view product in product view page in magento

What I have done to enable recently viewed on the product page was:
in catalog.xml inside the content block of catalog_product_view:

<block type="reports/product_viewed" name="reports.product.viewed" 
 as="recently_viewed" template="reports/product_viewed.phtml">
  <action method="setColumnCount"><columns>4</columns></action>
  <action method="setItemLimit"><type>recently_viewed</type><limit>4</limit></action>
</block>
 
in the template file: catalog/product/view.phtml:
 
<?php echo $this->getChildHtml('recently_viewed') ?>
 

Wednesday, April 18, 2012

Show all product in a page without pagination

If you want to display all product in a page  just open the file

app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php

Inside this file, the function that we are interested in is getLimit() beginning around line 723.

change
 $limit = 'all';

how do i change the price filter attributes price range with text

I am a coder but facing a unique problem. By default i get the magento price filter, with the price range but i want price filter like as given below:

under $50
$50 - $99(5)
$100 - $199(3)
$200 - $299(10)
$300 - $499(2)
$500 - $699(17)
$700 - $899(1)
$900 or above(12)

You can configure step of price ranges via admin panel. Log in into admin panel and go to System->Configuration->Catalog->Catalog->Layered Navigation. There select "Manual" in dropdown Price Navigation Step Calculation and enter prefered value into next input field.

Sunday, April 15, 2012

How to change pagination design in product listing page in magento

I have worked in the template\catalog\product\list\toolbar.phtml where you can change the total toolbar design. But there have a function
 <?php echo $this->getPagerHtml() ?>
it's for the pagination. if you want to change pagination html then go to the template\page\html\pager.phtml
and you can change it from here.

Saturday, April 14, 2012

jQuery conflict with prototype in Magento

Hi all,
I have got a good solution to prevent conflict jquery with prototype in magento
I have added the following code in page.xml after 
<action method="addJs"><script>mage/cookies.js</script></action>
this line and it's working fine.
 
<block type="core/text" name="google.cdn.jquery">
        <
action method="setText">
            <
text><![CDATA[<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
            
<script type="text/javascript">jQuery.noConflict();</script>]]>
            </
text>
        </
action>
    </
block>

Changing number of products / columns per row in Grid View

Hi All,
I am using Magento 1.6.2 and i wanted to bring 3 product in each row in product listing page grid view.
So i do the following.
Go to this file template/catalog/product/list.phtml

<?php
    $_productCollection
=$this->getLoadedProductCollection();
    
$_helper $this->helper(\'catalog/output\');
       
$this->setData(\'column_count\',3);            <------ add this line
?>
And it\’s really working.

Thursday, April 12, 2012

Magento - remove from validation text

Some of us may not want the validation advice that is given, and prefer to just use CSS styling.
Here is how to remove it:
in /js/prototype/validation.js

test : function(nameelmuseTitle{
        
var Validation.get(name);
        var 
prop '__advice'+name.camelize();
        try 
{
        
if(Validation.isVisible(elm) && !v.test($F(elm), elm)) {
            
//if(!elm[prop]) {
            /*
                var advice = Validation.getAdvice(name, elm);
                if (advice == null) {
                    advice = this.createAdvice(name, elm, useTitle);
                }
                this.showAdvice(elm, advice, name);
            */
            //}
            
elm[prop] 1;
            
elm.removeClassName('validation-passed');
            
elm.addClassName('validation-failed');
            return 
false;
        
else {
            
//var advice = Validation.getAdvice(name, elm);
            //this.hideAdvice(elm, advice);
            
elm[prop] '';
            
elm.removeClassName('validation-failed');
            
elm.addClassName('validation-passed');
            return 
true;
        
}
        } 
catch(e{
            
throw(e)
        
}
    }
,