Reputation: 23000
I'm using grouped products to track promotions. Simple products will at times belong to multiple grouped products, so checking parentProductIds is of no use. I'm wondering how to track the grouped product ID when a product is purchased through the grouped (promotion) SKU. I can see it's being stored in info_buyRequest and super_product_config within the orders, but how do I get that information back out? And is there a way to get it out in the cart/quote?
Upvotes: 2
Views: 3874
Reputation: 31
On cart page grouped product is treated as a simple product. In Magento 2 you can get the parent id of these simple products from session. This worked for me:
<?php
$catalogSession = $_SESSION['catalog'];
$parentId = $catalogSession['last_viewed_product_id'];
?>
Upvotes: 0
Reputation: 23000
I was able to get it with the following code in cart.phtml, in the foreach($this->getItems() as $_item):
$values = unserialize($_item->getOptionByCode('info_buyRequest')->getValue());
$parentId = $values['super_product_config']['product_id'];
Upvotes: 6
Reputation: 2466
Depending on where you want to get this information, you could get it after a checkout process when the sales is saved. Then you could use the events sales_order_save_after
and create a method in a class to get the items of a grouped product.
What is important here is the object of a class Mage_Sales_Model_Order_Item
which has information about the product and the parents of a product
Here is an example:
public function processSalesOrder($observer)
{
$order = $observer->getOrder()
$quoteItems = $order->getItemsCollection(null, true);
/*@var $item Mage_Sales_Model_Order_Item */
foreach ($quoteItems as $item) {
$parent = $item->getParentItem();
if(!is_null($parent)){
// do your stuff - you have a product parent which has children product
// $item is the children
echo 'The parent product is ' . $parent->getSku();
echo 'One of the children product is' .$item->getSku();
}
}
Upvotes: 0