最後我把它的工作就是讓包選項的數據,所以我可以操縱它。我發現的主要代碼Magento的模型捆綁observer類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。另一個相同。
可以回答這個問題http://stackoverflow.com/questions/31055966/magento-how-to-update-bundle-product-programmatically – rajatsaurastri
它不適用於我的一些產品。我放棄了這一點,發現問題(即'$ optionCollection-> appendSelections($ selectionCollection)'失敗) –
它以什麼方式失敗? selectionCollection是空的嗎?產品是否加載?它是捆綁嗎? –