Monday, October 1, 2012

Magento Get Product ID from SKU

Load magento product by its sku. This is the fastest way to get product by sku.
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);

Wednesday, August 29, 2012

How to reset Magento admin password

If you need to change your magento admin password, go to phpMyAdmin, select your Magento database, click the SQL tab and paste this query:

UPDATE admin_user SET password=CONCAT(MD5('sGnewpass'), ':sG') 
WHERE username='AdminUsername';


You have to change newpass in the MD5('sGnewpass') with your new password, and change 'AdminUsername' to your Magento admin username.

Execute the query and your password will be changed.

Tuesday, August 28, 2012

Enable Search Engine Friendly URLs in Magento

To enable Search Engine Friendly URLs in Magento, you have to log in to the Magento admin area and click on the Configurations button. Under the System navigation menu, go to Web page from the sub-navigation panel on the left.

Click on the Search Engines Optimization tab and turn on the Use Web Server Rewrites (mark as Yes). Click on the Save Config button and your Magento SEF URLs will be enabled.

Saturday, August 25, 2012

Remove Estimate Shipping and Tax box

If you need to remove the Estimate Shipping and Tax from shopping cart page on magento, follow these instructions:

Open /app/design/frontend/YOUR_PACKAGE/YOUR_THEME/template/checkout/cart.phtml file

Find the line that mentions shipping

Delete it and save file.

Clear cache and refresh page to see result.

Friday, August 24, 2012

Magento Print Collection Query

There come several situations where you need to print the collection query in magento and here is the way how to print collection query:

Print Collection Query

$collection->printLogQuery(true);


Sunday, August 19, 2012

Moving magento from subdirectory to root directory

Moving magento from sub directory to root directory require following steps:

  1. Move all files from sub-directory to root directory
  2. Change .htaccess file code:
    Find RewriteBase /subdirectory/ to RewriteBase /
    where subdirectory is the name of your subdirectory e.g. magento
  3. Open phpMyAdmin and run these queries:
    update core_config_data set value='your new store url' where path = 'web/unsecure/base_url';
    update core_config_data set value='your new store url' where path = 'web/secure/base_url';
Note: your new store url must be a url i.e. http://google.com/ with trailing slash.

Friday, August 10, 2012

All products from a category

Here is the way to show all products from a specific magento category to home page.


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

Show All Products on Home Page

This is very common now a days to show all products on magento home page. Here is the code to achieve this:

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

Thursday, August 9, 2012

Magento Display New Products on Home Page

There are many ways to show new products on home page but this is the simple way to display new products on home page.

{{block type="catalog/product_new" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"}}

Monday, August 6, 2012

URL is not accessible - Unable to read response, or response is empty

Sometimes during magento installation an error occurs saying "URL is not accessible - Unable to read response, or response is empty"

The simplest solution is to Click on "Skip Base URL Validation" and that test is ommitted at that point and it will work fine.

Wednesday, July 18, 2012

Add Product to Cart with Price Change

If you want to add a product to the cart after change its price, then you have to write an Observer that listens the checkout_cart_product_add_after or checkout_cart_update_items_after event. The code is same except checkout_cart_product_add_after is called for only one item and checkout_cart_update_items_after is called for all items in the cart.


/**
* @param Varien_Event_Observer $observer
*/
public function myDiscount(Varien_Event_Observer $observer)
{
$item = $observer->getQuoteItem();
if ($item->getParentItem()) {
$item = $item->getParentItem();
}

//discount 20% off
$discount = 0.20;

// Check if the discount isn't applied over and over while refreshing
$specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $discount);

if ($specialPrice > 0) {
$item->setCustomPrice($specialPrice);
$item->setOriginalCustomPrice($specialPrice);
$item->getProduct()->setIsSuperMode(true);
}
}

Saturday, June 30, 2012

Retrieve Magento Products with Attribute Value

Magento uses EAV Model to retrieve products, so you'll need to add additional attributes that you want to return.


$collection = Mage::getModel('catalog/product')->getCollection();
//fetch name and orig_price into data
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');

The following shows how to filter by a range of values (greater than AND less than):


$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');
//filter for products whose orig_price is greater than (gt) 50
$collection->addFieldToFilter(array(array('attribute'=>'orig_price','gt'=>'50'),));
//AND filter for products whose orig_price is less than (lt) 100
$collection->addFieldToFilter(array(array('attribute'=>'orig_price','lt'=>'100'),));

While this will filter by a name that equals one thing OR another.


$collection = Mage::getModel('catalog/product')->getCollection();
//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection->addFieldToFilter(array(array('attribute'=>'name','eq'=>'Widget A'), array('attribute'=>'name','eq'=>'Widget B'),));

A full list of the supported short conditionals (eq,lt, etc.) can be found in the _getConditionSql method in lib/Varien/Data/Collection/Db.php

Finally, all Magento collections may be iterated over (the base collection class implements on of the the iterator interfaces). This is how you'll grab your products once filters are set.



$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');
//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection>addFieldToFilter(array(array('name'=>'orig_price','eq'=>'Widget A'),array('name'=>'orig_price','eq'=>'Widget B'),));
foreach ($collection as $product) {
var_dump($product->getData());
}

Solution by Alan Storm

Friday, June 29, 2012

Why is Magento so slow?

These are some of the reasons behind the slowness of magento:
  1. Parts of Magento use an EAV database system implemented on top of MySQL. This means querying for a single "thing" often means querying multiple rows

  2. There's a lot of things behind the scenes (application configuration, system config, layout config, etc.) that involve building up giant XML trees in memory and then "querying" those same trees for information. This takes both memory (storing the trees) and CPU (parsing the trees). Some of these (especially the layout tree) are huge. Also, unless caching is on, these tree are built up from files on disk and on each request.

  3. Magento uses its configuration system to allow you to override classes. This is a powerful feature, but it means anytime a model, helper, or controller is instantiated, extra PHP instructions need to run to determine if an original class file or an override class files is needed. This adds up.

  4. Besides the layout system, Magento's template system involves a lot of recursive rendering. This adds up.

In general, the Magento Engineers were tasked, first and foremost, with building the most flexible, customizable system possible, and worry about performance latter.

The first thing you'll want to do to ensure better performance is turn caching on (System -> Cache Management). This will relive some of the CPU/disk blocking that goes on while Magento is building up its various XML trees.

The second thing you'll want to do is ensure your host and/or operations team has experience performance tuning Magento.

This was the answer by Alan Storm.

Friday, May 18, 2012

Get Current Store Information

To get information about the current store in Magento


<?php
$store = Mage::app()->getStore();

//Get Current Store Name
$storeName = $store->getName();

//Get Current Store ID
$storeName = $store->getStoreId();

//Get Current Store Code
$storeName = $store->getCode();

//Get Current Store Website ID
$storeName = $store->getWebsiteId();

//Check if the current store is active
$storeName = $store->getIsActive();

//Get Current Store Home URL
$storeName = $store->getHomeUrl();

?>

Tuesday, April 10, 2012

How to remove wishlist link?

If you need to disable wishlist completely then go to system -> configurations -> wishlist, under General Options there would be drop down with option Yes change it to No

If you need to disable wishlist from files then these are the layout files you need to modify in your theme: layout/checkout.xml, layout/customer.xml, layout/wishlist.xml

Thursday, February 9, 2012

Magento in Sub Directory

If you want to install magento in sub directory or sub folder of your domain i.e. yourdomain.com/store or yourdomain.com/magento etc

Edit your magento htaccess located in sub folder. Find the following line:

#RewriteBase /magento/

Remove the # sign and write your folder name instead of /magento/

It will work in this way to install magento in sub direcotry.

Tuesday, January 10, 2012

Starting Magento Development

Dev Environment

I am using Komodo and DreamWeaver for development.

I did try the Zend Studio as an IDE (Magento is based on the Zend Framework), and the SVN plugin for Eclipse, but didn't find either particularity enjoyable to use. I have generally found there is a little more friction than we are used to with Visual Studio. It's often easier to get things done outside of the IDE. With Magento you have to create a copy of a file you want to override and put it in a different folder, so you spend a lot of time messing around in the file system, outside of the IDE.

References

The Magento forums are rubbish I'm afraid - no one replies to anything. It has taken me hours to figure some little things out. I would just say stick at it, the code can appear to be very fragmented, but as you get used to it you will appreciate how modular and extensible it is.

Good Blogs

http://www.westwideweb.com/wp/category/magento/

http://inchoo.net/category/ecommerce/magento/

http://activecodeline.net/

http://www.exploremagento.com/category/magento

Get Current Logged in Customer Info

Check if the customer is logged in then

Mage::getSingleton('customer/session')->isLoggedIn();


Get Current Customer Name

Mage::getSingleton('customer/session')->getCustomer()->getName();


You can Get Current Customer All Information

echo "<pre>"; print_r(Mage::helper('customer')->getCustomer()->getData()); echo "</pre>";


Ger Current Logged in Admin

Mage::getSingleton('admin/session')->getUser();

Monday, January 2, 2012

Magento Products Are Not Showing

Sometimes your added products from admin are not displaying at frontend magento store. These products dont list in categories either. In order to avoid this situation, follow these steps:

  • The products must be Visible in Catalog*.
  • The products must be Enabled.
  • Product must have a stock Quantity.
  • The product must be set to In Stock.
  • The product must be assigned to the target Category (if you want that product to list in that specific category).
  • If using multi-website mode (or if you imported the products through Data Flow), the products must be assigned to the target Website.
  • You must clear your Cache/Indexes, just to make sure.

*You may set Visible in Catalog/Search to display in Search as well.