WishlistCollection keeps returning 0 products
-
14-04-2021 - |
题
I'm trying to make a widget using the wishlist to show whether you've added the current product to the wishlist already or not. I've seen some other threads with answers and I can safely say I tried most if not all of them. Please help a beginning Magento 2 Dev out 🙏🏼
Wishlist has 3 items (Can be seen in the wishlist page and Database)
/**
* @param Product $inputProduct
*
* @return WishlistCollectionFactory
*/
public function checkWishlistItem(Product $inputProduct)
{
$wishlistCollection = $this->_wishlistProvider->getWishlist()->getItemCollection();
$wishlistItemList = [];
/** @var Product $wishlistItem */
foreach ($wishlistCollection as $wishlistItem) {
array_add($wishlistItemList, 'product_id', $wishlistItem->getId());
}
return $wishlistCollection->addFieldToFilter('main_table.product_id', $inputProduct->getId());
}
Other block I've tried:
$wishlistCollection = $this->_wishlistHelper->getWishlistItemCollection();
$inWishlist = false;
foreach ($wishlistCollection as $wishlistItem) {
if ($inputProduct->getId() == $wishlistItem->getProduct()->getId()) {
$inWishlist = true;
}
}
I have created a single php file that does all the work in Block/Widget. I can use all the feedback I can get so feel free to tell me another way of doing things if you feel like I'm doing things wrong.
解决方案 2
I appreciate the help! Eventually I found out that using SessionFactory instead of Session worked out:
use Magento\Customer\Model\SessionFactory as Session;
use Magento\Wishlist\Model\Wishlist;
class FeaturedProducts extends Template implements BlockInterface
{
/**
* Session
*
* @var Session
*/
private $_customerSession;
/**
* WishlistModel
*
* @var Wishlist
*/
private $_wishlist;
/**
* Test constructor
*
* @param Context $context
* @param Session $customerSession
* @param Wishlist $wishlist
* @param array $data
*/
public function __construct(
Context $context,
Session $customerSession,
Wishlist $wishlist,
array $data = []
) {
parent::__construct($context, $data);
$this->_customerSession = $customerSession;
$this->_wishlist = $wishlist;
}
public function getWishlistCollection()
{
$customerSession = $this->_customerSession->create();
$isLoggedIn = $customerSession->isLoggedIn();
if ($isLoggedIn) {
$customer_id = $customerSession->getCustomerId();
$wishlist_collection = $this->_wishlist->loadByCustomerId($customer_id, true)->getItemCollection();
}
}
}
I don't exactly know why this works and Session doesn't tho.
其他提示
Create WishlistProducts block
To get Wishlist Products Collection, firstly, you need to create a WishlistProducts block. To do that, follow the path Vendor/ModuleName/Block/WishlistProducts.php
and add the below code:
<?php
namespace Vendor\ModuleName\Block;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistCollectionFactory;
use Vendor\ModuleName\Helper\Data;
class WishlistProducts extends AbstractSlider
{
/**
* @var WishlistCollectionFactory
*/
protected $_wishlistCollectionFactory;
/**
* WishlistProducts constructor.
* @param Context $context
* @param CollectionFactory $productCollectionFactory
* @param Visibility $catalogProductVisibility
* @param DateTime $dateTime
* @param Data $helperData
* @param HttpContext $httpContext
* @param WishlistCollectionFactory $wishlistCollectionFactory
* @param array $data
*/
public function __construct(
Context $context,
CollectionFactory $productCollectionFactory,
Visibility $catalogProductVisibility,
DateTime $dateTime,
Data $helperData,
HttpContext $httpContext,
WishlistCollectionFactory $wishlistCollectionFactory,
array $data = []
) {
$this->_wishlistCollectionFactory = $wishlistCollectionFactory;
parent::__construct($context, $productCollectionFactory, $catalogProductVisibility, $dateTime, $helperData, $httpContext, $data);
}
/**
* @inheritdoc
*/
public function getProductCollection()
{
$collection = [];
if ($this->_customer->isLoggedIn()) {
$wishlist = $this->_wishlistCollectionFactory->create()
->addCustomerIdFilter($this->_customer->getCustomerId());
$productIds = null;
foreach ($wishlist as $product) {
$productIds[] = $product->getProductId();
}
$collection = $this->_productCollectionFactory->create()->addIdFilter($productIds);
$collection = $this->_addProductAttributesAndPrices($collection)->addStoreFilter($this->getStoreId())->setPageSize($this->getProductsCount());
}
return $collection;
}
}