dream5694 2013-07-22 22:34
浏览 40
已采纳

如何从类别magento 1.5中删除产品

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')

I hope you guys can help me!

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • ds11111111111111111 2013-07-23 10:34
    关注

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

    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. You can use magento cron job functionality, than you do not need to use:

    require_once 'app/Mage.php';
    Mage::app();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码