Magento:如何加载产品及其在管理中使用的所有数据

2024-02-27

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

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

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

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


最后,我成功获取捆绑选项数据,以便我可以对其进行操作。我在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 中的原始数据。或 getBundleOptionsData。另一个也一样。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Magento:如何加载产品及其在管理中使用的所有数据 的相关文章

随机推荐