Monday 14 May 2012

Useful Functions for Magento Collection


There are different important functions that you can implement in your Collection object. The functions are present in Varien_Data_Collection_Db class. The class file is present in lib/Varien/Data/Collection/Db.php

Here are some of the functions that you can use in your collection object:-

/**
* Get Zend_Db_Select instance
*/
$collection->getSelect();

/**
* Get collection size
*/
$collection->getSelect()->getSize();

/**
* Get sql select string or object
*/
$collection->getSelect()->getSelectSql();

/**
* Add select order
*/
$collection->getSelect()->setOrder($field, $direction);

/**
* Add field filter to collection
*
* If $attribute is an array will add OR condition with following format:
* array(
* array('attribute'=>'firstname', 'like'=>'test%'),
* array('attribute'=>'lastname', 'like'=>'test%'),
* )
*/
$collection->getSelect()->setOrder($field, $condition);

/**
* Set select distinct
*/
$collection->getSelect()->distinct($flag);

/**
* Get all data array for collection
*/
$collection->getSelect()->getData();

/**
* Reset loaded for collection data array
*/
$collection->getSelect()->resetData();

/**
* Print and/or log query
*/
$collection->getSelect()->printLogQuery(true, true);

More functions below:-

Varien_Data_Collection_Db class extends Varien_Data_Collection class. Here are some more functions present in Varien_Data_Collection class:-

/**
* Get current collection page
*/
$collection->getSelect()->getCurPage();

/**
* Retrieve collection last page number
*/
$collection->getSelect()->getLastPageNumber();

/**
* Retrieve collection page size
*/
$collection->getSelect()->getPageSize();

/**
* Retrieve collection all items count
*/
$collection->getSelect()->getSize();

/**
* Retrieve collection first item
*/
$collection->getSelect()->getFirstItem();

/**
* Retrieve collection last item
*/
$collection->getSelect()->getLastItem();

/**
* Retrieve collection items
*/
$collection->getSelect()->getItems();

/**
* Clear collection
*/
$collection->getSelect()->clear();


Hope this helps. Thanks.

Saturday 12 May 2012

Get More Traffic to Your Site - Avoid the Top 10 SEO Mistakes


In years of Internet marketing and scrutiny of the SEO tactics of the web, I've found this list of today's most common mistakes that lead to a site's poor performance. In most cases, simply NOT doing these things will result in a more profitable online business... Hey, sometimes it's not what you do, it's what you don't do! Enjoy and benefit from these tips.


1. NUMBER OF KEYWORDS PER PAGE - You may well be over working your SEO labors. If you are optimizing more than one target keyword per page, than you in all probability are. Keeping your targeted keywords to one per page will probably get you better results. In some cases you can do two, but you honestly should keep it to one if possible.


2. URL HAS NO KEYWORDS - You can't always determine the domain name of a client. And maybe the company brand is before now well-known and is comprised of no keyword phrases, but changing the company name is not a possibility. You could possibly, in this case, be able to get a new domain with search phrases in it and point it to the company's website.


3. TITLE TAGS WITHOUT KEYWORDS - If you have set your company trademark, mission statement or some other really cool thing in the title tag, and it doesn't hold any search phrases, you've missed a huge opportunity to optimize that webpage. Keywords are more essential to search engines than your company motto, no matter how likable or cool it may be.


4. META TAGS NOT FULLY UTILIZED - There are Meta "keywords", and then there's the Meta "description". You should use both to convey to the search engines like Google what your focus is. With the "description", compress keyword terms and phrases and company slogans or tag lines here. With the Meta "keywords", put in the exact keywords you want to use, but keep the amount down to under 8 or 10 keyword terms.


5. KEYWORD DENSITY IS TOO DENSE - Keyword stuffing is lousy, and if you cram your keyword phrases into every single possible opening on the page, internet search engines might consider that you are spamming and punish your rankings. It's best to keep your words and phrases density to less than 5%. Typically 2-3% is fine.


6. TOO MUCH REDUNDANCY BACK-LINK ANCHOR TEXT - If you're applying the back-links with keyword terms and phrases in your anchor text, you're doing far more than most, but if you are simply putting one or two keyword phrases into these back-links, you're hurting your cause. Diversify your anchor text. Use 5 or 10 keywords and phrases and distribute them out properly throughout the Internet for more effective outcomes.


7. WORSE THAN THE PREVIOUS MISTAKE, HAVING NO KEYWORDS IN YOUR ANCHOR TEXT - If you will be going to the trouble to write articles and blogs and you are attaching in plenty of back links to your site, good. However, if your back links are not employing keyword phrases as anchor text, you’re not obtaining a fraction of the effect you could.


8. BACK LINKS, TOO MUCH TOO FAST - Don't get too enthusiastic about your blogs and articles and just bathe the Internet with back links. You have to compile your back links at a more usual rate of say 50 or 60 a week. This will prevent the search engine's thinking you are a automated bot.


9. LINK SOURCE VARIETY - Be positive that you exploit as many resources as possible when Creating back links. Putting all of your eggs in one basket tips off the internet search destinations that you may perhaps be a spammer seeking for a short cut to high rankings.


10. KEYWORD RESEARCH - None of this is important if you are employing the wrong keyword terms and phrases in the first place. Make it a point to devote time researching your keywords. Be confident that you are using keywords that will convert into business and that they are not too aggressive for your budget.


Avoid these 10 common mistakes, and you'll be ahead of most of the competition.


Hope this will help !!!!! :)

Friday 11 May 2012

How to select, insert, update, and delete data in Magento?


Here, I will be showing how to select, insert/add, update/edit and delete data in the Magento way. It’s about implementing the CRUD (Create Read Update Delete) concept. :)

Suppose, I have a database table named ‘news‘ with the following fields:-

id : int, auto increment, primary key
title : varchar
content : text
status : 0 or 1

Suppose, I have a module named ‘mynews‘. Here follows the code to select, insert, update, and delete data from the ‘news‘ table.

INSERT DATA

$data contains array of data to be inserted. The key of the array should be the database table’s field name and the value should be the value to be inserted.
$data = array('title'=>'hello there','content'=>'how are you? i am fine over here.','status'=>1);
$model = Mage::getModel('mynews/mynews')->setData($data);
try {
$insertId = $model->save()->getId();
echo "Data successfully inserted. Insert ID: ".$insertId;
} catch (Exception $e){
echo $e->getMessage();
}


SELECT DATA

$item->getData() prints array of data from ‘news’ table.
$item->getTitle() prints the only the title field.
Similarly, to print content, we need to write $item->getContent().
$model = Mage::getModel('mynews/mynews');
$collection = $model->getCollection();
foreach($collection as $item){
print_r($item->getData());
print_r($item->getTitle());
}


UPDATE DATA

$id is the database table row id to be updated.
$data contains array of data to be updated. The key of the array should be the database table’s field name and the value should be the value to be updated.
// $id = $this->getRequest()->getParam('id');
$id = 2;
$data = array('title'=>'hello test','content'=>'test how are you?','status'=>0);
$model = Mage::getModel('mynews/mynews')->load($id)->addData($data);
try {
$model->setId($id)->save();
echo "Data updated successfully.";

} catch (Exception $e){
echo $e->getMessage();
}


DELETE DATA

$id is the database table row id to be deleted.
// $id = $this->getRequest()->getParam('id');
$id = 3;
$model = Mage::getModel('mynews/mynews');
try {
$model->setId($id)->delete();
echo "Data deleted successfully.";

} catch (Exception $e){
echo $e->getMessage();
}


In this way you can perform select, insert, update and delete in your custom module and in any magento code.

Hope this helps. Thanks.

How to clear the cache using code in Magento?


If you have made any modifications to your Magento store, they will not appear immediately unless you clear the cache.
Magento keeps its cache in /var/cache located inside the Magento installation directory. So, for example, if your Magento is installed in your main public_html folder, the cache will be in public_html/var/cache.
To clear the cache, simply delete everything from the /var/cache directory and then reload your website in your browser.
To clear all sessions, you can also delete everything from the /var/session folder within your Magento installation directory.
To clear cache in Magento using code, you can use one of some methods below:
Clear all cache files of Magento by the command: rm -rf var/cache/*

Clear by Magento’s code
Clear all frontend caches:
Mage::app()->cleanCache();

Clear cache by tag:
$tags = array("CONFIG");
Mage::app()->cleanCache($tags);


Clear all caches:
Mage::app()->getCacheInstance()->flush();

Hope this will help you !!!! :)

Thursday 10 May 2012

Add Featured Products to Home Page in Magento


This article will show you an easy way to display products of your choosing on the home page.

1. Create a category to contain the featured products. Call it e.g. “Featured” or “Home Page”. Set “Is Active” to No. That way, it won’t display in the catalog menu.
2. After saving the category, please note what ID it gets. You can see it in the last part of the URL. If the URL ends with catalog_category/edit/id/8/, the ID is 8.
3. Add products for the home page to the new category.
4. Edit the Home Page (CMS → Manage Pages → Home Page) and add the following content, where 8 should be replaced by your category ID:

{{block type="catalog/product_list" category_id="8" template="catalog/product/list.phtml"}}

If you want a view that is different from the category lists, you can copy and modify list.phtml and change the path above.

Please note: In order to make this work for more than one block of products with different category IDs on the same page, you need to add the following code at the end of your phtml file(s):