Tuesday, May 31

How to Show Product Reviews on Product View Page in Magento

This is a fairly straightforward tutorial on how to bring the product reviews list and the review form out on the product page. In this example I’ll be adding it to view.phtml

1) Add form data to catalog.xml & view.phtml

The first thing to do is to add the review blocks to the correct XML reference in catalog.xml which is in app > design > frontend > default > yourtheme > layout

At about line 198 you’ll see the for the catalog_product_view class. Inside this reference add the following code:

<block type="review/product_view_list" name="product.info.product_additional_data" as="product_review" template="review/product/view/list.phtml">
  <block type="review/form" name="product.review.form" as="review_form"/>
 </block>

Now the XML is set up, you just need to echo it on the product page in app > design > frontend > default > yourtheme > template > catalog > product > view.phtml

<?php echo $this->getChildHtml('product_review') ?>

Hope it Helps. Thanks

If you feel any difficulties while setting the magento store feel free to contact us

Saturday, May 28

How to get CMS Page ID in Magento

Here is the code to get CMS Page ID in Magento

function getCurrentCmsPage() {
$pageId = Mage::getBlockSingleton('cms/page')->getPage()->getIdentifier();
return $pageId;
}


Hope it Helps... Thanks..

How to Show the Products in Magento Home Page

In magento admin go to CMS-> Manage Pages. then click the home page and add the following code to the "content" tab.

Here is the code to get all Product in Magento Home Page

{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" template="catalog/product/list.phtml"}}

Here is the code to get products in specific category in mageno home page
{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" category_id="4" template="catalog/product/list.phtml"}}

Hope it Helps. Thanks...

How to get Customer Billing & Shipping Address in Magento

Here is the script to get Customer Billing and Shipping Address

$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
if ($customerAddressId){
       $address = Mage::getModel('customer/address')->load($customerAddressId);
}
else
{
 $address = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
}

Hope it Helps .. Thanks....

Monday, May 16

Magento Product Thumbnail Image Switcher

Here we We will see how to easily replace the default functionality of your product images (the pop-up window that displays when you click on a thumbnail below the main image) with a image swapping functionality.

So what we want to do is get rid of the default action and make the thumbnail change the larger main product image above when clicked.

Step 1:
Locate the file we are going to be working with. It can be found at:
app/design/frontend/default/default/template/catalog/product/view/media.phtml



Step 2:

Locate the code that we will be editing (on or around line 72 in media.phtml) and replace it with the replacement code below. Thats it!

Original Code:
<a href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image) ?>', 'gallery', 'width=300,height=300,left=50,top=50,location=no,status=yes,scrollbars=yes,resizable=yes'); return false;">
   <!--nested img tag stays the same-->
</a>


Replacement Code
<a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" title="<?php echo $_product->getName();?>" onclick="$('image').src = this.href; return false;">
   <!--nested img tag stays the same-->
</a>




Hope it Helps! Thanks..

Monday, May 9

How to get Current URL in Magento

Here is the code to get current page url in magento

$currentUrl = $this->helper('core/url')->getCurrentUrl();

// Gives the base url of your magento installation
$baseUrl = Mage::getBaseUrl();

// Gives the url of media directory inside your magento installation
$mediaUrl = Mage::getBaseUrl('media');
Another way to get current url
$urlRequest = Mage::app()->getFrontController()->getRequest();
$urlPart = $urlRequest->getServer('ORIG_PATH_INFO');
if(is_null($urlPart))
{
    $urlPart = $urlRequest->getServer('PATH_INFO');
}
$urlPart = substr($urlPart, 1 );
$currentUrl = $this->getUrl($urlPart);

Hope it Helps! Thanks..

Sunday, May 8

How to get Most Viewed products in Magento

Here We will see how to get most viewed Products in Magento

here is the code:

public function getMostViewedProducts()
{
    /**
     * Number of products to display
     * You may change it to your desired value
     */
    $productCount = 5;
 
    /**
     * Get Store ID
     */
    $storeId    = Mage::app()->getStore()->getId();      
 
    /**
     * Get most viewed product collection
     */
    $products = Mage::getResourceModel('reports/product_collection')
        ->addAttributeToSelect('*')
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addViewsCount()
        ->setPageSize($productCount);
 
    Mage::getSingleton('catalog/product_status')
            ->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')
            ->addVisibleInCatalogFilterToCollection($products);
 
    return $products;
}

Hope it Helps! Thanks..

How to get Top Rated Products in Magento

Here We will see how to get top rated products in Magento

Here is the function to get the top rated products:

/**
 * Get Top Rated Products
 *
 * @return array
 */
public function getTopRatedProduct() {
 
    $limit = 5;
 
    // get all visible products
    $_products = Mage::getModel('catalog/product')->getCollection();
    $_products->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
    $_products->addAttributeToSelect('*')->addStoreFilter();           
 
    $_rating = array();
 
    foreach($_products as $_product) { 
 
        $storeId = Mage::app()->getStore()->getId();
 
        // get ratings for individual product
        $_productRating = Mage::getModel('review/review_summary')
                            ->setStoreId($storeId)
                            ->load($_product->getId());
 
        $_rating[] = array(
                     'rating' => $_productRating['rating_summary'],
                     'product' => $_product
                    );
    }
 
    // sort in descending order of rating
    arsort($_rating);
 
    // returning limited number of products and ratings
    return array_slice($_rating, 0, $limit);
}

Now, we can display the products in any template file. Here is the sample code to display the top rated products:-

I suppose that you have put the getTopRatedProduct() function in helper class of your module
<?php $topRatedProducts = $this->helper('yourModule')->getTopRatedProduct();?>
 
<ul>
<?php foreach($topRatedProducts as $_rating): ?>
    <?php $_product = $_rating['product']; ?>
<li>
    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(150); ?>"  alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
        <span>View Details</span>
    </a>
</li>
</ul>
<?php endforeach; ?>

Hope it Helps! Thanks..

How to Track Visitor´s Information in Magento

now we will see how to track the visitors information in magento. visitors in formations means we can track visitors ip address,browser, visit time and visitor id, etc.

The following scriptfetches the vistor data

$visitorData = Mage::getSingleton('core/session')->getVisitorData();
 
// printing visitor information data
echo "
"; print_r($visitorData); echo "
";

now we will get an array of visitor information data

Array
(
[] =>
[server_addr] => 154784124
[remote_addr] => 258974125
[http_secure] =>
[http_host] => 127.0.0.1
[http_user_agent] => Mozilla/4.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10
[http_accept_language] => en-US,en;q=0.8
[http_accept_charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.3
[request_uri] => /magentotalks/index.php/contacts/
[session_id] => 13qm5u80238vb15lupqcac97r5
[http_referer] => http://127.0.0.1/magentotalks/
[first_visit_at] => 2011-06-14 10:12:03
[is_new_visitor] =>
[last_visit_at] => 2011-06-17 11:54:18
[visitor_id] => 51
[last_url_id] => 149
)

In the above array, the server_addr and remote_addr are in different form than usual. The (IPv4) Internet Protocol dotted address has been converted into a proper address using ip2long PHP function. You can get the same kind of value from the following code:-

// user's ip address (visitor's ip address)
$remoteAddr = Mage::helper('core/http')->getRemoteAddr(true);
 
// server's ip address (where the current script is)
$serverAddr = Mage::helper('core/http')->getServerAddr(true);
 
Ref : http://blog.chapagain.com.np/magento-track-visitors-information/ 

Saturday, May 7

How to create a custom module in magento

Here is the steps to create the module in Magento

Step: 1

Inform Magento that you have a custom module.
app/etc/modules/Fido_All.xml Notice the _All in the xml file name. I can declare all of my modules here.
<?xml version="1.0"?>
<config>
<modules>
<fido_example>
<active>true</active>
<codepool>local</codePool>
</Fido_Example>
</modules>
</config>

Step: 2

Need to create directories as necessary.
app/code/local/Fido/Example/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
<fido_example>
<version>0.1.0</version>
</Fido_Example>
</modules>
<global>
<blocks>
<fido_example>
<class>Fido_Example_Block</class>
</fido_example>
</blocks>
</global>
</config>

I have informed Magento of my module version (it’s an arbitrary version). Version matters when you set up your module to be update-able. (A newer version will inform Magento to run the update files if you have them).
I have also informed Magento that my module contains block files which are found in fido/example/block. My class name will have “Fido_Example_Block”. If you want to see the many possibilities of stuff that goes in here, check out Mage config files (such as Catalog/etc/config.xml). You’ll also see other xml files in there. Those explanations are for another post.

Step: 3

Here is my block code. It doesn’t really do anything, but shows some functionality.
app\code\local\Fido\Example\Block\View.php

<?php
/**
* Example View block
*
* @codepool   Local
* @category   Fido
* @package    Fido_Example
* @module     Example
*/
class Fido_Example_Block_View extends Mage_Core_Block_Template
{
private $message;
private $att;

 
protected function createMessage($msg) {
$this->message = $msg;
}

 
public function receiveMessage() {
if($this->message != '') {
return $this->message;
} else {
$this->createMessage('Hello World');
return $this->message;
}
}

 
protected function _toHtml() {
$html = parent::_toHtml();

 
if($this->att = $this->getMyCustom() && $this->getMyCustom() != '') {
$html .= '
'.$this->att;
} else {
$html .= '
No Custom Attribute Found';
}

 
return $html;
}
}

The function receiveMessage() just returns “Hello World”
The function _toHtml() is responsible for outputting the template associated with the block. Make sure you run paret::_toHtml and return it as part of any other items returned in that function!

Step: 4
Here we create our template (phtml) file.
app\design\frontend\default\fido\template\example\view.phtml

<?php
 
/**
* Fido view template
*
* @see Fido_Example_Block_View
*
*/
?>
<div>
<span><strong>This is the output of the fido example:</strong></span>
<span style="color:#FF9933;">
<?php
echo $this->receiveMessage();
?>
</span>
</div>

This just outputs some HTML and also runs the receiveMessage() function from our block (view.php).

Two caveats here. By placing our view.phtml file in it’s location, we have created our own theme. You must make sure that

a) Magento knows about your theme (Admin->System->Design)
and

b) If you use the this block in a CMS page, you set the CMS page to use your theme (Admin->CMS->Manage Pages->’Your Page’->Custom Design->Custom Theme drop down)
You’re custom module is now ready for use.
In a cms page, add this to your content:

{{block type="fido_example/view" my_custom="Test" template="example/view.phtml" }}

Or something like this in the Layout Update XML area (Custom Design area in a CMS page)





//this will add your block in the right column
Now you can successfully have your block and the Hello World message being displayed (on your CMS page).


Hope it Helps! Thanks..

Sunday, May 1

How to get product price excluding tax in Magento

In Magento default price shown including Tax.

If you want price excluding Tax then you need to call the following Helper class .

<?php echo $this->helper('checkout')->formatPrice($_product->getPrice())
// For Special Price write this
echo $this->helper('checkout')->formatPrice($_product->getSpecialPrice()) ?> 

Hope it Helps.... Thanks!!!!