【问题标题】:Magento - assign multiple product ids to a categoryMagento - 将多个产品 ID 分配给一个类别
【发布时间】:2015-12-03 11:40:00
【问题描述】:

我有一个“新”类别,我想通过每日 cron 分配最近的 120 种产品。 我正在尝试覆盖分配给某个类别的产品 ID。 有没有简单的方法,比如:

$category->setProductIds(array())

【问题讨论】:

    标签: magento collections categories


    【解决方案1】:

    在 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 对象中做到这一点,因为我还想在分配新产品之前清空该类别,并且通过该类别中的所有产品不会那么好.
    【解决方案2】:

    它是这样工作的

    $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();
    

    【讨论】:

      【解决方案3】:

      从类别中删除产品:

      Mage::getSingleton('catalog/category_api')->removeProduct($category->getId(),$p‌​roduct->getId());
      

      将产品添加到类别:

      Mage::getSingleton('catalog/category_api')->assignProduct($category->getId(),$p‌​roduct->getId());
      

      现在您可以遍历所有产品并使用类别 ID 将它们分配给类别。

      注意:这不会覆盖产品已有的任何类别。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-20
        • 1970-01-01
        • 2012-05-18
        • 1970-01-01
        • 2016-03-19
        • 1970-01-01
        相关资源
        最近更新 更多