Wednesday, December 23, 2009

How to show new products on home page?

There are many ways to show products on home page.

Go to "CMS - Manage Pages" and select "Home Page" from the list of pages.

Use this code to show products as "New Products" on your home page:

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

(Note that you must have some new products in your catalogue for anything to show when you do this. In this context new doesn’t mean that you’ve recently added them; only products explicitly marked as new using ‘Set Product as New from Date’ and ‘Set Product as New to Date’ options in the ‘General’ product information page in the admin tool will be shown.)

Sunday, December 13, 2009

Code to get the Categories list in magento.

Below code can be used to get the categories tree in magento

require_once $_SERVER[‘DOCUMENT_ROOT’]."/app/Mage.php";
Mage::app(’1’);

function nodeToArray(Varien_Data_Tree_Node $node)
{
$result = array();
$result[’category_id’] = $node->getId();
$result[’parent_id’] = $node->getParentId();
$result[’name’] = $node->getName();
$result[’is_active’] = $node->getIsActive();
$result[’position’] = $node->getPosition();
$result[’level’] = $node->getLevel();
$result[’children’] = array();

foreach ($node->getChildren() as $child) {
$result[’children’][] = nodeToArray($child);
}

return $result;
}
function load_tree() {

$tree = Mage::getResourceSingleton(’catalog/category_tree’)
->load();
$store = 1;
$parentId = 1;
$tree = Mage::getResourceSingleton(’catalog/category_tree’)
->load();

$root = $tree->getNodeById($parentId);

if($root && $root->getId() == 1) {
$root->setName(Mage::helper(’catalog’)->__(’Root’));
}

$collection = Mage::getModel(’catalog/category’)->getCollection()
->setStoreId($store)
->addAttributeToSelect(’name’)
->addAttributeToSelect(’is_active’);

$tree->addCollectionData($collection, true);
return nodeToArray($root);

}

function print_tree($tree,$level) {
$level ++;
foreach($tree as $item) {
echo str_repeat("    ", $level).$item[’name’]."<br>";
print_tree($item[’children’],$level);
}
}

$tree = load_tree();
print_tree($tree[’children’],0);

Saturday, November 28, 2009

Use of getModel and getData methods

getModel

See the code below:
$model = Mage::getModel(‘catelog/product’);
Let me explain the things, basically we have created an instance of Mage_Catelog_Model_Product class.

This means that our $model variable is actually an instance of Mage_Catalog_Model_Product class. This matches the class name contained inside the app/code/core/Mage/Catalog/Model/Product.php file. Inside this Product.php file we can clearly see that Mage_Catalog_Model_Product class extends the Mage_Catalog_Model_Abstract class. What this means is that our $model can use all of the methods (functions) declared inside the Mage_Catalog_Model_Product class plus those declared inside the Mage_Catalog_Model_Abstract class.

To iterate and echo out the available method names like
echo ‘<ul>’;
foreach (get_class_methods(get_class($model)) as $method) {
echo ‘<li>’ . $method . ‘</li>’;
}
echo ‘</ul>’;

Now you can see lot of userful methods like getName, getPrice, getTypeId, getStatus which we can execute on our $model variable like

echo ‘Product name: ‘ . $model->getName();
echo ‘Product price: ‘ . $model->getPrice();

The above will not show anything, what you have to do is call the load function as follows before above code.

$model->load(13); 

getData

getData is just one of multiple available methods for $model. getData can be executed with or without any parameters passed to it. If you execute it without any parameters you get the array variable as a result.

echo ‘<pre>’;
print_r($model->getData());
echo ‘</pre>’;

Now if you want to get product name or price, you can use getData method as below.

echo $model->getData(’sku’);
or
$modelData = $model->getData();
echo $modelData->sku;

Now from above discussion you come to know how to use getModel and getData methods.

Monday, October 19, 2009

Simple steps to create a custom module.

Creating custom modules is one of the important part of magento. Its allow you to insert functionality anywhere, whether in a “static” block fashion that’s more than static, or a shipping/payment module, or large module.

We will start with a very basic example.
Step 1.
First allow magneto to know about your new module. Create a xml file at app/etc/modules/Unique_All.xml. Notice _All, it indicates that you can declare all your modules here.

<?xml version="1.0"?>
<config>
<modules>
<Unique_Example>
<active>true</active>
<codePool>local</codePool>
</Unique_Example>
</modules>
</config>

Step 2.
Now create a configuration file for your new module. app/code/local/Unique/Example/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
<Unique_Example>
<active>true</active>
<codePool>local</codePool>
</Unique_Example>
</modules>
</config>

Step 3.
Create block code. It doesn't do anything but show some functionality.

<?php
class Unique_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;
}
}
?>

Step 4.
Create template (phtml) file.
app\design\frontend\default\unique\template\example\view.phtml

<div>
<span><strong>This is the output of the unique example:</strong></span><br /><span style="color:#FF9933;">
<?php
echo $this->receiveMessage();
?>
</span>
</div>

Yours custom module is ready to use.
In CMS page add below code, where you want to use.

{{block type="unique_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)

<reference name="right">
<block type="unique_example/view" my_custom="Test" template="example/view.phtml" />
</reference>

Magento requirement and installation guidelines.

Requirements
Magento requires PHP 5.2.x or above to run properly.

Installation
The steps which you must follow in order to complete the Magento installation are:

Step 1: Download the latest stable Magento package from: http://www.magentocommerce.com/download

Step 2: Upload the package on your hosting account through your cPanel -> File Manager or using an FTP client.

There are 2 options where you can place the Magento download file:
Either under the public_html folder, which is the main one for your web site content. Then your web site will be accessible directly through your domain.
Or under a subfolder, for example if your domain name is called yourdomainname.com and you decide to install the script in the public_html/magento folder, then you will open the web site at: http://yourdomainname.com/magento. Once you upload the package, you can extract it through your cPanel -> File Manager. Using the same tool, please change the file permissions for the Magento files to 755.

Step 3: Create a MySQL database and assign a user to it through cPanel -> MySQL Databases. Remember the database details, since you will need them during the script installation.
Step 4: In our example we will install Magento in the public_html/magento folder. Once the package is uploaded and extracted and you have a MySQL database, navigate to http://yourdomainname.com/magento:
Click on the check box next to "I agree to the above terms and conditions" and click on the [Continue] button.

Step 5: Choose the preferred Time Zone, Locale and Currency:
Click on the [Continue] button.

Step 6: Enter the database details: Database Name, User Name and User Password. You can leave the other options intact. Click on the check box next to "Skip Base URL validation before next step".
Click on the [Continue] button.

Step 7: At this point you should enter the personal information and the admin login details which you want to use. You can leave the Encryption Key field empty and the script will generate one for you.
Click on the [Continue] button.

Step 8: Write down your encryption key; it will be used by Magento to encrypt passwords, credit cards and other confidential information.

Well done! Your Magento installation was successfully completed. Now you can navigate to its Frontend or Backend.

What is magento?

Magento is an open-source ecommerce web application launched on march 31, 2008. It was created by Varien, building on the component of the Zend Frameword.

It handle all aspects of building and maintaining an ecommerce site including shopping carts, shipping, product reviews, and much more.

Monday, July 27, 2009

E-Commerce Models

1) Business-to-Business (B2B) Model
2) Business-to-Consumer (B2C) Model
3) Consumer-to-Consumer (C2C) Model
4) Consumer-to-Business (C2B) Model

What is e-commerce?

The form of commerce, which is conducted via an electronic media, is called electronic commerce or e-commerce.