Friday, March 13, 2015

Display new products on the home page Magento

This tutorial will show you how to display new products on the home page in Magento using the Magento block.
  • Open Magento admin panel
  • Go to CMS>Pages>Home page
  • Switch to the content tab
  • Insert the following code:

{{block  type="catalog/product_new" column_count="4"  products_count="12" name="home.catalog.product.new"  alias="product_homepage"  template="catalog/product/new.phtml"}}
 
 
You may clone the new.phtml from
 \app\design\frontend\base\default\template\catalog\product


Thank you.


Thursday, March 12, 2015

How to Set Up Virtual Hosts For Magento at localhost

 Web developer often need to work into vitual host using live domain for  some reason.


Que : If you want to  run the license extensions into localhost, extension will not work.

Solution: Create VirtualHost with same license domain name & perform your work as best.

How you will do it ? follow the following few steps.

Step 1

Go to "C:/Windows/System32/drivers/etc" and open the "hosts" file in Notepad++ or notepad.

File will seem like below.


# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1  

Add following line at the end file.


127.0.0.1   www.xyz.com   #your site name


Step 2

Open httpd.conf file from "D:\xampp\apache\conf\httpd.conf"
or you can open it directly from XAMPP control panel

Search following line

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf


Remove # (comment ) from #Include conf/extra/httpd-vhosts.conf


Step 3

Go to "C:\xampp\apache\conf\extra\httpd-vhosts.conf"

Add following code at the end of file.


<Directory C:/xampp/htdocs>
Order Deny,Allow  
Allow from all
</Directory>

After the directory code you just added, add following code too:

<VirtualHost *:80>  
DocumentRoot "C:/xampp/htdocs/magento19"
ServerName www.xyz.com
</VirtualHost>


Save all and Restart the Apache.

Step4

Open your browser and enter  "www.xyz.com" into address bar, or whatever your domain name is.


working ? Yes!!!  Enjoy!!!

:( not working ? comment your query here....















Wednesday, March 11, 2015

Add Store View selection to your Module’s Adminhtml

While developing an extension recently I came across the need to add the ability to allow changes on a store view level.

To start off with I wanted to make sure this functioned the same as the built store view selection, to make it easy on the end user (take a look at how store view is handled with CMS Page creation to see what I’m referring to). However, by default Magento keeps its store view information in seperate tables (ie CMS_PAGE_STORE), which wasn’t ideal for this scenerio. The solution I came up with involves using one field ‘store_id’ in your modules table to save the Store ID’s as comma seperated values.

In your module’s adminhtml edit/create form (<Namespace>_<Module>_Block_Adminhtml_<Model>_Edit_Tab_Form)
add the following code in the _prepareForm function where you would like the store selection to show up:

if (!Mage::app()->isSingleStoreMode()) {
    $fieldset->addField('store_id', 'multiselect', array(
        'name' => 'stores[]',
        'label' => Mage::helper('')->__('Store View'),
        'title' => Mage::helper('')->__('Store View'),
        'required' => true,
        'values' => Mage::getSingleton('adminhtml/system_store')
                     ->getStoreValuesForForm(false, true),
    ));
}
else {
    $fieldset->addField('store_id', 'hidden', array(
        'name' => 'stores[]',
        'value' => Mage::app()->getStore(true)->getId()
    ));
}
 
(This adds the store view multiselect to your form if you have multiple stores setup, if not it adds a hidden field with the current stores id taking into consideration the possibility of more stores being added in the future.)

Next in your module’s Controller file (<Namespace>_<Module>_Adminhtml_Controller)
add the following to your saveAction:

if(isset($data['stores'])) {
    if(in_array('0',$data['stores'])){
        $data['store_id'] = '0';
    }
    else{
        $data['store_id'] = implode(",", $data['stores']);
    }
   unset($data['stores']);
}
 
(This sets store_id to 0 if “All Store Views” was selected or sets store_id as comma-seperated values representing the store selection.)
In your modules Grid file (<Namespace>_<Module>_Block_Adminhtml_<Model>_Grid),
add the following:

protected function _prepareCollection(){
    $collection = Mage::getModel('_/')->getCollection();
    foreach($collection as $link){
        if($link->getStoreId() && $link->getStoreId() != 0 ){
            $link->setStoreId(explode(',',$link->getStoreId()));
        }
        else{
            $link->setStoreId(array('0'));
        }
    }
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
 
(This parses the collections store_id’s and resets them as an Array, taking into consideration any that might not be assigned yet or any with a value of 0, which represents ‘All Store Views’)

In the grids prepareColumns function add:

if (!Mage::app()->isSingleStoreMode()) {
    $this->addColumn('store_id', array(
        'header'        => Mage::helper('')->__('Store View'),
        'index'         => 'store_id',
        'type'          => 'store',
        'store_all'     => true,
        'store_view'    => true,
        'sortable'      => true,
        'filter_condition_callback' => array($this,
            '_filterStoreCondition'),
    ));
}
 
(If you have multiple stores setup this will add the “Store View” column with the store selection drop down, if no column is added as its not needed.)
and add the following function:

protected function _filterStoreCondition($collection, $column){
    if (!$value = $column->getFilter()->getValue()) {
        return;
    }
    $this->getCollection()->addStoreFilter($value);
}
 
(This function checks to see if a store filter has been selected and if so calls the function to add the filter to the collection.)
Lastly in your modules Collection file(<Namespace>_<Module>_Model_Mysql4_<Model>_Collection) add this function:

public function addStoreFilter($store, $withAdmin = true){

    if ($store instanceof Mage_Core_Model_Store) {
        $store = array($store->getId());
    }

    if (!is_array($store)) {
        $store = array($store);
    }

    $this->addFilter('store_id', array('in' => $store));

    return $this;
}

Thursday, March 5, 2015

Change Fonts and Reduce File Size for PDF Invoice in Magento

We often meet a situation where we need to print an Order Invoice for a particular customer.

If you ever noticed, the size of a PDF Invoice file is roughly 1.5Mb. It is not a problem if you only need to download a single file.
But in a situation where we need to download 10, 20 or 50 files in one go; the total download size will increase dramatically.
This becomes a headache especially when you need to get these files urgently.

We found a solution to reduce the size of PDF Invoice files. The reason why the PDFs are so large by default is because magento is using the following font

/lib/LinLibertineFont/LinLibertineC_Re-2.8.0.ttf

which is attached to every file. This font file by itself is 1.5MB. We can replace these fonts with Zend_Pdf default fonts. This way we will get smaller file size for our PDFs. In order to do this, just open the file:

app/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php

find lines

$font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertineC_Re-2.8.0.ttf');

and replace them with

$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

The font of Pdf files will be as readable, though not as beautiful as before. And the size of file will be reduced by minimum 5 times.

If you want to increase the text size on the PDF Invoice for billing and shipping addresses, it can be done by modifying exact same file. Simply change the lines:

protected function _setFontRegular($object, $size = 7)
protected function _setFontBold($object, $size = 7)
protected function _setFontItalic($object, $size = 7)
to
protected function _setFontRegular($object, $size = 9)
protected function _setFontBold($object, $size = 9)
protected function _setFontItalic($object, $size = 9)

accordingly.

As you can see we simply increased the default size value. In order to preserve your modifications from future Magento upgrades you need to copy:

app/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php
into
app/local/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php

and enter your changes there.

Welcome for your queries!!