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; ?>

Changed the base URL but old base url seems to be cached somewhere in Magento

Make a test.php file and upload in your server and run it. so clean your store and remove unwanted file and folder like var/cache... 

## Function to set file permissions to 0644 and folder permissions to 0755

function AllDirChmod( $dir = "./", $dirModes = 0755, $fileModes = 0644 ){ $d = new RecursiveDirectoryIterator( $dir ); foreach( new RecursiveIteratorIterator( $d, 1 ) as $path ){ if( $path->isDir() ) chmod( $path, $dirModes ); else if( is_file( $path ) ) chmod( $path, $fileModes ); }}
## Function to clean out the contents of specified directory
function cleandir($dir) { if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..' && is_file($dir.'/'.$file)) { if (unlink($dir.'/'.$file)) { } else { echo $dir . '/' . $file . ' (file) NOT deleted!<br />'; } } else if ($file != '.' && $file != '..' && is_dir($dir.'/'.$file)) { cleandir($dir.'/'.$file); if (rmdir($dir.'/'.$file)) { } else { echo $dir . '/' . $file . ' (directory) NOT deleted!<br />'; } } } closedir($handle); } } function isDirEmpty($dir){ return (($files = @scandir($dir)) && count($files) <= 2);} echo "----------------------- CLEANUP START -------------------------<br/>";$start = (float) array_sum(explode(' ',microtime()));echo "<br/>*************** SETTING PERMISSIONS ***************<br/>";echo "Setting all folder permissions to 755<br/>";echo "Setting all file permissions to 644<br/>";AllDirChmod( "." );echo "Setting pear permissions to 550<br/>";chmod("pear", 550); echo "<br/>****************** CLEARING CACHE ******************<br/>"; if (file_exists("var/cache")) { echo "Clearing var/cache<br/>"; cleandir("var/cache");} if (file_exists("var/session")) { echo "Clearing var/session<br/>"; cleandir("var/session");} if (file_exists("var/minifycache")) { echo "Clearing var/minifycache<br/>"; cleandir("var/minifycache");} if (file_exists("downloader/pearlib/cache")) { echo "Clearing downloader/pearlib/cache<br/>"; cleandir("downloader/pearlib/cache");} if (file_exists("downloader/pearlib/download")) { echo "Clearing downloader/pearlib/download<br/>"; cleandir("downloader/pearlib/download");} if (file_exists("downloader/pearlib/pear.ini")) { echo "Removing downloader/pearlib/pear.ini<br/>"; unlink ("downloader/pearlib/pear.ini");} echo "<br/>************** CHECKING FOR EXTENSIONS ***********<br/>";If (!isDirEmpty("app/code/local/")) { echo "-= WARNING =- Overrides or extensions exist in the app/code/local folder<br/>";}If (!isDirEmpty("app/code/community/")) { echo "-= WARNING =- Overrides or extensions exist in the app/code/community folder<br/>";}$end = (float) array_sum(explode(' ',microtime()));echo "<br/>------------------- CLEANUP COMPLETED in:". sprintf("%.4f", ($end-$start))." seconds ------------------<br/>";?>

Wednesday 1 July 2015

How to get number of rows affected, While executing mysql query PHP

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');
/* this should return the correct numbers of deleted records */
mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Records deleted: %d\n", mysql_affected_rows());
/* with a where clause that is never true, it should return 0 */
mysql_query('DELETE FROM mytable WHERE 0');
printf("Records deleted: %d\n", mysql_affected_rows());
?>