如何以编程方式在magento中为产品分配类别

Posted

技术标签:

【中文标题】如何以编程方式在magento中为产品分配类别【英文标题】:How to assign categories for products in magento Programmatically 【发布时间】:2014-08-24 06:16:43 【问题描述】:

我是 magento 的新手。基本上,我想将多个产品分配给多个类别。我已经关注this post 并且我已经完成了以下代码,它工作正常:

   $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 种产品,这需要两分钟左右,这是很多时间。

我想知道是否有一种方法可以将类别分配给产品数组,而不是将产品分配给类别或可以优化并花费更少时间的方法。

【问题讨论】:

【参考方案1】:

以下是如何将多个产品分配到一个类别并与现有产品合并的方法。 该示例适用于一个类别,但您可以将其转换为循环以使其适用于更多类别。

$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。 只需确保在完成后重新索引即可。

【讨论】:

【参考方案2】:

我是这样做的,使用了较新 php 版本的一些快速数组管理功能:

<?php
require_once '../../app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$storeCode = Mage::app()->getStore()->getStoreId();

function addProductsToCategoryId($mergeProductIds, $categoryId, $storeCode) 
    // load the $category by $categoryId
    $category = Mage::getModel('catalog/category')->setStoreId($storeCode)->load($categoryId);
    // build a flipped array of two merged arrays (1) array keys from flipped $mergeProductIds, (2) array keys from product_id keyed array in $category
    $categoryProductIds = array_flip(array_merge(array_keys(array_flip($mergeProductIds)),array_keys($category->getProductsPosition())));
    // combine array_keys from resulting merge with a matched index array filled with '0'
    // THIS resets position of product within category, change this logic if desired
    $categoryProductIds = array_combine(array_keys($categoryProductIds), array_fill(0, count($categoryProductIds), '0'));

    $category->setPostedProducts($categoryProductIds);
    $category->save();

    // optional
    // return $categoryProductIds;


// optional array of category IDs to test against for nin (not in) or in a find_in_set array test
// in the optional example line below, nin (not in) is used
$categoryIds = array(5,8,9,10,11,12,45,46);

$collectionIds = Mage::getModel('catalog/product')->getCollection()
    ->setStoreId($storeCode)
    // optional inclusion of join for category_id
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    // optional logic to only gather ids that are, or are not in a given categoryIds array, nin (not in) is shown in example
    // ->addAttributeToFilter('category_id', array('nin' => array('finset' => $categoryIds)))
    // optional line to test whether product is associated to ANY category
    ->addAttributeToFilter('category_id', array('null' => true))
    // example qualifiers to affect gathered IDs
    ->addAttributeToFilter('sku', array('like' => 'M-H%'))
    ->addAttributeToFilter('sku', array('nlike' => '%E'))
    ->addAttributeToFilter('sku', array('nlike' => '%E#'))
    ->addAttributeToFilter('sku', array('nlike' => '%Euro'))
    ->addAttributeToFilter('sku', array('nlike' => '%Euro#'))
    ->getAllIds()
    ;

// if using a return value, you can set the results of this to a variable
// to perform further operations against the resulting data
addProductsToCategoryId($collectionIds, 8, $storeCode);

请注意,默认情况下,我的方法不会为您设置的类别中的产品保留任何位置。它会将所有位置设置回默认值“0”。

效果很好。之后需要重新索引,快速添加质量。代码有点复杂,所以在这种情况下,在直接上下文 cmets 中解释代码对我来说更有意义。

我在这里包含了很多可选的附加功能,但它们都被标记为这样并得到了充分的解释。

【讨论】:

【参考方案3】:

以下代码对我有用:

include '../../app/Mage.php';
Mage::app('admin');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$product_id = Mage::getModel("catalog/product")->getIdBySku($sku);
$product = Mage::getModel('catalog/product')->load($product_id);
$product->setCategoryIds($category_id);
$product->save();

【讨论】:

以上是关于如何以编程方式在magento中为产品分配类别的主要内容,如果未能解决你的问题,请参考以下文章

Magento 将产品批量分配到类别

Magento - 如何以编程方式取消选中超级产品属性上的“使用默认值”?

如何以编程方式为单个指定的商店视图重建Magento产品平面数据?

magento 1.9 新添加的产品没有显示在分类页面?

magento 以编程方式为可配置产品创建订单

Magento - 以编程方式添加的捆绑产品不适用于购物篮/前端