Magento - 将多个产品 ID 分配给一个类别
Magento - assign multiple product ids to a category
我有一个类别 "new",我想通过每日 cron 分配最近的 120 种产品。
我正在尝试覆盖分配给某个类别的产品 ID。
有没有简单的方法,比如:
$category->setProductIds(数组())
在 PHP 代码中,您可以在导入它们时将它们放入类别中。
假设您有一个名为 $product 的产品和一个名为 $category_id
的类别 ID
您可以通过以下方式设置产品所属的类别
$categories = array($category_id);
$product->setCategoryIds($categories);
$product->save();
如果产品已经有类别并且您想再添加一个类别,那么您可以像这样使用 getCategoryIds():
$categories = $product->getCategoryIds();
$categories[] = $categoryId;
$product->setCategoryIds($categories);
$product->save();
它是这样工作的
$category = Mage::getModel('catalog/category')->load($categoryID);
$product_array = $category->getProductsPosition()
... make changes ... format : $item[$product_id] => $position_in_category
$category->setPostedProducts($product_array);
$category->save();
要从类别中删除产品:
Mage::getSingleton('catalog/category_api')->removeProduct($category->getId(),$product->getId());
将产品添加到类别:
Mage::getSingleton('catalog/category_api')->assignProduct($category->getId(),$product->getId());
现在您可以遍历所有产品并使用类别 ID 将它们分配给类别。
注意:这不会覆盖产品已有的任何类别。
我有一个类别 "new",我想通过每日 cron 分配最近的 120 种产品。 我正在尝试覆盖分配给某个类别的产品 ID。 有没有简单的方法,比如:
$category->setProductIds(数组())
在 PHP 代码中,您可以在导入它们时将它们放入类别中。
假设您有一个名为 $product 的产品和一个名为 $category_id
的类别 ID您可以通过以下方式设置产品所属的类别
$categories = array($category_id);
$product->setCategoryIds($categories);
$product->save();
如果产品已经有类别并且您想再添加一个类别,那么您可以像这样使用 getCategoryIds():
$categories = $product->getCategoryIds();
$categories[] = $categoryId;
$product->setCategoryIds($categories);
$product->save();
它是这样工作的
$category = Mage::getModel('catalog/category')->load($categoryID);
$product_array = $category->getProductsPosition()
... make changes ... format : $item[$product_id] => $position_in_category
$category->setPostedProducts($product_array);
$category->save();
要从类别中删除产品:
Mage::getSingleton('catalog/category_api')->removeProduct($category->getId(),$product->getId());
将产品添加到类别:
Mage::getSingleton('catalog/category_api')->assignProduct($category->getId(),$product->getId());
现在您可以遍历所有产品并使用类别 ID 将它们分配给类别。
注意:这不会覆盖产品已有的任何类别。