由于
Mage::app()->getWebsite(true)->getDefaultStore()->getRootCategoryId();
返回'2'
我已编写
$productCatalog = Mage::getModel('catalog/product')->load($product->getId());
$productCatalog->setCategoryIds('2')->save();
但它不会改变产品的类别——在"类别"选项卡中,复选框"默认类别"没有勾选。
假设您有一个名为$product的产品和一个名称为$category_ID 的类别ID
你可以通过以下将其分类
$product->setCategoryIds(array($category_id));
$product->save();
我认为问题是,您将类别id包含在一个引号中。该方法需要一个整数数组作为其参数。所以试试这个
$category_id = Mage::app()->getWebsite(true)->getDefaultStore()->getRootCategoryId();
$productCatalog = Mage::getModel('catalog/product')->load($product->getId());
$productCatalog->setCategoryIds(array($category_id));
$productCatalog->save();