Show Top Selling Products in Magento
This a simple best seller product viewing code. you can put it in template. this will grab all the sold product according to sold amount, you can simply put a loop break to show number of products you want to show .
<?php /* Best Seller*/
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);
$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty()
->addAttributeToFilter('visibility', $visibility)
->setOrder('ordered_qty', 'desc');
//print_r($_productCollection);
foreach($_productCollection as $bs_product){
echo '<li><a href="'.$bs_product->getUrl_path().'">'.$bs_product->Name.'</a></li>';
}
?>
Category with Product in Menu Navigation of Magento frontend
Shops with a few products can view their product according to category in navigation. It will simplify a for customer to find their product find easily.
There is no such feature in magento default layered navigation. So, I created one, It take me 7 hr to write few line of code
you can add this code in cms page, /app/design/frontend/base/default/template/page/2column-right.phtml or any where in your theme. have fun !
<ul><?php
$collection = Mage::getModel('catalog/category')->getCollection();
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('id')
->load();
foreach ($collection AS $_cat)
{
if(($_cat->getId()!=1) && ($_cat->getId()!=2))
{
$cat = Mage::getModel('catalog/category')->load($_cat->getId());
echo '<li>'.$cat->getName().' </li>';
$cur_category = Mage::getModel('catalog/category')->load($_cat->getId());
$_productCollection =Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($cur_category)
->setOrder('position', 'ASC');
echo '<ul>';
foreach ($_productCollection AS $_product)
{
$cur_product = Mage::getModel('catalog/product')->load($_product->getId());
echo '<li><a href="'.$cur_product->getUrl_key().'.html">'.$cur_product->getName().'</a></li>';
}
echo '</ul></dd>';
}
} //end menu
?></ul>
Create Joomla registered user by component
Creating a Joomla component that create user, can help you to add user from other forum, script etc… or from a simple mailing list. below function will help you to create a registered user from email address and name. pass the array with your user data.
$qcnewu=array(
'name'=>$gname,
'username'=>$gemail,
'email'=>$gemail,
'password'=>$tmp_pwd,
'password2'=>$tmp_pwd,
'id'=>"0",
'gid'=>"0"
);
function auto_usr($qcnewu)
{
$db =& JFactory::getDBO();
global $mainframe;
$user = clone(JFactory::getUser());
$pathway =& $mainframe->getPathway();
$config =& JFactory::getConfig();
$authorize =& JFactory::getACL();
$document =& JFactory::getDocument();
$newUsertype = 'Registered';
if (!$user->bind( $qcnewu, 'usertype' )) {
JError::raiseError( 500, $user->getError());
}
$user->set('id', 0);
$user->set('usertype', $newUsertype);
$user->set('gid', 18);
$user->set('lastvisitDate', '0000-00-00 00:00:00');
$user->set('sendEmail', 0);
$date =& JFactory::getDate();
$user->set('registerDate', $date->toMySQL());
jimport('joomla.user.helper');
//$user->set('activation', JUtility::getHash( JUserHelper::genRandomPassword()) );
$user->set('block', '0');
// Show error with registration
if ( !$user->save() )
{
//JError::raiseWarning('', JText::_( $user->getError()));
return false;
}else return true;
}
Verifying file extention by JS and PHP before and after uploading file
Why we need a JS file extension verification ! user can easily submit from by writing js code in url or using tools like Greasemonkey. Yap ! user can upload server executable script file !!! but, we need a js extension verification code coz, Let user uploading a large file which takes time then user got the message ‘file format not supported’ ! ![]()
So, I used a js file extension verifier and a php extension / file type verifier both. have a look.
<script type="text/javascript">
function ex_chk(){
var str=document.getElementById("user_file").value;
compr = str.split(".");
i=compr.length-1;
if((compr[i]!="php")&&(compr[i]!="asp")
&&(compr[i]!="jsp")&&(compr[i]!="js")
&&(compr[i]!="cgi")&&(compr[i]!="swf")
&&(compr[i]!="exe")&&(compr[i]!="html"))
{ document.getElementById("file_upload").submit(); }
else{ alert("File format not supported"); }
}
</script>
<form name="file_upload" id="file_upload" action="upload.php" method="post" enctype="multipart/form-data" onsubmit="">
Click Browse, select a file, then <br>
click upload<br>
<input type="file" name="user_file" id="user_file" />
<br />
<input type="button" onclick="ex_chk();" name="btn_upload" value="Upload" />
</form>
php verification code:
function chk_ext(){
$allowedExtensions = array("php","asp","html","jsp","cgi","exe","js","swf");
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '') {
if (in_array(end(explode(".",
strtolower($file['name']))),
$allowedExtensions)) {
$re = "File type not supported";
return 0;
}
}
}
return 1;
}//func end
Finding a menu Itemid in Joomla
My joomla component need to redirect in another page of same site but, that menu item not showing active, coz Itemid is not added in redirect url. I can’t just add the the itemid coz it will install in many site where itemid will not be the same. It’s maybe very simple for any joomla developer. I detect the page’s itemid by link attribute.
$menu = & JSite::getMenu();
$item = $menu->getItems('link', 'index.php?option=com_qc&view=qc',1);
echo $item->id;
and redirect to this link
index.php?option=com_qc&view=qc&Itemid='.$item->id