C:\ XAMPP \ htdocs中\项目\ baab.bh \应用\代码\核心\法师\收藏\助手\ Data.php
getWishlist()
当我调用此函数从前端 phtml 文件获取心愿单时,它给了我正确的心愿单,但当我从控制器调用此功能时,它给了我默认值wishlist只有当前的愿望清单才是这个功能的代码
public function getWishlist()
{
if (is_null($this->_wishlist)) {
if (Mage::registry('shared_wishlist')) {
$this->_wishlist = Mage::registry('shared_wishlist');
} elseif (Mage::registry('wishlist')) {
$this->_wishlist = Mage::registry('wishlist');
} else {
$this->_wishlist = Mage::getModel('wishlist/wishlist');
if ($this->getCustomer()) {
$this->_wishlist->loadByCustomer($this->getCustomer());
}
}
}
return $this->_wishlist;
}
我使用相同的代码调用它,但它的行为与前端 phtml 文件和控制器的行为不同。如何从控制器获取当前的愿望清单?
答案 0 :(得分:1)
为此,您将获得所有客户的愿望清单:
public function getWishlist()
{
$customer = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');
$wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer);
$wishListAllItem = $wishList->getItemCollection();
if (count($wishListAllItem)) {
$arrOfProductIds = array();
foreach ($wishListAllItem as $item) {
$arrOfProductIds[] = $item->getProductId();
}
}
return $arrOfProductIds;
}
为此,您可以获得当前用户心愿单:
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer)
$wishListAllItem = $wishList->getItemCollection();
if (count($wishListAllItem)) {
$arrOfProductIds = array();
foreach ($wishListAllItem as $item) {
$product = $item->getProduct();
$arrOfProductIds[] = $product->getId();
}
}