I wrote
public function catalogProductTypePrepare($observer)
{
$quote = Mage::getSingleton('checkout/session')->getQuote();
$product = $observer->getEvent()->getProduct();
$productid = $product->getId();
$buyRequest = $observer->getEvent()->getBuyRequest();
$productqtty = $buyRequest->getQty();
it returns the last product of the grouped product I want to get each one of the single product of the grouped product
I think I need to use
if($product->getTypeId() == 'grouped') // check if the product is grouped product
{
$associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
// get all simple products details through $associatedProducts
}
but I am not sure how because I need to use $buyRequest to get the quantity of each sub product
I was looking at this yesterday - for each order I want to get the associated child product's default sale quantity & then check that against the remaining stock level, to see if I need to disable the grouped product.
I will do this via an observer but in the meantime this is how I get the associated products:
require_once('app/Mage.php');
umask(0);
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
Mage::app();
Mage::log('starting');
$product_id=4788;//grouped product
Mage::log($product_id);
$item = Mage::getModel('catalog/product')->load($product_id);
Mage::log('Bundle Product Name ' .$item->getName());
Mage::log('Bundle Product Item Type ' .$item->getTypeId());
if($item->getTypeId() == 'grouped' ){
Mage::log(' Product is grouped');
//this gets us all the associated products
$products = $item->getTypeInstance(true)->getAssociatedProducts($item);
foreach($products as $p){
$productSimple = Mage::getModel('catalog/product')->load($p->getId());
$_stockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($p->getId())->getQty();
Mage::log($p->getId() . ' Qty ' .$_stockQty);
$_minQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($p->getId())->getMinQty();
Mage::log($p->getId() . ' Qty ' .$_minQty);
if ($_stockQty < $_minQty) {
//set the parent product to be disabled
//Mage::getModel('catalog/product_status')->updateProductStatus($_parent->getProductId(), $storeId, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
Mage::log('parent sku disabling ' .$item->getProductId() );
}
}
}
Mage::log('finishing');
What I'm not sure is how to get the min sale or default qty back from the associated product - for this item one of the associated products has 9 as the Default Qty but my code is returning 0.