2011-10-22 44 views
0

Heyhey,Magento的1.6副本類產品

我試圖讓一個腳本,將加載所有的產品從一類,並將其添加到其他的類別(所以,基本上,只是鏈接的所有產品,其他類別) 。我想要的是:

$category = Mage::getModel('catalog/category'); 
$category->load($id); // Preset category id 
$collection = $category->getProductCollection(); 
$collection->addAttributeToSelect('*'); 

foreach ($collection as $product) { 
    $result[] = $product->getProductId(); 
// Now get category ids and add a specific category to them and save? 
} 

$結果出現空,我不知道如何繼續。有任何想法嗎?

+0

,如果你在foreach之前做總彙的的var_dump你得到了什麼? –

回答

2

首先要看的是,不要選擇所有屬性,$collection->addAttributeToSelect('id')就夠了。其次,以獲得產品ID使用

$product->getId(); 

要更改的類別,你可以嘗試這樣的事:

$categories = $product->getCategoryIds(); 
$categories[] = 4; // Category to add 
$product->setCategoryIds($categories); 
$product->save();