How to remove items from wishlist collection in magento
在 Magento 中,我想移除或删除当前登录用户的愿望清单项目。
目前我通过启用复选框来选择愿望清单项目,然后使用 Mage::getModel('wishlist/item')->load($id)->delete() 删除它们。我使用的代码片段粘贴在下面。当我单击提交按钮时,项目会被删除,但只有在我再次刷新页面时才能看到效果。问题是我需要强行刷新页面才能看到删除的项目。
任何人都可以向我建议任何合适的解决方案。这将不胜感激。
注意:当复选框被选中时,wishlist-item id 被分配给它的值字段:
value= $item->getWishlistItemId()
表单提交后,执行如下代码
1 2 3 4 5 6 7 8 9 10 11 12 | 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; } |
如果我理解正确,您想删除与特定用户关联的所有愿望清单项目吗? ...
1 2 3 4 5 6 7 8 | $customerId = 1; // Replace with the customer id you are targetting $itemCollection = Mage::getModel('wishlist/item')->getCollection() ->addCustomerIdFilter($customerId); foreach($itemCollection as $item) { $item->delete(); } |
你可以使用
1 2 3 | foreach ($items as $key => $item) { if ($condition) $items->removeItemByKey($key); } |
德鲁的回答肯定会从数据库中删除项目。
但是,如果您想从"当前"集合中删除项目,您应该使用 Varien_Data_Collection::removeItemByKey()
1 2 3 4 | $items = $this->getItems(); foreach ($items as $key => $item) { if ($condition) $items->removeDataByKey($key); } |
但请注意,有时(当在代码中的不同位置分别访问集合时)它可能看起来不起作用。