【问题标题】:How to remove items from wishlist collection in magento如何从magento的愿望清单中删除项目
【发布时间】:2012-08-03 05:59:08
【问题描述】:

在 Magento 中,我想删除或删除当前登录用户的愿望清单项目。 目前我通过启用复选框来选择愿望清单项目,然后使用 Mage::getModel('wishlist/item')->load($id)->delete() 删除它们。我使用的代码 sn-p 粘贴在下面。当我单击提交按钮时,项目会被删除,但只有在我再次刷新页面时才能看到效果。问题是我需要强制刷新页面才能看到删除的项目。 任何人都可以建议我任何合适的解决方案。这将不胜感激。

注意:当复选框被选中时,wishlist-item id 分配给它的值字段为:

值= $item->getWishlistItemId()

表单提交后,执行以下代码

    if(isset($_POST['wishlist']))  // wishlist is name of check box.
    {
      $checkboxes = $_POST['wishlist'];
   foreach ($checkboxes as $id):      
      $bulk = $_COOKIE["bulkaction"];
      if($bulk == "Remove"):
      Mage::getModel('wishlist/item')->load($id)->delete();   // $id contains the wishlist item id.
      $url = $this->getBaseUrl().'wishlist';
      header("refresh:0.0000000000001;", $url);
      endif;
  endforeach;
}  

【问题讨论】:

    标签: magento


    【解决方案1】:

    如果我理解正确,您想删除与特定用户关联的所有愿望清单项目吗? ...

    $customerId = 1; // Replace with the customer id you are targetting
    
    $itemCollection = Mage::getModel('wishlist/item')->getCollection()
        ->addCustomerIdFilter($customerId);
    
    foreach($itemCollection as $item) {
        $item->delete();
    }
    

    【讨论】:

    • 澄清点:这似乎会从数据库中删除产品,而不仅仅是从集合对象中删除它。我相信从数据库中删除该项目将是错误的意图。
    【解决方案2】:

    Drew 的回答肯定会从数据库中删除项目。 但是,如果您想从“当前”集合中删除项目,您应该使用 Varien_Data_Collection::removeItemByKey()

    $items = $this->getItems();
    foreach ($items as $key => $item) {
        if ($condition) $items->removeDataByKey($key);
    }
    

    但请注意,有时(当集合在代码中的不同位置单独访问时)它可能看起来不起作用。

    【讨论】:

      【解决方案3】:

      你可以使用

      foreach ($items as $key => $item) {
          if ($condition) $items->removeItemByKey($key);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多