Magento get all URL paths

1- Get Store URL =>  $storeURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

2- Get Skin URL =>  $skinURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);

3- Get Media URL =>  $mediaURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

4- Get Direct Link => $directLink = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);

5- Get Secure Base URL =>  $secureBaseURL = Mage::getBaseUrl(Mage_Core_Model_Store::XML_PATH_SECURE_BASE_URL);

6- Get JS URL =>  $jsURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);

7- Get Site URL => $siteURL = Mage::helper(‘core/url’)->getHomeUrl();

8- Get Current Page URL => $currentPageURL = Mage::helper(‘core/url’)->getCurrentUrl();

how can I get URL path in static blocks


base url
{{base url=’yourstore/yourpage.phtml’}}
get store url
{{ store url = ‘yourpage.phtml’}}
get skin url
{{ skin url = ‘images/image.png’}}
get media url
{{ media url=’/image.png’}}

Magento get Category and Sub category details

When you are customize the magento site you may be want to get current category,root category and sub category details like category ID,name,…etc.Here is a simple example.

If you want to load known category details ,for a example want to load category id=10, details.That is like this.

How to solve this product price changed to zero issue

1-Go to the following directory.
app/design/frontend/[yourtheme]/catalog/products/view/

2-Just replace the options.phtml file with correct magento version.

3-Check the page, if not different clear magento all cache and check it.

If not success,(version 1.7)
1-open app/design/[your package]/[yourtheme]/template/catalog/product/view/options.phtml

2-Find this script,

3-Replace with,

Magento get order increment id

Magento get order increment id into the success.phtml page


$orderId = Mage::getSingleton(‘checkout/session’)->getLastRealOrderId();

This is another method


$_orderID = Mage::getModel(‘sales/order’)
->loadByIncrementId($this->getOrderId());

Magento get order increment id into admin panel


// Getting last id from all orders
$orders = Mage::getModel(‘sales/order’)->getCollection()
->setOrder(‘increment_id’,’DESC’)
->setPageSize(1)
->setCurPage(1);

$orderId = $orders->getFirstItem()->getEntityId();

Above method will not work, if there are multi store in a single magento setup.


$orders = Mage::getModel(‘sales/order’)->getCollection()
->setOrder(‘created_at’,’DESC’)
->setPageSize(1)
->setCurPage(1);

$orderId = $orders->getFirstItem()->getEntityId();

Note there is a different of
->setOrder(‘created_at’,’DESC’)

Magento get order details by order id

$order = Mage::getModel(‘sales/order’)->load($orderId);
$_grand = $order->getGrandTotal();
$customerName = $order->getCustomerName(); // or $order->getBillingAddress()->getName();
foreach ($order->getAllItems() as $itemId => $item)
{
// display details here
}

Magento execute custom query

Magento execute custom query to insert data

 

Execute custom query to fetch data


Getting data into phtml files/view files

Method-1

Method-2


Execute custom query to update data

Magento set, get and delete cookie data

How to magento access cookie data or create, read, and delete cookie in magento? Magento Mage_Core_Model_Cookie class can be use to get,delete and set cookie data.If you try to use normal php cookie to get data, sometimes not working.

Magento set cookie data/values

 

NOTE:name and value are required.Other parameters are optional and can be set as null.

Usage


Magento get all cookies data/values

Magento get specific cookie data/value


Magento delete cookie data/value

Usage

 

Magento reindexing problems

If you have magento store sure you have seen “There was a problem with reindexing process” error message.If you try to reindex from magento admin panel, all options are not reindexing.There are may be category flat data,product flat data and product prices reindex errors.Here I’ll describes how to solve this reindex problem in the magento store.

What are the reasons for magento reindexing error

  • The number of products
  • The number of stores
  • The apache/lighttpd timeout settings
  • There are references to deleted products in catalog_product_flat_% tables
  • The php.ini maximum execution time

Method-1

1-Backup the table “catalog_category_product_index” with “Disable foreign key checks” marked.

2-Drop that table from the database.

3-Open that mysql file of the table and remove the constraints at the bottom, except “SET FOREIGN_KEY_CHECKS=1;” .Then it look like this.

4-Save and execute that file in that database.

5-Then clear the magento all cache and do the reindexing process in the magento backend.

If not success, do the following Method-2.

Method-2

1-Increase your execution time value in the .htaccess file.

2-Create a file reindexing.php in the root.

 

3-Copy the following scripts,paste and save that file.

4-Then clear the magento all cache and run this file as http://mymagento.com/reindexing.php

Note:Sometimes it will take long time because it will reindexing all.If you have shell access do the following method to do this quickly.

php shell/indexer.php reindex all

Method-3

How to solve “An error occurred while saving the URL rewrite” or “cannot initialize indexer process” problems

1-Goto the magento root directory and goto var/locks directory and delete all the files under it.

2-Change the var/locks folder permission to 777.

3-Run the following mysql query in your database.

4-Clear the magento all cache and try to reindexing process in the magento backend.

Method-4

1-Log into the phpmyadmin and find “catalog_product_flat_%” tables.Those are

  • catalog_product_flat_1
  • catalog_product_flat_2
  • catalog_product_flat_3

2-Try to truncate/empty those tables.

3-Clear the magento all cache and try to reindexing process in the magento backend.

That’s all.If you have problems,ideas,tips about this put as comments below.

Add custom comment box to each product in Cart

1)  In your theme, for the file: template/checkout/cart.phtml
<th><?php echo $this->__(‘Comments’) ?></th>

2)  Template/checkout/cart/item/default.phtml Add a new column
<td class=”a-center”>
<textarea name=”cart[<?php echo $_item->getId() ?>][comments]” rows=”3″ cols=”20″><?php echo $_item->getItemcomment() ?></textarea>
</td>

3) The next step is to save the comment in DB, when customer update the cart. So add a new field ‘itemcomment’
in the tabel ‘sales_flat_quote_item’

4) app/code/core/Mage/Checkout/Model/Cart.php, to add some code to the function updateItems()
public function updateItems($data)
{
Mage::dispatchEvent(‘checkout_cart_update_items_before’, array(‘cart’=>$this, ‘info’=>$data));
foreach ($data as $itemId => $itemInfo)
{
$item = $this->getQuote()->getItemById($itemId);
if (!$item)
{
continue;
}
if (!empty($itemInfo[‘remove’]) || (isset($itemInfo[‘qty’]) && $itemInfo[‘qty’]==’0′)) {
$this->removeItem($itemId);
continue;
}
$qty = isset($itemInfo[‘qty’]) ? (float) $itemInfo[‘qty’] : false;
if ($qty > 0) {
$item->setQty($qty);
}
/* Start: Custom code added for comments */
if(!empty($itemInfo[‘comments’])) {
$write = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
# make the frame_queue active
$query = “UPDATE `sales_flat_quote_item` SET itemcomment = ‘”.$itemInfo[‘comments’].”‘ where item_id = $itemId”;
$write->query($query);
$item->setItemcomment($itemInfo[‘comments’]);
}
/* End: Custom code added for comments */
}
Mage::dispatchEvent(‘checkout_cart_update_items_after’, array(‘cart’=>$this, ‘info’=>$data));
return $this;
}
5)  Showing the comment in Admin -> View Order. Add a new function getItemcomment() to the file below:
app/code/core/Mage/Adminhtml/Block/Sales/Order/View/Items.php
public function getItemcomment($item)
{
$itemId = $item->getId();
$write = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
$query = “SELECT q.* FROM `sales_flat_order_item` o
LEFT JOIN `sales_flat_quote_item` q on o.quote_item_id = q.item_id
WHERE o.item_id = $itemId”;
# For older versions of Magento
/*      $query = “SELECT q.* FROM `sales_order_entity_int` o
LEFT JOIN `sales_flat_quote_item` q on o.value = q.entity_id
WHERE o.entity_id = $itemId AND o.attribute_id = 343″;       */
$res = $write->query($query);
while ($row = $res->fetch() ) {
if(key_exists(‘itemcomment’,$row)) {
echo nl2br($row[‘itemcomment’]);
}
}
}
6) To add the comments column to the items edit the .phtml file below:
app/design/adminhtml/default/default/template/sales/order/view/items.phtml

<tr class=”headings”>
<th><?php echo $this->helper(‘sales’)->__(‘Product’) ?></th>
<th><?php echo $this->helper(‘sales’)->__(‘Comments’) ?></th>
<th><?php echo $this->helper(‘sales’)->__(‘Item Status’) ?></th>

7) Adding Column with comments. app/design/adminhtml/default/default/template/sales/order/view/items/renderer/default.phtml
Add a column for item comments juts before status columns to make it look a like below.

8) <td><?php echo $this->getItemcomment($_item) ?></td>
<td class=”a-center”><?php echo $_item->getStatus() ?></td>

Magento URL Rewrite using config.xml

Magento URL Rewrite using config.xml

This post will helpful to do the rewrite url (source to destination) from config.xml file. Normally will do the the rewriting from old url to new url  (some think like fancy url) then we will go for Magento rewrite management or .htaccess change and will do our changes, once done we will get a fancy URL for our page.

Old Url :
http://www.mywebsite.com/mymodule/mycontroller/myaction/Param/paramvalue
Ex: http://www.mywebsite.com/customproduct/index/index/productid/10

If you see the above url, seems not like a fancy url, this url is easily explain the the mangento module and controller action of this page.So we are going to change the above url to fancy kind of url

New Url Fancy:
We are going to redirect old url to new url like below
Ex: http://www.mywebsite.com/customproduct/10

Config.xml

1. Open config.xml file from customproduct module
2. add below code inside <global> tag

Code:
<global>
<rewrite>
<mycontroller_index_index>
<from><![CDATA[/customproduct\/(.*)/]]></from>
<to><![CDATA[customproduct/index/index/productid/$1]]></to>
<complete>1</complete>
</mycontroller_index_index>
</rewrite>
……
……
……
</global>

3. Cleare cache and trigger old url on browser. The page redirect to new url as like above fancy kind of url.

How to Remove index.php from Magento URL

Once we installed magento, when you check any of the page URL (like: product detail page , category page, etc..) at the end of the url you may get index.php. This kind of url is basically not good for the SEO purpose. We have to remove from the URL. We need to convert the url FROM http://MYWEBSITE.com/index.php/customer/account/login/  TO  http://MYWEBSITE.com/customer/account/login/

Yes we have the option in magento for this,

1. First check “mod_rewrite” module is enabled on your server.If not then installed and restart the apache

2. admin – System – Configuration – Generat – Web – Search Engines Optimization – Change to “YES

3. admin – System – Configuration – Generat – Web – Secure – Use Secure URLs in Frontend “Yes“.

4. Modify .htaccess file as like below

  • <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

5. Incase magento store is installed in a subfolder , then try to use below code

  • RewriteEngine On
    RewriteBase /shop/
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /shop/index.php [L]

6. Clear cache

7. Done, index.php wont be display from the browser URL.