如何从magento 1.5类别中删除产品 [英] How to remove a product from a category magento 1.5

查看:80
本文介绍了如何从magento 1.5类别中删除产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Magento的新手,并且正在从事一项cron作业,该作业将在分配日期之后删除特定类别中的产品.在工作和Stackoverflow的帮助下,我想到了以下代码:

I'm kind of new in Magento and I'm working on a cron job that removes a product in a specific category after the date that was assigned. With work and the help of Stackoverflow, I came up with this code:

require_once 'app/Mage.php';
Mage::app();
$date = Mage::getModel('core/date')->date('Y-m-d H:i:s');
$collection = Mage::getModel('catalog/product')->getCollection();    
$collection->addfieldtofilter('news_to_date', array(array('to' => $date)));        
foreach($collection as $product) {        
   $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
   $product->save();
 }

这将检查实际日期并将其与产品日期进行比较.如果日期已过,则该产品被禁用.我需要的是,代码不应禁用产品,而应删除类别的产品(在这种情况下,类别为销售")

This checks the actual date and compares it with the date of the products. If the date has passed, the product is disabled. What I need is that instead of disabling the product, the code should remove the product of the category (in this case the category is 'Sales')

希望你们能帮助我!

提前谢谢!

推荐答案

您需要从产品中获取所有类别ID,然后从类别ID数组中删除销售类别ID,然后将其重新设置为产品.

You need get all category ids from product, then remove Sales category ID from category ids array and set them back to product.

例如,销售类别ID为5.

Example, Sales category ID is 5.

foreach ($collection as $product) {
    //Getting all category ids
    $ids = $product->getCategoryIds();
    //Searching array key with value 5 and removing from array
    if (($key = array_search(5, $ids)) !== false) {
        unset($ids[$key]);
        $product->setCategoryIds($ids)
        $product->save();
    }
}

P.S.您可以使用magento cron作业功能,而不需要使用以下功能:

P.S. You can use magento cron job functionality, than you do not need to use:

require_once 'app/Mage.php';
Mage::app();

这篇关于如何从magento 1.5类别中删除产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆