Showing Out of Stock variants(simple) when all variants are out of stock?
-
12-12-2019 - |
Question
I need the ability to show Out of Stock variants when all variants(simple/children) under a configurable are out of stock. The default bahavior I am trying to overcome is to remove the product-options all together.
I've made the following code adjustment as a local edit to app\code\core\Mage\Catalog\Block\product\View\Type\Configurable.php to ensure out of stock variants show when the Show Out of Stock option (System > Admin > Inventory > Show Out of Stock) is set to 'Yes':
public function getAllowProducts()
{
// check OoS display value (admin > config > inventory > stock options > display out of stock products)
$showOos = Mage::helper('cataloginventory')->isShowOutOfStock();
if (!$this->hasAllowProducts()) {
$products = array();
$skipSaleableCheck = Mage::helper('catalog/product')->getSkipSaleableCheck();
$allProducts = $this->getProduct()->getTypeInstance(true)
->getUsedProducts(null, $this->getProduct());
foreach ($allProducts as $product) {
// if OoS display 'Yes' show all variants
if ($showOos === true) {
$products[] = $product;
// if not, filter out OoS variants
} else if ($product->isSaleable() || $skipSaleableCheck) {
$products[] = $product;
}
}
$this->setAllowProducts($products);
}
return $this->getData('allow_products');
}
This works great so long as at least one variant is in stock. If all of them are set to out of stock, product-options disables at the configurable level. A few notes:
- I started by manipulating the hasOptions() function, with no luck. I also removed this as an if condition on the view.phtml page around my 'container1' and 'container2' getChildChildHTML declarations. (originally 'if ($_product->isSaleable() && $this->hasOptions())', now commented out solely for the test) This returns the product options container itself, but does not generate the variants list.
- Running a $product->getOptions() foreach for a configurable in view.phtml with all out of stock variants returns nothing.
I have a feeling there is something deeper I am missing. Anyone dealt with this before?
Solution
Add this
$defaultSkip = Mage::helper('catalog/product')->getSkipSaleableCheck();
Mage::helper('catalog/product')->setSkipSaleableCheck(true);
right above
$skipSaleableCheck = Mage::helper('catalog/product')->getSkipSaleableCheck();
and
Mage::helper('catalog/product')->setSkipSaleableCheck($defaultSkip);
before or after
$this->setAllowProducts($products);