Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Magento - Configurable Product Checks
<html>
<head>
<title>Configurable Product Checks</title>
<style>
.container {width:960px;margin:25px auto;}
table {margin:25px 0px;}
td {padding:10px 5px;}
.green {color:green;}
.red {color:red;}
</style>
</head>
<body>
<div class="container">
<h1>Stock Status of Configurable Products</h1>
<p>This page loads all the configurable products and outputs their stock status.
<ul>
<li>Items that are all green across are in stock and have inventory.</li>
<li>Items that are all red across are out of stock and have no inventory</li>
<li>Items that have both red and green should be reviewed and updated.</li>
</ul>
<table border="1"><tr><th>Item Name</th><th>Magento ID Number</th><th>Saleable</th><th>Config Product Status</th><th>Associated Product Stock</th></tr>
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')->addAttributeToFilter('type_id', array('eq' => 'configurable'));
foreach ($collectionConfigurable as $_configurableproduct) {
/**
* Load product by product id
*/
$product = Mage::getModel('catalog/product')->load($_configurableproduct->getId());
echo "<tr><td>".$product->getName()."</td><td>".$product->getId()."</td>";
if ($product->isSaleable()) {
echo "<td class='green'>Saleable</td>";
} else {
echo "<td class='red'>Out of Stock</td>";
}
$stock = $product->getStockItem();
if ($stock->getIsInStock()) {
echo "<td class='green'>Product In Stock</td>";
} else {
echo "<td class='red'>Product Out of Stock</td>";
}
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
$instock_childrenisinstock = false;
foreach ($childProducts as $childProduct) {
$qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($childProduct)->getQty();
if ($qty > 0) {
$instock_childrenisinstock = true;
}
}
if ($instock_childrenisinstock) {
echo "<td class='green'>Sub-Products in-stock</td>";
} else {
echo "<td class='red'>Sub-Products out-of-stock</td>";
}
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
<html>
<head>
<title>Configurable product checks - only error items</title>
<style>
table {margin:25px 0px;}
td {padding:10px 5px;}
.green {color:green;}
.red {color:red;}
</style>
</head>
<body>
<div style="width:960px;margin:25px auto;">
<h1>Stock Status of Configurable Products</h1>
<p>This page shows all the products that have mismatched stock status.
<table border="1"><tr><th>Item Name</th><th>Magento ID Number</th><th>Saleable</th><th>Config Product Status</th><th>Associated Product Stock</th></tr>
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')->addAttributeToFilter('type_id', array('eq' => 'configurable'));
foreach ($collectionConfigurable as $_configurableproduct) {
/**
* Load product by product id
*/
$product = Mage::getModel('catalog/product')->load($_configurableproduct->getId());
$stock = $product->getStockItem();
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
$instock_childrenisinstock = false;
foreach ($childProducts as $childProduct) {
$qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($childProduct)->getQty();
if ($qty > 0) {
$instock_childrenisinstock = true;
}
}
if ($product->isSaleable() && $stock->getIsInStock()) {
} else if (!$stock->getIsInStock() && !$instock_childrenisinstock) {
} else {
echo "<tr><td>".$product->getName()."</td><td>".$product->getId()."</td>";
if ($product->isSaleable()) {
echo "<td class='green'>Saleable</td>";
} else {
echo "<td class='red'>Out of Stock</td>";
}
if ($stock->getIsInStock()) {
echo "<td class='green'>Product In Stock</td>";
} else {
echo "<td class='red'>Product Out of Stock</td>";
}
if ($instock_childrenisinstock) {
echo "<td class='green'>Sub-Products in-stock</td>";
} else {
echo "<td class='red'>Sub-Products out-of-stock</td>";
}
echo "</tr>";
}
}
?>
</table>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.