$_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
Explore magento tutorials, magento guide, magento faqs, magento help, magento development, magento tips and tricks and everything related to magento.
Monday, October 1, 2012
Magento Get Product ID from SKU
Wednesday, August 29, 2012
How to reset Magento admin password
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
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
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
Print Collection Query
$collection->printLogQuery(true);
Sunday, August 19, 2012
Moving magento from subdirectory to root directory
- Move all files from sub-directory to root directory
- Change .htaccess file code:
Find RewriteBase /subdirectory/ to RewriteBase /
where subdirectory is the name of your subdirectory e.g. magento - 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';
Friday, August 10, 2012
All products from a category
{{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
{{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
{{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
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
/**
* @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?
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
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.
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.
- 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
<?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 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
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
Install guide for windows http://www.magentocommerce.com/wiki/general/installing_on_windows_with_xampp_and_wamp
Good video/pdf describing templates/ui http://www.magentocommerce.com/media/screencasts/designers-guide-1/view http://www.magentocommerce.com/design_guide/articles/how-magento-builds-content
Magento is build on the Zend framework. there are afew views on here, the MVC aspect is very similar to .net MVC http://framework.zend.com/
I found it very useful to try and write my own custom module: http://www.magentocommerce.com/wiki/how-to/create-payment-method-module http://activecodeline.net/writing-a-custom-module-in-magento-detailed-walktrough/
This book is pretty good - only 160 odd pages but overs some good area.
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/
Get Current Logged in Customer Info
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
- 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.