Far*_*han 6 product magento categories mass-assignment
我是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();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,主要问题是,当我为产品分配设置类别ID时,需要花费大量时间.我有200个产品,这需要两分钟左右,这是很多时间.
我想知道是否有一种方法可以为产品阵列分配类别,而不是将产品分配到类别或可以优化的东西,花费更少的时间.
以下是如何将多个产品分配到类别并与现有产品合并的方法.
该示例适用于一个类别,但您可以将其转换为循环以使其更有效.
$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();
Run Code Online (Sandbox Code Playgroud)
如果您想要更快的方法,可以在表格中直接插入catalog_category_product.
完成后,请确保重新索引.