Magento Coding Solutions

program to delete Products Image by Category Magento

Step 1:
create a .php file "deleteProductImage.php" on root directory

<?php
require_once "app/Mage.php";
umask(0);
Mage::app('admin');
Mage::setIsDeveloperMode(true);
echo "<pre>";
if(!empty($_REQUEST['catId'])){
$categoryId=$_REQUEST['catId'];
}else{
echo "please Pass CategoryId!";
exit;
}

$limit=1000;
$start=0;
if(!empty($_REQUEST['start'])){
$start=$_REQUEST['start'];
}
if(!empty($_REQUEST['limit'])){
$limit=$_REQUEST['limit'];
}

$productCollection = Mage::getModel('catalog/category')
                    ->load($categoryId)
                    ->getProductCollection();
$productCollection->getSelect()->limit($limit,$start);



/* print_r($collection);
die; */
/*  ->load($categoryId)
                    ->getProductCollection(); */
foreach($productCollection as $product){
//print_r($product);
/* echo $product->getId();
echo "---</br>"; */
//echo $product->getId();
/* echo "<br/>";
$MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
echo $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product'; */
//echo "<br/>";

$MediaGallery=Mage::getModel('catalog/product_attribute_media_api')->items($product->getId());
/* echo "<pre>";
print_r($MediaGallery);
echo "</pre>"; */
if(count($MediaGallery)>0){

foreach($MediaGallery as $eachImge){
$MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
$MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';
$DirImagePath=str_replace("/",DS,$eachImge['file']);
$DirImagePath=$DirImagePath;
// remove file from Dir

$io     = new Varien_Io_File();
$io->rm($MediaCatalogDir.$DirImagePath);

$remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']);
}
}else{
echo $product->getSku();
echo "</br>";

}


}
?>

Step 2:
Calling form url as
abc.com/deleteProductImage.php?catId=52limit=100start=0

Note: abc.com : your website.
          catId : pass category Id
         limit: how many product you want to delete.
         start : delete from product.




Magento. Listing Sub-Categories On A Category Page

Step 1 .
 create a "Static Block"
 and put in Content area below code.
{{block type="core/template" template="catalog/category/sub.phtml"}}

  



Step 2.
create "sub.phtml" file in  "/your template/catalog/category/sub.phtml"
 <div class="category-products">

<ul class="products-grid">
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren());
$catDat =explode(',',$category->getChildren());
if(count($catDat)>0){
$categorycount = 0;
foreach($catDat as $catId){
$_category=Mage::getModel('catalog/category')->load($catId);

if($_category->getIsActive()){

if ($categorycount == 0){
$class = "first";
}elseif ($categorycount == 4){
$class = "last";
}else{
$class = "";
}
?>

<li class="item <?=$class?> catitm" align="center">
<div class="itemimg"><?php //echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>
<a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><img src="<?php  echo $_category->getImageUrl(); ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>"     style="width: 425px !important;height: 425px;"/></a>
</div>
<h3><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a></h3>
</li>

<?php

if($categorycount == 4){
$categorycount = 0;
echo "</ul>\n\n<ul class=\"products-grid\">";
}else{
$categorycount++;
}


}


}

}?>

</ul>

</div>
Output is :



program to delete Products Image Magento

<?php
require_once "app/Mage.php";
umask(0);
Mage::app('admin');
Mage::setIsDeveloperMode(true);

$productCollection=Mage::getResourceModel('catalog/product_collection');
foreach($productCollection as $product){
echo $product->getId();
echo "<br/>";
         $MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
        echo $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';
echo "<br/>";

$MediaGallery=Mage::getModel('catalog/product_attribute_media_api')->items($product->getId());
echo "<pre>";
//print_r($MediaGallery);
echo "</pre>";

    foreach($MediaGallery as $eachImge){
        $MediaDir=Mage::getConfig()->getOptions()->getMediaDir();
        $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product';
        $DirImagePath=str_replace("/",DS,$eachImge['file']);
        $DirImagePath=$DirImagePath;
        // remove file from Dir

        $io     = new Varien_Io_File();
         $io->rm($MediaCatalogDir.$DirImagePath);

        $remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']);
    }


}
?>

magento custom search

Step1. create search input box. when you want to applied

<div class="row srchby">
<div class="col-sm-12"><h3 class="blue-font" align="center">Search our database</h3></div>

<form role="search" action="<?=Mage::getBaseUrl()?>catalogsearch/result/" method="get" class="form-inline">
<input type="hidden" name="cat" id="searchCatId" value="<?=$categoryid?>" >
    <div class="form-group"><input type="text" name="q" class="form-control"  placeholder="Enter Part No. / Model No" id="searchCat" value=""></div>
<div class="form-group"><input type="submit" value="Search Now"></div>
</form>

</div>
<style>
#getproductCat{
    position: absolute;
z-index: 999;
margin-left: 27.90%;
margin-top: -2%;
display: none;
}
</style>
<div class="row">
<div id="getproductCat">
<div class="product_instant_search_popup clearfix">
  <a href="javascript:void(0)" onclick="removeddCatSearch()"><img src="<?php echo $this->getSkinUrl('images/FME_instantsearch/close.png');?>" alt="" class="close" width="20px"></a>
<?php 


$a=1;
?>

<div class="related">
<ul id="listCatSearch">

</ul>
</div>
</div>
</div>
</div>
Step 2. create ajax to call indexController 
<script>
// instantsearch is my module.
jQuery(document).on('keyup','#searchCat',function(obj){
var seaechData=jQuery(this).val();
var catId=jQuery('#searchCatId').val();
var dataString ='seaechData='+seaechData+'&catId='+catId;
//alert('asdasd'+seaechData);
var urlPost= '<?php echo $this->getUrl('instantsearch/index/searchcat')?>';
jQuery('#listCatSearch').html('');
jQuery.ajax({
type: "POST",
url: urlPost,
data: dataString,
cache: false,
success: function(result){
//alert(result);
jQuery('#getproductCat').show();
jQuery('#listCatSearch').html(result);
 
}
}); 
});
function removeddCatSearch(){
jQuery('#listCatSearch').html('');
jQuery('#getproductCat').hide();
}
</script>

Step 3:
create function in IndexController in any module

public function searchcatAction(){
if(!empty($_REQUEST['seaechData'])){
$q = $_REQUEST['seaechData'];
$categoryid = $_REQUEST['catId']; 
$category = new Mage_Catalog_Model_Category();
if(!empty($categoryid)){
$category->load($categoryid);
}
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
if(!empty($q)){
$collection->addAttributeToFilter('name', array('like' => '%'.$q.'%'));
}
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(100);
$_productCollection=$collection;
//echo $_productCollection->count();
$result='';
if($_productCollection->count()>0){
foreach ($_productCollection as $_product){
$result.='<li id="is-'.$_product->getid().'">
<a href="'.$_product->getProductUrl().'">'.ucfirst($_product->getName()).'</a>

</li>';
}


}else{
$result.='<li id="is-0">No Recorde Found!</li>';
}
echo $result;
}
}