Magento Coding Solutions

get Collection oder by and limit In Magento


hear  mydesign: is custom module
$limit=10
 $start=0
$modelColl=Mage::getModel('mydesign/mydesign')->getCollection()->addFieldToFilter('user_id',$userId);
            $modelColl->setOrder('mydesign_id','DESC');
            $modelColl->getSelect()->limit($limit,$start);

orders product by oder_id in magento


======================
get orderproduct By oder_id in magento
============================

$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
        $items = $order->getAllItems();
                        $itemcount=count($items);
                                             
                        $date=date ("Y-m-d H:i:s");
                        foreach ($items as $itemId => $item)
                        {
                       
                            $product=Mage::getModel('catalog/product')->load($item->getProductId());
                      }

Get Gallery Images in Magento by Product Id

<?php
include "../app/Mage.php";
Mage::app();

$product_id=$_REQUEST['product'];
$product=Mage::getModel('catalog/product')->load($product_id);
$mediaImages=$product->getMediaGalleryImages();
foreach($mediaImages as $_image){
  
    ?>
     <li>
        <a href="<?php echo Mage::helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(364,418); ?>" title="<?php echo $_image->getLabel();?>" onclick="$('image').src = this.href; return false;">
            <img src="<?php echo Mage::helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(76,70); ?>" width="76" height="70" alt="<?php echo $_image->getLabel(); ?>" />          
            </a>
     </li>
    <?php
  
  
    }
    ?>

Add css ,js in xml in magento

<reference name="head">
                <action method="addCss">
                    <stylesheet>css/pagination.css</stylesheet>
                </action>

<action method="addCss"><stylesheet>directory/file.css</stylesheet></action>
<action method="addItem"><type>skin_js</type><name>js/myjavascript.js</name></action>
            </reference>

Pagination In Magento programmatic

Stape1. ...
<style>
ul.pagination {
font-family: "Arial", "Helvetica", sans-serif;
font-size: 13px;
height: 100%;
list-style-type: none;
margin: 20px 0;
overflow: hidden;
padding: 0; }
ul.pagination li.details {
background-color: white;
border-color: #C8D5E0;
border-image: none;
border-style: solid;
border-width: 1px 1px 2px;
color: #1E598E;
font-weight: bold;
padding: 8px 10px;
text-decoration: none; }
ul.pagination li.dot {
padding: 3px 0; }
ul.pagination li {
float: left;
list-style-type: none;
margin: 0 3px 0 0; }
ul.pagination li:first-child {
margin-left: 0; }
ul.pagination li a {
color: black;
display: block;
padding: 7px 10px;
text-decoration: none; }
ul.pagination li a img {
border: medium none; }
ul.pagination li a.current {
background-color: white;
border-radius: 0 0 0 0;
color: #333333; }
ul.pagination li a.current:hover {
background-color: white; }
ul.pagination li a:hover {
background-color: #C8D5E0; }
ul.pagination li a {
background-color: #F6F6F6;
border-color: #C8D5E0;
border-image: none;
border-style: solid;
border-width: 1px 1px 2px;
color: #1E598E;
display: block;
font-weight: bold;
padding: 8px 10px;
text-decoration: none; }
</style>
Stape2...

<?php  
    function pagination($per_page = 10, $page = 1, $url = '', $total){

        $adjacents = "2";

        $page = ($page == 0 ? 1 : $page);
        $start = ($page - 1) * $per_page;

        $prev = $page - 1;
        $next = $page + 1;
        $lastpage = ceil($total/$per_page);
        $lpm1 = $lastpage - 1;

        $pagination = "";
        if($lastpage > 1)
        {
        $pagination .= "<ul class='pagination'>";
        $pagination .= "<li class='details'>Page $page of $lastpage</li>";
        if ($lastpage < 7 + ($adjacents * 2))
        {
        for ($counter = 1; $counter <= $lastpage; $counter++)
        {
        if ($counter == $page)
        $pagination.= "<li><a class='current'>$counter</a></li>";
        else
        $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>";
        }
        }
        elseif($lastpage > 5 + ($adjacents * 2))
        {
        if($page < 1 + ($adjacents * 2))
        {
        for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
        {
        if ($counter == $page)
        $pagination.= "<li><a class='current'>$counter</a></li>";
        else
        $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>";
        }
        $pagination.= "<li class='dot'>...</li>";
        $pagination.= "<li><a href='{$url}$lpm1'>$lpm1</a></li>";
        $pagination.= "<li><a href='{$url}$lastpage'>$lastpage</a></li>";
        }
        elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
        {
        $pagination.= "<li><a href='{$url}1'>1</a></li>";
        $pagination.= "<li><a href='{$url}2'>2</a></li>";
        $pagination.= "<li class='dot'>...</li>";
        for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
        {
        if ($counter == $page)
        $pagination.= "<li><a class='current'>$counter</a></li>";
        else
        $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>";
        }
        $pagination.= "<li class='dot'>..</li>";
        $pagination.= "<li><a href='{$url}$lpm1'>$lpm1</a></li>";
        $pagination.= "<li><a href='{$url}$lastpage'>$lastpage</a></li>";
        }
        else
        {
        $pagination.= "<li><a href='{$url}1'>1</a></li>";
        $pagination.= "<li><a href='{$url}2'>2</a></li>";
        $pagination.= "<li class='dot'>..</li>";
        for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
        {
        if ($counter == $page)
        $pagination.= "<li><a class='current'>$counter</a></li>";
        else
        $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>";
        }
        }
        }

        if ($page < $counter - 1){
        $pagination.= "<li><a href='{$url}$next'>Next</a></li>";
        // $pagination.= "<li><a href='{$url}$lastpage'>Last</a></li>";
        }else{
        //$pagination.= "<li><a class='current'>Next</a></li>";
        // $pagination.= "<li><a class='current'>Last</a></li>";
        }
        $pagination.= "</ul>\n";
        }
        return $pagination;
    }


    $page=1;
    if(isset($_GET['page']) && $_GET['page']!=''){
    $page=$_GET['page'];
    }
    //echo pagination(10,$page,Mage::getBaseUrl('web').'mydesign?page=',200);
?>

Step3) mydesign is my custom module

    <form action='' name="myDesignSave" method="post" >
      $modelCount=Mage::getModel('mydesign/mydesign')->getCollection()->addFieldToFilter('user_id',$userId);
            $rows=count($modelCount);
           
            $page=1;//Default page
            $limit=1;//Records per page
            $start=0;//starts displaying records from 0
            if(isset($_GET['page']) && $_GET['page']!=''){
            $page=$_GET['page'];
            }
            $start=($page-1)*$limit;
           
           
           
                            $modelColl=Mage::getModel('mydesign/mydesign')->getCollection()->addFieldToFilter('user_id',$userId);
            $modelColl->getSelect()->limit($limit,$start);


foreach($modelColl as $value){


}



   </form>
<?php
        echo pagination($limit,$page,Mage::getBaseUrl('web').'mydesign?page=',$rows);
        //call function to show pagination
        ?>


choose an option how to change in magento

from path :
/app/code/core/Mage/Catalog/Block/Product/View/Type/Configurable.php and
/app/design/frontend/evia/default/template/catalog/product/view/type/options/configurable.phtml

get directory paths, get Base URLs, get URLs in static block, get secure URL in Magento



1) Get Directory paths

 Mage::getBaseDir()  //output : /var/www/html/magento
Mage::getBaseDir('app')  //output : /var/www/html/magento/app
Mage::getBaseDir('media') //output : /var/www/html/magento/media
Mage::getBaseDir(‘design’) => Get design directory path
Mage::getBaseDir(‘code’) => Gives code directory file path
Mage::getBaseDir(‘lib’) => Gives lib directory file path
Mage::getBaseDir(‘skin’) => Gives skin directory file path
Mage::getBaseDir(‘var’) => Gives var directory file path
Mage::getBaseDir(‘cache’) => Gives cache directory file path
Mage::getBaseDir(‘log’) => Gives log directory file path

 
 
 
2) Get Base URL
 Mage::getBaseUrl() => Get base url path e.g. http://yourwebsite.com/
 Mage::getBaseUrl('media') => Get MEDIA folder path e.g. http://yourwebsite.com/media/
 Mage::getBaseUrl('js') => Get JS folder path e.g. http://yourwebsite.com/js/
 Mage::getBaseUrl('skin') => Get SKIN folder path e.g. http://yourwebsite.com/skin/

 3) Get Current URL
Mage::helper('core/url')->getCurrentUrl();


4) Get Home URL
Mage::helper('core/url')->getHomeUrl();

5) Create module your
Mage::getUrl('module/controller/action');

6)  Get secure URL(To get secure URL you just need to add one more parameter as bellow)
Mage::getUrl('',array('_secure'=>true))
Mage::getUrl('module/controller/action',array('_secure'=>true))

 
 7) Get URL path in Static Block

 i) Get Base URL
{{base url=''}}

ii) Get Skin URL
{{skin url='images/skinimage.jpg'}} 
iii) Get Media URL
{{media url='/mediaimage.jpg'}}
iv) Get Store URL
{{store url=''}}   


How to make Renderer in custom module magento


1)managedesign is my custom module
2) /app/code/local/Managedesign/Managedesign/Block/Adminhtml/Managedesign/Grid.php

    call rander method:
    "design_id" ia a database attribut;
   
        $this->addColumn("design_id", array(
            "header" => Mage::helper("managedesign")->__("Link"),
            "index" => "design_id",
            'renderer'  => 'managedesign/adminhtml_managedesign_renderer_image'

                ));
               
3)crerate a folder Renderer and make a file Image.php
    /app/code/local/Managedesign/Managedesign/Block/Adminhtml/Managedesign/Renderer/Image.php
   
   
    class Managedesign_Managedesign_Block_Adminhtml_Managedesign_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract{
    
    public function render(Varien_Object $row)
    {
        $html = '<img width="105" height="15" ';
        $html .= 'id="' . $this->getColumn()->getId() . '" ';
        $html .= 'src="' . Mage::getBaseUrl('web').'customizer/designs/'.$row->getData($this->getColumn()->getIndex()) . '_FRONT.png"';
        $html .= 'class="grid-image ' . $this->getColumn()->getInlineCss() . '"/>';
       $html .= '<br/><p><a href="'.$row->getData($this->getColumn()->getIndex()).'">Downlode Design</a></p>';
        return $html;
    }
}

get customer information by user Id in magento


 $session=Mage::getSingleton('customer/session', array('name'=>'frontend'));
//get current login user Id
$userId=$session->getId();

$cusotmer = Mage::getModel('customer/customer')->load($uid);
            $to = $cusotmer->getEmail();

login information Magento

$from_email = Mage::getStoreConfig('trans_email/ident_general/email');
 $from_name = Mage::getStoreConfig('trans_email/ident_general/name');


$customerId=0;
if($customer = Mage::getSingleton('customer/session')->isLoggedIn()) { $customerData = Mage::getSingleton('customer/session')->getCustomer();  
echo  $customerId=$customerData->getId();
}

Resize Image in magento

 /****Image ReSize class in magento */

1) save this code as resize-class.php

<?php



   # ========================================================================#

   #

   #  Author:    Jarrod Oberto

   #  Version:     1.0

   #  Date:      17-Jan-10

   #  Purpose:   Resizes and saves image

   #  Requires : Requires PHP5, GD library.

   #  Usage Example:

   #                     include("classes/resize_class.php");

   #                     $resizeObj = new resize('images/cars/large/input.jpg');

   #                     $resizeObj -> resizeImage(150, 100, 0);

   #                     $resizeObj -> saveImage('images/cars/large/output.jpg', 100);

   #

   #

   # ========================================================================#





        Class resize

        {

            // *** Class variables

            private $image;

            private $width;

            private $height;

            private $imageResized;



            function __construct($fileName)

            {

                // *** Open up the file

                $this->image = $this->openImage($fileName);



                // *** Get width and height

                $this->width  = imagesx($this->image);

                $this->height = imagesy($this->image);

            }



            ## --------------------------------------------------------



            private function openImage($file)

            {

                // *** Get extension

                $extension = strtolower(strrchr($file, '.'));



                switch($extension)

                {

                    case '.jpg':

                    case '.jpeg':

                        $img = @imagecreatefromjpeg($file);

                        break;

                    case '.gif':

                        $img = @imagecreatefromgif($file);

                        break;

                    case '.png':

                        $img = @imagecreatefrompng($file);

                        break;

                    default:

                        $img = false;

                        break;

                }

                return $img;

            }



            ## --------------------------------------------------------



            public function resizeImage($newWidth, $newHeight, $option="auto")

            {

                // *** Get optimal width and height - based on $option

                $optionArray = $this->getDimensions($newWidth, $newHeight, $option);



                $optimalWidth  = $optionArray['optimalWidth'];

                $optimalHeight = $optionArray['optimalHeight'];





                // *** Resample - create image canvas of x, y size

                $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);

                imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);





                // *** if option is 'crop', then crop too

                if ($option == 'crop') {

                    $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);

                }

            }



            ## --------------------------------------------------------

           

            private function getDimensions($newWidth, $newHeight, $option)

            {



               switch ($option)

                {

                    case 'exact':

                        $optimalWidth = $newWidth;

                        $optimalHeight= $newHeight;

                        break;

                    case 'portrait':

                        $optimalWidth = $this->getSizeByFixedHeight($newHeight);

                        $optimalHeight= $newHeight;

                        break;

                    case 'landscape':

                        $optimalWidth = $newWidth;

                        $optimalHeight= $this->getSizeByFixedWidth($newWidth);

                        break;

                    case 'auto':

                        $optionArray = $this->getSizeByAuto($newWidth, $newHeight);

                        $optimalWidth = $optionArray['optimalWidth'];

                        $optimalHeight = $optionArray['optimalHeight'];

                        break;

                    case 'crop':

                        $optionArray = $this->getOptimalCrop($newWidth, $newHeight);

                        $optimalWidth = $optionArray['optimalWidth'];

                        $optimalHeight = $optionArray['optimalHeight'];

                        break;

                }

                return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);

            }



            ## --------------------------------------------------------



            private function getSizeByFixedHeight($newHeight)

            {

                $ratio = $this->width / $this->height;

                $newWidth = $newHeight * $ratio;

                return $newWidth;

            }



            private function getSizeByFixedWidth($newWidth)

            {

                $ratio = $this->height / $this->width;

                $newHeight = $newWidth * $ratio;

                return $newHeight;

            }



            private function getSizeByAuto($newWidth, $newHeight)

            {

                if ($this->height < $this->width)

                // *** Image to be resized is wider (landscape)

                {

                    $optimalWidth = $newWidth;

                    $optimalHeight= $this->getSizeByFixedWidth($newWidth);

                }

                elseif ($this->height > $this->width)

                // *** Image to be resized is taller (portrait)

                {

                    $optimalWidth = $this->getSizeByFixedHeight($newHeight);

                    $optimalHeight= $newHeight;

                }

                else

                // *** Image to be resizerd is a square

                {

                    if ($newHeight < $newWidth) {

                        $optimalWidth = $newWidth;

                        $optimalHeight= $this->getSizeByFixedWidth($newWidth);

                    } else if ($newHeight > $newWidth) {

                        $optimalWidth = $this->getSizeByFixedHeight($newHeight);

                        $optimalHeight= $newHeight;

                    } else {

                        // *** Sqaure being resized to a square

                        $optimalWidth = $newWidth;

                        $optimalHeight= $newHeight;

                    }

                }



                return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);

            }



            ## --------------------------------------------------------



            private function getOptimalCrop($newWidth, $newHeight)

            {



                $heightRatio = $this->height / $newHeight;

                $widthRatio  = $this->width /  $newWidth;



                if ($heightRatio < $widthRatio) {

                    $optimalRatio = $heightRatio;

                } else {

                    $optimalRatio = $widthRatio;

                }



                $optimalHeight = $this->height / $optimalRatio;

                $optimalWidth  = $this->width  / $optimalRatio;



                return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);

            }



            ## --------------------------------------------------------



            private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)

            {

                // *** Find center - this will be used for the crop

                $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );

                $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );



                $crop = $this->imageResized;

                //imagedestroy($this->imageResized);



                // *** Now crop from center to exact requested size

                $this->imageResized = imagecreatetruecolor($newWidth , $newHeight);

                imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);

            }



            ## --------------------------------------------------------



            public function saveImage($savePath, $imageQuality="100")

            {

                // *** Get extension

                $extension = strrchr($savePath, '.');

                   $extension = strtolower($extension);



                switch($extension)

                {

                    case '.jpg':

                    case '.jpeg':

                        if (imagetypes() & IMG_JPG) {

                            imagejpeg($this->imageResized, $savePath, $imageQuality);

                        }

                        break;



                    case '.gif':

                        if (imagetypes() & IMG_GIF) {

                            imagegif($this->imageResized, $savePath);

                        }

                        break;



                    case '.png':

                        // *** Scale quality from 0-100 to 0-9

                        $scaleQuality = round(($imageQuality/100) * 9);



                        // *** Invert quality setting as 0 is best, not 9

                        $invertScaleQuality = 9 - $scaleQuality;



                        if (imagetypes() & IMG_PNG) {

                             imagepng($this->imageResized, $savePath, $invertScaleQuality);

                        }

                        break;



                    // ... etc



                    default:

                        // *** No extension - No save.

                        break;

                }



                imagedestroy($this->imageResized);

            }





            ## --------------------------------------------------------



        }

?>
2)include("resize-class.php");

$resizeObj = new resize($newfile);
    $regular = Mage::getBaseDir() . DS .'images'. DS .'backgrounds'. DS .$time."_regular.".$extension;
    $thumbnail = Mage::getBaseDir() . DS .'images'. DS .'backgrounds'. DS .$time."_small.".$extension;
    $resizeObj -> resizeImage(500, 480, 'crop');
    $resizeObj -> saveImage($regular, 100);
    $resizeObj -> resizeImage(75, 75, 'crop');
    $resizeObj -> saveImage($thumbnail, 100);                //$post_data['swf']='swf/swf/'.$filename;