Thursday, January 5, 2012

Magento most viewed products

Magento most viewed products
http://blog.chapagain.com.np/magento-how-to-get-most-viewed-products/

Magento Layered Navigation in Drop-Down

Magento Layered Navigation in Drop-Down

It is very simple.
We just need to replace the contents of file
/app/design/frontend/default/your-magento-template/template/catalog/layer/filter.phtml
with

<select onchange="setLocation(this.value)">
  <option value=""><?php echo 'Choose an Option...' ?></option>
<?php foreach ($this->getItems() as $_item): ?>
    <option
        <?php if ($_item->getCount() > 0): ?>
        value="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel() ?>
        <?php else: echo '>' . $_item->getLabel() ?>
        <?php endif; ?>
        (<?php echo $_item->getCount() ?>)
    </option>
<?php endforeach ?>
</select>
and you are done.

Wednesday, January 4, 2012

Search magento function

Search magento function 
http://magento-php-xref.tech-resolved.com/nav.html?_functions/index.html
http://magento.itx-technologies.com/nav.php?app/code/core/Mage/Adminhtml/Block/Report/Product/Viewed/Grid.php.source.php

Magento : Add custom image attribute to category

 Magento : Add custom image attribute to category

Just copy paste the below code in header.phtml and run yourmagento once, your attribute will be created and you can see in backend under manage category. After you are done remove this code again.

 ------------------------------------------------------------------------------------------
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('catalog_category', 'sliderimage', array(
    'group'         => 'General',
    'input'         => 'image',
    'type'          => 'varchar',
    'label'         => 'Slider Image',
    'backend'       => 'catalog/category_attribute_backend_image',
    'visible'       => 1,
    'required'        => 0,
    'user_defined' => 1,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
------------------------------------------------------------------------------------------

Tuesday, January 3, 2012

Magento Admin tutorial video part-1

 Magento Admin tutorial video part-1
<iframe src="http://player.vimeo.com/video/2486600?title=0&amp;byline=0&amp;portrait=0" width="400" height="206" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe><p><a href="http://vimeo.com/2486600">Magento - a quick review of the admin area</a> from <a href="http://vimeo.com/stastic">Shayne Sanderson</a> on <a href="http://vimeo.com">Vimeo</a>.</p>

Monday, January 2, 2012

Export Categories Magento

 <?php
  //Export Categories Magento
//Upload this file in root and access from url.
//Exported file will be stored in var/import

    define('MAGENTO', realpath(dirname(__FILE__)));
    require_once MAGENTO . '/app/Mage.php';
    Mage::app();

    $category = Mage::getModel ( 'catalog/category' );
    $tree = $category->getTreeModel ();
    $tree->load ();

    $ids = $tree->getCollection ()->getAllIds ();

    if ($ids) {
        $file = "var/import/catwithid.csv";
        file_put_contents($file,"catId, catName\n");
        foreach ( $ids as $id ) {
          $string = $id . ', ' .$category->load($id)->getName() . "\n";
            file_put_contents($file,$string,FILE_APPEND);
        }
    }
   
    ?>

Thursday, December 29, 2011

Upload image through iframe magento

Upload image through iframe magento

My phtml file

<form id="file_upload_form" method="post" enctype="multipart/form-data" action="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'catalog/product/myupload';?>">
<input name="file" id="file" size="27" type="file" /><br />
<input type="submit" name="action" value="Upload" /><br />
<iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
 <div id="image_details"></div>
<script type="text/javascript">
function init() {
    document.getElementById('file_upload_form').onsubmit=function() {
        document.getElementById('file_upload_form').target = 'upload_target'; //'upload_target' is the name of the iframe
        document.getElementById("upload_target").onload = uploadDone; //This function should be called when the iframe has compleated loading
            // That will happen when the file is completely uploaded and the server has returned the data we need.
    }
}
function uploadDone() { //Function will be called when iframe is loaded
    var ret = frames['upload_target'].document.getElementsByTagName("body")[0].innerHTML;
    document.getElementById("uploadaddtocart").disabled = false;
    document.getElementById("deleteimage").style.display = "block";
   
    jQuery("#image_details").html("<img src='http://xaprio.com/Client/demos/tfasigns/media/customertemplates/" + ret + "' />");
    }
window.onload=init;
</script>


My controller action


 public function myuploadAction()
    {
    /*echo "hello";   
    print_r($_POST);
    print_r($_FILES);*/
    $path = '/home/xaprioc/public_html/Client/demos/tfasigns/media/customertemplates';
   
    $imgarr = $this->upload('file',$path,'txt,jpg,jpeg,gif,png');
    echo $imgarr[0];
    $imagewithpath = $path."/".$imgarr[0];
    Mage::getSingleton('core/session')->setUploadimage($imagewithpath);       
    }
   
   
   
    public function upload($file_id, $folder="", $types="")
    {
    if(!$_FILES[$file_id]['name']) return array('','No file specified');

    $file_title = $_FILES[$file_id]['name'];
    //Get file extension
    $ext_arr = split("\.",basename($file_title));
    $ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension

    //Not really uniqe - but for all practical reasons, it is
    $uniqer = substr(md5(uniqid(rand(),1)),0,5);
    $file_name = $uniqer . '_' . $file_title;//Get Unique Name

    $all_types = explode(",",strtolower($types));
    if($types) {
        if(in_array($ext,$all_types));
        else {
            $result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any.
            return array('',$result);
        }
    }

    //Where the file must be uploaded to
    if($folder) $folder .= '/';//Add a '/' at the end of the folder
    $uploadfile = $folder . $file_name;

    $result = '';
    //Move the file from the stored location to the new location
    if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) {
        $result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; //Show error if any.
        if(!file_exists($folder)) {
            $result .= " : Folder don't exist.";
        } elseif(!is_writable($folder)) {
            $result .= " : Folder not writable.";
        } elseif(!is_writable($uploadfile)) {
            $result .= " : File not writable.";
        }
        $file_name = '';
       
    } else {
        if(!$_FILES[$file_id]['size']) { //Check if the file is made
            @unlink($uploadfile);//Delete the Empty file
            $file_name = '';
            $result = "Empty file found - please use a valid file."; //Show the error message
        } else {
            chmod($uploadfile,0777);//Make it universally writable.
        }
    }

    return array($file_name,$result);
    }