Tuesday, 18 August 2015

How to get only cart sidebar Layout block in magento

In your controller you could try something like this:

$block = $this->getLayout()->createBlock('checkout/cart_sidebar');

$block->setTemplate('checkout/cart/sidebar.phtml');
 
Depending on your configuration (Config -> Checkout -> Shopping Cart Sidebar),
you can render the template with  
 
$block->toHtml(); 
If you use a custom template, you could ignore the config value so it renders anytime.

JQuery serialize() function with $.ajax() post for bigger HTML forms

JQuery provides a very useful function jQuery.serialize() which encodes a set of form elements as a string for submission, and is very useful while we are dealing with huge HTML forms. Basically it will combine your values of form element and then you can post it on any server side scripting page.

For example, suppose you have a very long form with many fields and you want to post it with the help of $.ajax() function, you need to add just few lines of code for jquery from with ajax, no need to add id attribute to any form elements, just have to give id to form itself as follows:

<form name="frm_details" id="frm_details" method="post">
<input type="text" name="name" value="jems">
<input type="submit" name="submit" value="submit">
</form>

The above form will have all the fields like text boxes, text areas, select boxes, radios etc. having only name attribute, just as our usual html form. Now the JQuery code will be as follows:
<script>
 $(function() {
   $("#product_details").on("submit", function(event) {
      event.preventDefault();

      $.ajax({
         url: "somefile.php",
         type: "post",
         data: $(this).serialize(),
         success: function(d) {
           alert(d);
         }
      });
    });
  });
</script>

Saturday, 8 August 2015

Including jQuery condition if in page have jQuery or not

// Including jQuery conditionnally.
  if (typeof jQuery === 'undefined') {
    document.write("\u003cscript src=\"\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1\/jquery.min.js\" type=\"text\/javascript\"\u003e\u003c\/script\u003e");
    document.write('<script type="text/javascript">jQuery.noConflict();<\/script>');
  }

Monday, 20 July 2015

How to Set, Retrieve and Unset Session Variables in Magento

Set session Data


$session = Mage::getSingleton("core/session", array("name"=>"frontend"));

$session->setData("day_filter", 'weeks');
$session->setData("days", '5');          
$session->setData("next_delivery_date", '2012-05-12');
 
or
 
Mage::getSingleton('core/session')->setIsSalesrepTax('test'); 
 

UnSet session Data

 
$session->unsetData('day_filter');
$session->unsetData('days');
$session->unsetData('next_delivery_date'); 
$session->unsetData('IsSalesrepTax');  

Session Data Set to null

$session->setData('day_filter', NULL);
$session->setData('days', NULL);
$session->setData('next_delivery_date', NULL); 
 
 

Magento Set, Retrieve and Unset Session Variables

 To set a Magento session variable:

$myValue = 'My session';
Mage::getSingleton('core/session')->setMyValue($myValue);
 

To Retrieve:

$myValue=Mage::getSingleton('core/session')->getMyValue();

To Unset:

Mage::getSingleton('core/session')->unsMyValue();

Wednesday, 15 July 2015

Reverse array values while keeping keys

$a = array('a' => 'a1', 'b' => 'a2', 'c' => 'a3', 'd' => 'a4', 'e' => 'a5'); $k = array_keys($a);$v = array_values($a); $rv = array_reverse($v); $b = array_combine($k, $rv); var_dump($b);
 
 
Result: 

array(5) { 'a' => string(2) "a5" 'b' => string(2) "a4" 'c' => string(2) "a3" 'd' => string(2) "a2" 'e' => string(2) "a1"}

Saturday, 11 July 2015

How to sort Magento products by popularity in frontend?

I have created a module to add a new option in 'sort by' dropdown box in the category page. the new option should show in each and every category page same as 'position' option which is already there. I want to add 'popularity' as an option. My Model class is executing and it's adding the option to the option array. But the block is not working. Please I need your help to find the issue in my code?
Here is the Block class


<?php
class Tal_Popularity_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar{ public function setCollection($collection) { $this->_collection = $collection; $this->_collection->setCurPage($this->getCurrentPage()); // we need to set pagination only if passed value integer and more that 0 $limit = (int)$this->getLimit(); if ($limit) { $this->_collection->setPageSize($limit); } if($this->getCurrentOrder() == 'popularity'){ $this->_collection->sortByReview($this->getCurrentDirection()); } else if ($this->getCurrentOrder()) { $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection()); } return $this; }}
 
 
following is my config.xml
 
<?xml version="1.0" ?> <config> <modules> <Tal_Popularity> <version>0.1.0</version> </Tal_Popularity> </modules> <frontend> <routers> <popularity> <use>standard</use> <args> <module>Tal_Popularity</module> <frontName>popularity</frontName> </args> </popularity> </routers> </frontend> <global> <models> <catalog> <rewrite> <config>Tal_Popularity_Model_Config</config> </rewrite> </catalog> </models> <blocks> <catalog> <rewrite> <product_list_toolbar>Tal_Popularity_Block_Product_List_Toolbar</product_list_toolbar> </rewrite> </catalog> <catalog> <rewrite> <catalog>Tal_Popularity_Block</catalog> </rewrite> </catalog> </blocks> <resources> <popularity_setup> <setup> <module>Tal_Popularity</module> </setup> <connection> <use>core_setup</use> </connection> </popularity_setup> <popularity_write> <connection> <use>core_write</use> </connection> </popularity_write> <popularity_read> <connection> <use>core_read</use> </connection> </popularity_read> </resources> </global> </config> <rewrite> <product_list_toolbar>Tal_Popularity_Block_Product_List_Toolbar</product_list_toolbar> </rewrite> </catalog> <catalog> <rewrite> <catalog>Tal_Popularity_Block</catalog> </rewrite> </catalog> </blocks> <resources> <popularity_setup> <setup> <module>Tal_Popularity</module> </setup> <connection> <use>core_setup</use> </connection> </popularity_setup> <popularity_write> <connection> <use>core_write</use> </connection> </popularity_write> <popularity_read> <connection> <use>core_read</use> </connection> </popularity_read> </resources> </global></config> 




Friday, 10 July 2015

Sort By Price: Low to High and High to Low in Magento

To Make this change please navigate through -> /app/design/frontend/default/your-theme/template/catalog/product/list/toolbar.phtml file.
Find

<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"
<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
<?php echo $this->__($_order) ?>
</option>
 
and Replace with

<?php if ($_order != 'Price'): ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
    <?php echo $this->__($_order) ?>
</option>
<?php else: ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key) && $this->getCurrentDirection() == 'asc'): ?> selected="selected"<?php endif; ?>>
    <?php echo $this->__($_order) . ': Low to High' ?>
</option>
<option value="<?php echo $this->getOrderUrl($_key, 'desc') ?>"<?php if($this->isOrderCurrent($_key) && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
    <?php echo $this->__($_order) . ': High to Low' ?>
</option>
<?php endif; ?>