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.
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 | |
} |
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:
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.
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():
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.
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.
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.
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.
<?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 " " . $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.
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.
<?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'; | |
?> |
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)