Tuesday, July 19, 2011

to find url in text and make them hyperlink


$text = preg_replace("
  #((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie",
  "'<a href=\"$1\" target=\"_blank\">$3</a>$4'",
  $text);

Thursday, June 30, 2011

How to get sold quantity of product in magento


If you want to have it so that it only shows quantity sold on the product page on items that are currently on sale.. use the following., There will also be an alert for customers when there is only 1 remaining in stock.Then use the following code.
<?php 
$_finalPrice 
$this->helper('tax')->getPrice($_product$_product->getFinalPrice());$_regularPrice $this->helper('tax')->getPrice($_product$_product->getPrice());
if (
$_regularPrice != $_finalPrice):$sku nl2br($_product->getSku());$to $_product->getResource()->formatDate(time());$from $_product->getResource()->formatDate(time() - 60 60 24 1);$_productCollection Mage::getResourceModel('reports/product_collection')
->
addOrderedQty($from$totrue)
->
addAttributeToFilter('sku'$sku)
->
setOrder('ordered_qty''desc')
->
getFirstItem();$product $_productCollection;

echo 
'Already Bought Today '.(int)$product->ordered_qty
endif;
?>
<?php 
if ((int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()==1): ?><p style="color:#990000; padding:5px 0; text-align:right;"><strong>ONLY 1 LEFT IN STOCK!</strong></p><?php endif; ?>

Or if you want to show stock sold on the product page for the entire duration the product has been active, use the following
<?php 

$sku 
nl2br($_product->getSku());$_productCollection Mage::getResourceModel('reports/product_collection')
->
addOrderedQty()
->
addAttributeToFilter('sku'$sku)
->
setOrder('ordered_qty''desc')
->
getFirstItem();$product $_productCollection;

echo 
'Already Bought '.(int)$product->ordered_qty?>

and finally if you want to just just on the page how many have been sold of the product for today, use the following

<?php 
$sku 
nl2br($_product->getSku());$to $_product->getResource()->formatDate(time());$from $_product->getResource()->formatDate(time() - 60 60 24 1);$_productCollection Mage::getResourceModel('reports/product_collection')
->
addOrderedQty($from$totrue)
->
addAttributeToFilter('sku'$sku)
->
setOrder('ordered_qty''desc')
->
getFirstItem();$product $_productCollection;

echo 
'Quantity Ordered Today '.(int)$product->ordered_qty
endif;
?>

Just copy and paste into your view.phtml file, into a suitable place :)

How to resize Image in magento



Here is the custom image resize function. This function will resize image proportionally.




/**
* Resize Image proportionally and return the resized image url
*
* @param string $imageName name of the image file
* @param integer|null $width resize width
* @param integer|null $height resize height
* @param string|null $imagePath directory path of the image present inside media directory
* @return string full url path of the image
*/
public function resizeImage($imageName, $width=NULL, $height=NULL, $imagePath=NULL)
{
$imagePath = str_replace("/", DS, $imagePath);
$imagePathFull = Mage::getBaseDir('media') . DS . $imagePath . DS . $imageName;

if($width == NULL && $height == NULL) {
$width = 100;
$height = 100;
}
$resizePath = $width . 'x' . $height;
$resizePathFull = Mage::getBaseDir('media') . DS . $imagePath . DS . $resizePath . DS . $imageName;

if (file_exists($imagePathFull) && !file_exists($resizePathFull)) {
$imageObj = new Varien_Image($imagePathFull);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(TRUE);
$imageObj->resize($width,$height);
$imageObj->save($resizePathFull);
}

$imagePath=str_replace(DS, "/", $imagePath);
return Mage::getBaseUrl("media") . $imagePath . "/" . $resizePath . "/" . $imageName;
}



You can write the following code in template (.phtml) file to display the resized image:


<img src="<?php echo Mage::helper('moduleName')->resizeImage('abc.jpg', 400, 300, 'xyz/image'); ?>" alt="resized image" />

Thursday, June 23, 2011

Template Hints for the Magento Admin

Simply paste the following into
app/code/core/Mage/Adminhtml/Block/Template.php
public function fetchView($fileName)
{
Varien_Profiler::start($fileName);
extract ($this->_viewVars);
$do = $this->getDirectOutput();
if (!$do) {
ob_start();
}
echo '<div style="position:relative;
border:1px dotted red; margin:6px 2px; 

padding:18px 2px 2px 2px; zoom:1;">

<div style="position:absolute; left:0; top:0; padding:2px 5px; 
background:red; color:white; font:normal 11px Arial; text-align:left !important; 
z-index:998;" onmouseover="this.style.zIndex=\'999\'" onmouseout="this.style.zIndex=\'998\'"
title="'.$fileName.'">'.$fileName.'</div>';
$thisClass = get_class($this);
echo '<div style="position:absolute; 
right:0; top:0; padding:2px 5px; background:red; color:blue; font:normal 11px Arial; 
text-align:left !important; z-index:998;" onmouseover="this.style.zIndex=\'999\'"
onmouseout="this.style.zIndex=\'998\'" title="'.$thisClass.'">'.$thisClass.'</div>';
try {
include $this->_viewDir . DS . $fileName;
} catch (Exception $e) {
ob_get_clean();
throw $e;
}
echo '</div>';
if (!$do) {
$html = ob_get_clean();
} else {
$html = '';
}
Varien_Profiler::stop($fileName);
return $html;
}
before the last closing brace }.
Just be sure to remove the code again then you are finished. If there is enough demand for it I might package the change up into a new MageBase extension.