我是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种产品,这最多需要两分钟左右的时间,这是很多时间。
我想知道是否有一种方法可以将类别分配给产品数组,而不是将产品分配给类别或可以优化并花费更少时间的项目。
这是将多个产品分配到一个类别并与现有产品合并的方法。
该示例仅针对一个类别,但您可以将其变成一个循环以使其适用于更多类别。
$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
。
只要确保完成后重新索引即可。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句