Category Archives: Magento

Magento

How to show out of stock products to the end of the product list in Magento

First of all overwrite the file

1
app/code/core/Mage/Catalog/Model/Layer.php

and copy this file to

1
app/code/local/Mage/Catalog/Model/Layer.php

In function getProductCollection(), Put this code after line #102

1
2
3
<?php
$collection->joinField('inventory_in_stock', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id','is_in_stock>=0', 'left')->setOrder('inventory_in_stock', 'desc');
?>

How to add categories to product by product sku programmatically in Magento

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
<?php
$rootDir = ""; //root path
include($rootDir."app/Mage.php");
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::app("default");
$baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

// $productsSkus is an array of the products you want to modify.
// Create it however you want, I did it like this...
$productsSkus = array('100246','800649','017261','006403');

//update product categories
for($i=0;$i<count($productsSkus);$i++)
{
$productModel = Mage::getModel('catalog/product');
$productId = $productModel->getIdBySku($productsSkus[$i]);
$product = $productModel->load($productId);
$categoryArr = $product->getCategoryIds();
// Array of category_ids to add.
$categoryArr[] = 842;
$product->setCategoryIds(array($categoryArr));
$product->save();

}
?>

Hope you will enjoy!

How to get customer name, customer email on success.phtml/order confirmation page in Magento

Use the foloowing code to get customer information on success.phtml/order confirmation page given below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
if($order->getCustomerId() === NULL)
{
$customerName = $order->getBillingAddress()->getName();
$customerEmail = $order->getBillingAddress()->getEmail();
}
else
{
$customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
$customerName = $order->getCustomerName();
$customerEmail = $order->getCustomerEmail();
}
?>

How to get total quantity of product sold in Magento

Use following code given below:

1
2
3
4
5
6
7
8
9
10
<?php
$id = 100001; // Enter your product ID here
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToFilter('id', $id)
->setOrder('ordered_qty', 'DESC')
->getFirstItem();
$product = $_productCollection;
echo 'Total Sold'.$product->ordered_qty;
?>

How to disable Google Checkout button in Magento

Use the following steps to disable google checkout button from cart page as given below:

Login into Admin first then

1
Steps 1: Go to System->Configuration
1
Steps 2: Click on "Gopple API" under "Sales Tab" from left side.
1
Steps 3: Click on "Google Checkout" and select Enable "No".
1
Steps 4: Click on "Save Config" button at the top.

Sort products by 2 attributes in Magento

Sort products by 2 attributes in magento

In magento site suppose you have the requirement to sort the product listings by 2 attributes like “in stock” and “popularity”

After some research, it seems to work.

Use the following code given below:

1
2
3
<?php
$Collection->setOrder(array('attribute1', 'attribute2'), asc); //the setOrder() method accepts an array.
?>

Modified this line in Toolbar.php in the Catalogue/Product/List/ directory.

1
2
3
4
5
6
<?php
 if ($this->getCurrentOrder()) 
 {
    $this->_collection->setOrder(array($this->getCurrentOrder(), 'in stock'), $this->getCurrentDirection());
 }
?>

Hope this will work!

Enable template path hint in Magento

To enable magento path hint for your magento website

1
Login into admin section and go to System->Configuration->Advanced->Developer

Inside this, click on “Debug” option.

If the Current Configuration Scope is “Default” (left top), you will be not able to set template path hints for your magento website.

So you have to select the magento website from the dropdown and then change the template Path Hints to “yes” .

Now your template path hint has been enabled. You can view template path hint on the frontend of your website.

if some how due to cache you are not getting the path hint.

Then go to System->Cache management and “flush” or “refresh” the cache.

Magento: Difference between order states and statuses

If you are building website in Magento, you may have noticed that there are two columns insales_flat_order table which are confusing. These are state and status. You might think what is the difference between these two, both having same meaning.Well, this is not the case. They both are different. State is used by magento to tell if the order is new, processing, complete, holded, closed, canceled, etc.; while Statuses are the one that YOU would be defining at the backend in System -> Order Statuses. Magento displays order STATUSES and not STATES in the backend order detail page to let you know which status is assigned as per your mapping. Remember, multiple statuses can be mapped with one state, while vice versa is not possible.Consider an example, your customer places an order as Cash on Delivery, you will need something like COD_Pending as the order status so that you know it is not yet paid. Magento will have state new for this, which makes you unpredictable of what kind of transaction is this, COD or Prepaid. The STATUS can be anything, as you define, for your understanding; while STATE is something which Magento needs to understand, internally.

In short, Magento uses order state internally for processing order, whereas order status are used by store owners to understand the exact order flow where one state can be assigned to multiple statuses.

To understand this in detail, have a look at this

How to create categories tree structure in Magento programmatically

This is simple example to get nested category list. We can easily create a left navigation and a drop-down menu with the Retrieved HTML. Follow the code :

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
<?php

set_time_limit(0);
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors",'On');

$rootDir = ""; // Root Directory Path
include($rootDir."app/Mage.php");
Mage::app("default");

$rootcatId= Mage::app()->getStore()->getRootCategoryId(); // get default store root category id
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId); // else use default category id =2

function show_categories_tree($categories) {
$array= '<ul>';
foreach($categories as $category) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
$count = $cat->getProductCount();
$array .= '<li>'.
'<a href="' . Mage::getUrl($cat->getUrlPath()). '">' .
$category->getName() . "(".$count.")</a>\n";
if($category->hasChildren()) {
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
$array .= show_categories_tree($children);
}
$array .= '</li>';
}
return $array . '</ul>';
}
echo show_categories_tree($categories);

?>

How to add/update category in magento from csv programmatically

The following code given below is used to add/update category from the .csv 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
34
35
36
37
38
39
<?php

set_time_limit(0);
ini_set('memory_limit','1024M');
require_once '../../app/Mage.php';
Mage::app();//read data from csv file
$row = 1; $cat_arr = array();
if (($handle = fopen('category.csv', 'r')) !== FALSE)
{
while (($data = fgetcsv($handle, 100000, ',')) !== FALSE)
{
if($row > 1)
{

/* $data[0] is csv column in csv file
$data[1],$data[2],$data[3],$data[4] is category Id column */

$cat_arr[trim($data[0])] = $data[1].','.$data[2].','.$data[3].','.$data[4].','.$data[5].','.$data[6].','.$data[7]; // $data[0] column represent sku

}

$row++;
}
fclose($handle);
}

$products = Mage::getResourceModel('catalog/product_collection');
$i = 1 ;
foreach ( $products as $index => $productModel )
{
$_product = Mage::getModel('catalog/product')->load($productModel->getId());
$cnids = $cat_arr[$_product->getSku()];
$ids = explode(',',$cnids);
$_product->setCategoryIds($ids);
$_product->save();
$i++;
}

?>