2014-07-03 100 views
6

我是magento的新手。基本上,我想分配多個產品到多個類別。我按照this post,我已經做了以下代碼是工作的罰款:如何分配magento產品的類別以編程方式

$collection = Mage::getModel('catalog/product')->getCollection();//my coustom collection 
     $categorys_ids = array(1,2,3,4,5);//Array of ids etc 
     if ($categorys_ids != NULL && $collection->getData()!= NULL) 
      { 
       foreach ($collection as $product) 
       { 
         $categories_pd = $product->getCategoryIds();        
         $product->setCategoryIds(array_merge($product->getCategoryIds(),array($categorys_ids))); 
         $product->save(); 
       } 
      } 

現在,主要的問題是,當我給你設置的類別ID爲產品需要花費大量的時間。我有200個產品,這需要兩分鐘左右,這是很多時間。

我想知道是否有一種方法可以將產品類別分配給產品陣列,而不是將產品分配給類別或可以進行優化並花費更少時間的產品。

回答

8

以下是如何將多個產品分配到某個類別並與現有產品合併。
這個例子是針對一個類別的,但是你可以把它變成一個循環,以使其適用於更多。

$categoryId = 6; 
$category = Mage::getModel('catalog/category')->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)->load($categoryId); 
//get the current products 
$products = $category->getProductsPosition(); 
//now attach the other products. 
$newProductIds = array(1,2,3,4,5); 
foreach ($newProductIds as $id){ 
    $products[$id] = 1;//you can put any other position number instead of 1. 
} 
//attach all the products to the category 
$category->setPostedProducts($products); 
//save the category. 
$category->save(); 

如果你想要一個更快的方式,你可以直接插入表catalog_category_product
只要確保你完成後重新索引。

+0

感謝馬呂斯。我明白了,:) –

0

這裏是我是如何做到的,利用較新的PHP版本一些快速陣列管理功能:

<?php 
require_once '../../app/Mage.php'; 
Mage::app(); 
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID)); 
$storeCode = Mage::app()->getStore()->getStoreId(); 

function addProductsToCategoryId($mergeProductIds, $categoryId, $storeCode) { 
    // load the $category by $categoryId 
    $category = Mage::getModel('catalog/category')->setStoreId($storeCode)->load($categoryId); 
    // build a flipped array of two merged arrays (1) array keys from flipped $mergeProductIds, (2) array keys from product_id keyed array in $category 
    $categoryProductIds = array_flip(array_merge(array_keys(array_flip($mergeProductIds)),array_keys($category->getProductsPosition()))); 
    // combine array_keys from resulting merge with a matched index array filled with '0' 
    // THIS resets position of product within category, change this logic if desired 
    $categoryProductIds = array_combine(array_keys($categoryProductIds), array_fill(0, count($categoryProductIds), '0')); 

    $category->setPostedProducts($categoryProductIds); 
    $category->save(); 

    // optional 
    // return $categoryProductIds; 
} 

// optional array of category IDs to test against for nin (not in) or in a find_in_set array test 
// in the optional example line below, nin (not in) is used 
$categoryIds = array(5,8,9,10,11,12,45,46); 

$collectionIds = Mage::getModel('catalog/product')->getCollection() 
    ->setStoreId($storeCode) 
    // optional inclusion of join for category_id 
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left') 
    // optional logic to only gather ids that are, or are not in a given categoryIds array, nin (not in) is shown in example 
    // ->addAttributeToFilter('category_id', array('nin' => array('finset' => $categoryIds))) 
    // optional line to test whether product is associated to ANY category 
    ->addAttributeToFilter('category_id', array('null' => true)) 
    // example qualifiers to affect gathered IDs 
    ->addAttributeToFilter('sku', array('like' => 'M-H%')) 
    ->addAttributeToFilter('sku', array('nlike' => '%E')) 
    ->addAttributeToFilter('sku', array('nlike' => '%E#')) 
    ->addAttributeToFilter('sku', array('nlike' => '%Euro')) 
    ->addAttributeToFilter('sku', array('nlike' => '%Euro#')) 
    ->getAllIds() 
    ; 

// if using a return value, you can set the results of this to a variable 
// to perform further operations against the resulting data 
addProductsToCategoryId($collectionIds, 8, $storeCode); 

請注意,默認情況下,我的方法不保存您設置的類別內產品的任何位置。 IT會將所有位置恢復爲默認值'0'。

精美的作品。之後需要重新索引,快速批量添加。代碼有點複雜,所以在這種情況下,直接上下文中的代碼解釋對我來說更有意義。

我在這裏包含了很多可選的附加功能,但它們都被標記爲這樣並完全解釋。

0

下面的代碼爲我工作:

include '../../app/Mage.php'; 
Mage::app('admin'); 
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

$product_id = Mage::getModel("catalog/product")->getIdBySku($sku); 
$product = Mage::getModel('catalog/product')->load($product_id); 
$product->setCategoryIds($category_id); 
$product->save(); 
相關問題