Browsing articles tagged with "magento Archives - Page 3 of 21 - Kalpesh Mehta"
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.

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

<?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';
?>
Nov 6, 2014
kalpesh

Magento: Get all products with 0 quantity and In Stock

Magento get all products which have zero quantity and are still In Stock in inventory. Below script will show you all such simple products.

<?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=1',
                 'left')
     ->addAttributeToFilter('qty', array("eq" => 0));

echo "<h2>Simple Products with 0 quantity and In Stock</h2>";
foreach($productCollection as $product) { //print_r($product->getData());exit;
    if($product->getTypeId() == 'simple')
        echo $product->getName() . " | " . $product->getSku() . "<br>";
}
echo 'Done';
?>
Nov 6, 2014
kalpesh

Magento: Get all products without categories (orphaned products)

Magento get all the configurable/simple products which are not associated with any categories, which are orphaned products. To get all the products (regardless of their type), simply ignore the condition where it checks for type_id in the below query and comment the condition line in foreach loop.

Below script will get all such products, you can create new PHP file at the root of Magento installation and paste the code:

<?php
require_once('app/Mage.php');
umask(0);
Mage::app('admin');
set_time_limit(0);

$i=0;
$sql = "select
    type_id,sku
 from catalog_product_entity a
 left join catalog_category_product cp on cp.`product_id` = a.entity_id
 left join catalog_product_relation cpr on cpr.child_id = a.entity_id
 where
       cp.product_id is null
   and cpr.parent_id is null
   and a.type_id = 'configurable'";
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
//echo count($connection->fetchAll($sql));exit;

foreach ($connection->fetchAll($sql) as $arr_row) {
        $pid = $arr_row['sku'];
        $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$pid);
        if($product->getTypeId()!='configurable') continue;
        $i++;
        echo $i .") ". $product->getName() . " - " . $product->getSku() . "<br>";
}
?>
Oct 7, 2014
kalpesh

Magento fix for error “Code already exists.”

Fix for Magento error Code already exists. in Sales > Tax > Manage Tax Rules, when saving the rule.

When trying to save Tax rules in Magento, you may get “Code already exists.” error if there are lots of tax rates passing in POST. Magento error is not making sense here, as the issue is something different. Basically when you post the data in PHP, it limits maximum post variables which should pass to the server. For me, that limit was 1000 in max_input_vars, which by changing to 20000 solved this issue and Magento successfully accepted my changes without any error.

To change max_input_vars to higher value, you need to edit in PHP.ini file for max_input_vars like this:

max_input_vars 20000

But as editing PHP.ini requires restarting apache to reflect the changes, I did the change in .htaccess file just for Tax rule modification and later reverted it.

You can edit .htaccess to make this change by copy-pasting below line at the end of the file:

php_value max_input_vars 20000

Now you should be able to change Tax rules even if you have lots of tax rates posting through for that change! I prefered to revert .htaccess back to what it was as I didn’t need to change tax rules frequently.

Note: If the above does not solve the issue for you, make sure your Tax Rule name is not duplicating with another tax rule name.

Pages:«1234567...21»

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

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions