【问题标题】:Magento: how to load product along its all data as it is used in adminMagento:如何在管理中使用其所有数据加载产品
【发布时间】:2011-10-14 05:09:08
【问题描述】:

我正在尝试获取捆绑选项数据。使用这个:$product->getBundleOptionsData 我需要使用这个,因为我正在尝试以编程方式更改数据,并且我想以与 admin 中使用的一样接近的方式进行操作。

但是,当我 var_dump 上述函数的结果时,我得到NULL,而在捆绑模型产品类型的管理员端,我得到了正确的数据。

当我在自己的文件中使用 var_dump $product 时,我得到的数据比在捆绑模型产品类型保存功能中使用 var_dump 时要短得多。

我需要做什么来加载产品的所有数据,所以我可以使用getBundleOptionsData。我查看了几个文件并用谷歌搜索,但找不到答案。

【问题讨论】:

    标签: php magento


    【解决方案1】:

    最后,我成功地获取了捆绑选项数据,以便我可以对其进行操作。我在magento的模型包观察者类duplicateProduct函数中找到了主要代码:但是我需要添加option_id(小心不要忘记)

    这是最后阶段的代码。

    $product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
    $optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
    $selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection(
        $product->getTypeInstance(true)->getOptionsIds($product),
        $product
    );
    $optionCollection->appendSelections($selectionCollection);
    
    $optionRawData = array();
    $selectionRawData = array();
    
    $i = 0;
    foreach ($optionCollection as $option) {
        $optionRawData[$i] = array(
                'option_id' => $option->getOptionId(), //my addition. important otherwise, options going to be duplicated
                'required' => $option->getData('required'),
                'position' => $option->getData('position'),
                'type' => $option->getData('type'),
                'title' => $option->getData('title')?$option->getData('title'):$option->getData('default_title'),
                'delete' => ''
            );
        foreach ($option->getSelections() as $selection) {
            $selectionRawData[$i][] = array(
                'product_id' => $selection->getProductId(),
                'position' => $selection->getPosition(),
                'is_default' => $selection->getIsDefault(),
                'selection_price_type' => $selection->getSelectionPriceType(),
                'selection_price_value' => $selection->getSelectionPriceValue(),
                'selection_qty' => $selection->getSelectionQty(),
                'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
                'delete' => ''
            );
        }
        $i++;
    }
    
    $product->setBundleOptionsData($optionRawData);   //changed it to $product
    $product->setBundleSelectionsData($selectionRawData);  //changed it to $product
    

    您现在可以更改 optionsrawdata 中的原始数据。或获取捆绑选项数据。另一个也一样。

    【讨论】:

    • 你能回答这个问题吗stackoverflow.com/questions/31055966/…
    • 它不适用于我的某些产品。我已经调试并发现了问题(即$optionCollection->appendSelections($selectionCollection) 失败)
    • 以什么方式失败? selectionCollection 是空的吗?是否已加载产品?是捆绑包吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    相关资源
    最近更新 更多