I have added a wishlist icon on my header, I need to show my list of wishlist product if customer click the wishlist icon as a popup. Any reference thanks.
2 Answers
- Create Block file on following location:
app\code\Vendor\Extension\Block\WishlistProducts.php
namespace Vendor\Extension\Block;
use Magento\Framework\View\Element\Template\Context;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistCollectionFactory;
use Magento\Customer\Model\Session,
class WishlistProducts extends \Magento\Framework\View\Element\Template
{
protected $_wishlistCollectionFactory;
protected $_customerSession;
protected $_productCollectionFactory;
public function __construct(Context $context,
CollectionFactory $productCollectionFactory,
WishlistCollectionFactory $wishlistCollectionFactory,
Session $customerSessio
) {
$this->_productCollectionFactory = $productCollectionFactory;
$this->_wishlistCollectionFactory = $wishlistCollectionFactory;
$this->_customerSession = $customerSessio;
}
public function getWishlistProductCollection()
{
$collection = [];
if ($this->_customerSession->isLoggedIn()) {
$wishlist = $this->_wishlistCollectionFactory->create()
->addCustomerIdFilter($this->_customerSession->getCustomer()->getId());
$productIds = null;
foreach ($wishlist as $product) {
$productIds[] = $product->getProductId();
}
$collection = $this->_productCollectionFactory->create()->addIdFilter($productIds);
$collection = $this->_addProductAttributesAndPrices($collection)->addStoreFilter($this->getStoreId());
}
return $collection;
}
}
now create phtml file and assign above block class, and then do code like this :
<?php $collection = $block->getWishlistProductCollection(); foreach ($collection as $_product) { echo $product->getName(); echo $product->getProductUrl(); }
-
I want to show my wishlist on popup window, my code : codeshare.io/29oQY0 its not working, can u pls help me on this.– zusCommented Mar 9, 2021 at 15:34
-
path : Path: app\design\frontend\Zero\my_theme\Magento_Theme\templates\location_link.phtml,– zusCommented Mar 9, 2021 at 16:05
-
yes you can do the code into the proper block file and use the code here. Commented Mar 10, 2021 at 3:54
-
Bro, i am getting strange error, on my home page, copyright not getting full width, but other pages its getting full width, i.imgur.com/P0WClFw.png what is the possibilities error.– zusCommented Mar 10, 2021 at 4:47
-
If you have new question then ask it separately. if above answer help you then mark as solution and upvote, so it help other into community Commented Mar 10, 2021 at 5:02
Refer to the article: https://www.mageplaza.com/devdocs/get-wishlist-products-collection-magento-2.html Please tell me if it helps!