Browsing articles in "Magento frontend"
Jul 12, 2013
kalpesh

Magento convert quote item to order item

If you have custom product attribute(s), then you may need to convert them to quote item and then to order item in Magento when an order is placed. That helps the order get the product attributes details and saves it in order tables to reference in the backend Manage Orders screen. This allows you to know what values for the product is selected by the customer, so that you can consider it when dispatching the order items.

This is a two way process where first the product attribute converts to quote item, and then quote item (with your product attribute’s value) converts to order item.

To do this you will have to put the below code in your module’s config.xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<global>
    <!--convert your custom product attribute "myattribute" from quote item to order item-->
    <fieldsets>
        <sales_convert_quote_item>
            <myattribute>
                <to_order_item>*</to_order_item>
            </myattribute>
        </sales_convert_quote_item>
    </fieldsets>

    <!--add your custom product attribute "myattribute" to quote item-->
    <sales>
        <quote>
            <item>
                <product_attributes>
                    <myattribute></myattribute>
                </product_attributes>
            </item>
        </quote>
    </sales>

    <!--convert product attribute to quote item through event observer-->
    <events>
        <sales_quote_item_set_product>
            <observers>
                <mymodule>
                    <class>mymodule/observer</class>
                    <method>convertAttribute</method>
                </mymodule>
            </observers>
        </sales_quote_item_set_product>
    </events>
</global>

Continue reading »

Jun 29, 2013
kalpesh

Magento get store information

Magento get store information from any page.

1
$store = Mage::app()->getStore(); //Mage_Core_Model_Store object

Now to get store ID,

1
$storeID = $store->getStoreId();

To get store code,

1
$storeCode = $store->getCode();

To get store Website ID,

1
$websiteID = $store->getWebsiteId();

To get store name,

1
$storeName = $store->getName();

To get Default View store name,

1
$storeName = $store->getFrontendName();

To check if store is active or not,

1
$active = $store->getIsActive();

To get store’s Home URL,

1
$storeHomeURL = $store->getHomeUrl();

If you want to find store’s group name,

1
$storeGroupName = $store->getGroup()->getName();

If you want ADMIN Store information, admin store has ID 0. So, you can get the details of Admin Store like this:

1
$store = Mage::getModel('core/store')->load(0);

and then getting all the information as shown above.

May 26, 2013
kalpesh

Magento filter products by status

Get all the products with status equal to enabled/disabled.

If you are using Flat Catalog in Magento, you will not get disabled products by this filter as in flat table all the products are only enabled one. You can check if your Magento website uses flat catalog or not by going here:
Admin > System > Configuration > Catalog section > Catalog > Frontend

Check Use Flat Catalog Category and Use Flat Catalog Product, if they are Yes it means you are using flat catalog, if they are No it means you are NOT using flat catalog.

So, if you don’t have flat catalog enabled and you want to filter all the products with status equal to disabled, you can use below code:

1
2
3
4
5
6
7
8
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*') //whatever attributes you want to get here
->addAttributeToFilter(
    'status',
    array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED) 
        //replace DISABLED to ENABLED for products with status enabled
);
May 26, 2013
kalpesh

Magento get subcategories of a category by category ID

Get all the subcategories of any category in Magento.

If you have a category ID (current category OR any category) then you can easily get all the subcategories of that particular category.

Below code will load all the subcategories of the current category. You can even get all the subcategories of any specific category by assigning category ID to $catID.

1
2
3
4
5
6
7
8
$catID = $current_category->getId(); //or any specific category id, e.g. 5
$children = Mage::getModel('catalog/category')->getCategories($catID);
foreach ($children as $category) {
        echo $category->getId();
        echo $category->getName();
        echo $category->getRequestPath();
        print_r($category->getData());
}
May 26, 2013
kalpesh

Magento remove index.php from URL

In Magento remove index.php from URL using below steps. It will convert your URL from

http://www.example.com/index.php/blah

to

http://www.example.com/blah

1.) In Magento Admin > System > Configuration > Web > Search Engine Optimization, change the value of Web Server Rewrites to Yes

Make sure your web server rewrite module is enabled. If you are using apache as your web server on Linux, you can check it by going to /etc/apache2/mods-enabled/rewrite.load, if rewrite.load is present there it means your rewrite module is enabled. If not, you will need to copy rewrite.load from /etc/apache2/mods-available/ and paste it at /etc/apache2/mods-enabled/ location. Then reload the apache by running service apache2 reload.

2.) Check the file permission of .htaccess, it should be present in Magento root directory and readable by server.

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications