2011-10-14 39 views
2

使用我試圖讓包選項數據如何沿其所有數據加載產品。使用這個:$product->getBundleOptionsData我需要使用這個,因爲我試圖以編程方式更改數據,我想以一種與管理員所用方式相近的方式執行此操作。Magento的:因爲它是在管理

然而,當我的var_dump上述函數的結果,我得到NULL而在捆綁模式產品類型管理側I得到正確的數據。

當我在我自己的文件的var_dump $product我得到的比我在捆綁模式的產品類型的var_dump保存功能,更短的數據。

什麼,我需要做的加載產品的所有數據,所以我可以使用getBundleOptionsData。我查看了幾個文件並進行了搜索,但無法找到答案。

回答

5

最後我把它的工作就是讓包選項的數據,所以我可以操縱它。我發現的主要代碼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。另一個相同。

+0

可以回答這個問題http://stackoverflow.com/questions/31055966/magento-how-to-update-bundle-product-programmatically – rajatsaurastri

+0

它不適用於我的一些產品。我放棄了這一點,發現問題(即'$ optionCollection-> appendSelections($ selectionCollection)'失敗) –

+0

它以什麼方式失敗? selectionCollection是空的嗎?產品是否加載?它是捆綁嗎? –