Magento:如何在管理中使用产品时沿其所有数据加载产品 [英] Magento: how to load product along its all data as it is used in admin
问题描述
我正在尝试获取捆绑包选项数据.使用这个:$product->getBundleOptionsData
我需要使用这个,因为我正在尝试以编程方式更改数据,并且我想以一种与admin中所使用的接近的方式来进行操作.
I'm trying to get bundle options data. using this : $product->getBundleOptionsData
I need to use this, as I'm trying to change data programmatically and I would like to do it in a way that's as close as used in admin .
但是,当我var_dump上面函数的结果时,我得到NULL
,而在捆绑模型产品类型的管理员端,我正确地得到了数据.
However, when I var_dump the result of the above function I get NULL
while in admin side in bundle model product type I get correctly the data.
当我在自己的文件中var_dump $product
时,与捆绑模型产品类型保存功能中的var_dump相比,我得到的数据短得多.
When I var_dump $product
in my own file I get much shorter data than when I var_dump in bundle model product type save function.
我需要做些什么来加载产品的所有数据,所以我可以使用getBundleOptionsData
.我查看了几个文件并用谷歌搜索,但找不到答案.
what do I need to do to load all data of the product, so I can use getBundleOptionsData
. I looked in several files and googled, but can't find an answer.
推荐答案
最后,我使它能够获取捆绑软件选项数据,因此我可以对其进行操作.我在magento的模型捆绑观察器类plicateProduct函数中找到了主要代码:但是,我需要添加option_id(注意不要忘记这一点)
Finally I made it work to get bundle options data so I can manipulate it. I found the main code in magento's model bundle observer class duplicateProduct function: I needed however to add option_id (careful not to forget that)
这是最后阶段的代码.
$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.和另一个相同.
you can either now change on the raw data in optionsrawdata. or getBundleOptionsData. and same for the other one.
这篇关于Magento:如何在管理中使用产品时沿其所有数据加载产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!