Browsing articles in "Magento"
Nov 7, 2014
kalpesh

Magento: Get real IP address behind a proxy

Get real IP address if your server or customer is behind a proxy network. Magento have function Mage::helper(‘core/http’)->getRemoteAddr(); to get client IP address, but it gives proxy IP address instead of real IP if there is proxy network in between. Below code checks and returns both real IP and proxy IP address if it founds any.

1
2
3
4
5
6
7
8
9
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    echo $_SERVER['HTTP_CLIENT_IP'];
} else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    echo trim($ips[count($ips) - 1]); //real IP address behind proxy IP
    // echo $_SERVER['REMOTE_ADDR']; //proxy IP address
} else {
    echo $_SERVER['REMOTE_ADDR']; //no proxy found
}
Nov 7, 2014
kalpesh

Magento error: SCP 404: GET /spp/ajax/?___SID=Uco/?id=460&pid=123

If you are using Magento extension Simple Configurable Product (SCP http://www.magentocommerce.com/magento-connect/simple-configurable-products.html), it gets dangerous sometimes when you have configured Special Prices or Tiered Pricing. It requests for prices via ajax after page is loaded. For some customers having old system and browser, this results in sending ___SID (session ID) along with GET request parameter, which causes double question mark in the URL.

Ideally, the ajax URL should be:

1
http://ma.gen.to/spp/ajax/co?id=460&pid=123

where it requests to module’s AjaxController and coAction() method, with parameters id (product ID) and pid (parent ID). This should go good without any issue.

1
http://ma.gen.to/spp/ajax/?___SID=Uco/?id=460&pid=123

But, ___SID=U, which is used in the cache as a placeorder, also goes along with the parameters it creates a problem. As there are two “?” in the URL, Magento will try to see for AjaxController’s indexAction() because when you don’t have anything for action part in URL, it by defaults to indexAction of the controller.

The problem doesn’t end in just a 404 error in error log, this also DISPLAYS whole 404 page just below the product price in product page. So, customer will get confused as there is a big 404 page inside the product page and configurable attributes like size, color and quantity box are pushed down. This may result in low sales when it appears frequently.

The easiest way to fix this is to create an action indexAction() and check if you have ___SID as a parameter, then just ignore it.

Add the following code in your module’s AjaxController.php file, just above the coAction():

1
2
3
4
public function indexAction() {
    $p = $this->getRequest()->getParam('___SID');
    if($p) { return ''; }
}

This will solve the weird 404 page that gets displayed in product page, but not the 404 error coming up in the logs. You need to remove ?___SID=U from the URL when it appears in the request to completely resolve the issue.

Nov 6, 2014
kalpesh

Magento: Get all attribute values (colors, sizes, etc..)

Magento get all the attribute values you have in the store. Get all the colors and sizes values in Magento. Change the attribute name to whatever you want values for, in the below script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
require_once('app/Mage.php');
umask(0);
Mage::app('admin');
//set_time_limit(0);
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');
$colors = array();
foreach ($attribute->getSource()->getAllOptions(true) as $option) {
        $colors[] = $option['label'];
}
sort($colors);
foreach($colors as $lbl) {
    echo "<span style='background-color:lightgrey'>".$lbl . "</span>";
        echo "<br>";
}

Replace color with any attribute you want to get values for. Above script also sorts the result set in alphabetic order to make it easy to view all values. If you don’t want to sort the values, just comment out the line which sorts it.

Nov 6, 2014
kalpesh

Magento: Get all category and subcategory products

Magento get all category and subcategory products which are assigned to categories at different levels. Below script will show you all the categories, and all the associated product names under each of those categories.

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
require_once('app/Mage.php');
umask(0);
Mage::app('admin');
set_time_limit(0);

$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();

$ids = $tree->getCollection()->getAllIds();

if ($ids)
{
     foreach ($ids as $id)
  {
     $cat = Mage::getModel('catalog/category');
     $cat->load($id);
     if($cat->getLevel()==3 && $cat->getIsActive()==1)
     {
        $category1 = Mage::getModel('catalog/category')->load($cat->getId());
        $products = Mage::getResourceModel('catalog/product_collection')
                                ->addAttributeToSelect('name')
                             ->addCategoryFilter($category1);
        echo "<b>".$cat->getName()."</b><br>";
        foreach ($products as $product) { //print_r($product->getData());exit;
                echo " &nbsp; &nbsp; &nbsp; " . $product->getName() . " - ". $product->getSku() . "<br/>";
        }
     }
  }
}
?>

Note the line which checks category getLevel()==3, you can change this line to get different subcategory levels by adjusting it.
For root category, getLevel() should be 1.
For all the main/primary categories, getLevel() should be 2.
For all the subcategories, getLevel() should be 3.
For all the subsubcategories, getLevel() should be 4.
and so on…

Above script will get you all the category and subcategories products, products assigned to each and every categories of your store.

Nov 6, 2014
kalpesh

Magento: Get all products with quantities and Out Of Stock

Magento get all the simple products which have greater than 0 quantity and are still Out of Stock in inventory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
require_once('app/Mage.php');
umask(0);
Mage::app('admin');
set_time_limit(0);

$productCollection = Mage::getModel('catalog/product')
     ->getCollection()
     ->addAttributeToSelect('*')
     ->joinField('qty',
                 'cataloginventory/stock_item',
                 'qty',
                 'product_id=entity_id',
                 '{{table}}.is_in_stock=0',
                 'left')
     ->addAttributeToFilter('qty', array("gt" => 0));

echo "<h2>Simple Products with >0 quantity and Out of Stock</h2>";
foreach($productCollection as $product) { //print_r($product->getData());exit;
    if($product->getTypeId() == 'simple')
        echo $product->getName() . " | " . $product->getSku() . "<br>";
}
echo 'Done';
?>
Pages:1234567...26»

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