如何从购买或购物车产品中获取分组产品ID

时间:2011-10-21 13:48:57

标签: magento magento-1.4

我正在使用分组产品来跟踪促销活动。简单的产品有时会属于多个分组产品,因此检查parentProductIds是没有用的。我想知道如何通过分组(促销)SKU购买产品时跟踪分组的产品ID。我可以看到它存储在订单中的info_buyRequest和super_product_config中,但是如何将这些信息取回?有没有办法在购物车/报价中找到它?

3 个答案:

答案 0 :(得分:6)

我能够使用cart.phtml中的以下代码获取它,在foreach中($ this-> getItems()为$ _item):

$values = unserialize($_item->getOptionByCode('info_buyRequest')->getValue());
$parentId = $values['super_product_config']['product_id'];

答案 1 :(得分:0)

根据您希望获取此信息的位置,您可以在结帐过程结束时保存销售。然后,您可以使用事件sales_order_save_after并在类中创建方法以获取分组产品的项目。 这里重要的是类Mage_Sales_Model_Order_Item的对象,它具有关于产品和产品父母的信息 这是一个例子:

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();
        }

}

答案 2 :(得分:0)

在购物车页面上,分组产品被视为简单产品。在Magento 2中,您可以从会话中获取这些简单产品的父ID。这对我有用:

<?php 

$catalogSession = $_SESSION['catalog'];             
$parentId = $catalogSession['last_viewed_product_id'];

?>
相关问题