显示心愿单产品对鼓励客户从您的商店购买商品有积极的影响,因为它提醒客户他们有想要订购的首选产品。愿望清单也是您可以了解客户的购买兴趣和购物倾向的地方。因此,在 Magento 2 商店中处理此功能非常重要。

为了在网站上显示愿望清单产品集合,商店管理员将需要有关愿望清单产品集合的数据。然而,获取集合并不是一项简单的任务,尽管有几篇文章写了有关此问题的解决方案,但没有一篇真正有效。因此,在今天的帖子中,我将为您提供在 Magento 2 中获取愿望清单产品集合的三个步骤。所有步骤都很容易遵循,我相信您将能够解决您商店的愿望清单产品集合当前的问题。

3步获取心愿单产品系列

  • 第 1 步:创建 WishlistProducts 块
  • 第 2 步:插入phtml文件
  • 第 3 步:刷新缓存和测试结果

第 1 步:创建 WishlistProducts 块

要获得愿望清单产品集合,首先,您需要创建一个WishlistProducts块。为此,请按照路径Example/Productslider/Block/WishlistProducts.php添加以下代码:

<?php

namespace Example\Productslider\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 Example\Productslider\Helper\Data;
/**
 * Class FeaturedProducts
 * @package Example\Productslider\Block
 */
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;
    }
}

步骤2:插入phtml文件

将集合放入区块后,现在您可以按照此代码片段从区块中获取产品集合Example/HelloWorld/view/frontend/templates/list.phtml

然后,请在 phtml 文件中插入以下代码。

<?php
$collection = $block->getProductCollection();
foreach ($collection as $_product) {
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

第 3 步:刷新缓存和测试结果

最后,让我们刷新缓存并测试结果。