【问题标题】:Remove disabled product from wishlist从愿望清单中删除禁用的产品
【发布时间】:2019-05-22 05:17:55
【问题描述】:

我需要从愿望清单中删除所有禁用的产品,为此我在Magento_Wishlist/templates/item/list.phtml 中写了一个产品状态检查,如下所示:

if($product->getStatus() == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED):
------------
endif;

我还在标题部分添加了一个插件来更新愿望清单计数,如下所示:

/etc/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Wishlist\CustomerData\Wishlist">
        <plugin name="vendor-customer-wishlist"
                type="Vendor\Customer\Plugin\WishlistPlugin" sortOrder="1" />
    </type>
</config>

供应商/客户/插件/WishlistPlugin

<?php
namespace Vendor\Customer\Plugin;

use Magento\Catalog\Model\Product\Attribute\Source\Status;

/**
 * Class WishlistPlugin
 * @package Vendor\Customer\Plugin
 */
class WishlistPlugin
{
    /**
     * @var \Magento\Wishlist\Helper\Data
     */
    protected $wishlistHelper;

    /**
     * @param \Magento\Wishlist\Helper\Data $wishlistHelper
     */
    public function __construct(
        \Magento\Wishlist\Helper\Data $wishlistHelper
    ) {
        $this->wishlistHelper = $wishlistHelper;
    }

    /**
     * Plugin function after get section data
     *
     * @param \Magento\Wishlist\CustomerData\Wishlist $subject
     * @param $result
     * @return mixed
     */
    public function afterGetSectionData(\Magento\Wishlist\CustomerData\Wishlist $subject, $result)
    {
        $disabledProductsCount = 0;

        foreach ($this->wishlistHelper->getWishlistItemCollection() as $item) {
            if($item->getProduct()->getStatus() == Status::STATUS_DISABLED) $disabledProductsCount++;
        }

        $counterNumber = $this->wishlistHelper->getItemCount();

        if($disabledProductsCount) {
            $counterNumber -= $disabledProductsCount;
        }

        $result['counter'] = $counterNumber;

        return $result;
    }


}

但是当我在愿望清单中添加三个项目并从中禁用一个产品时,愿望清单计数器没有正确更新,它仍然是三个。我该如何解决这个问题?

【问题讨论】:

    标签: product magento2 catalog


    【解决方案1】:

    这取决于你在哪里添加它,可能最好在页脚中调用它(它会触发该客户的愿望清单计数在每个页面上更新)。

    没有简单的方法可以使您的插件(后端)中的数据无效,因此对于您正在做的事情,因为您永远不知道何时在前端禁用产品,所以我们可以假设刷新每个页面上的愿望清单数据加载是最好的选择...

    例如(在页脚中添加,更改为您的要求):

    require([
       ....
       'Magento_Customer/js/customer-data'
    ], function (...,customerData) {
       "use strict";
       ...
       var sections = ['wishlist'];
       customerData.invalidate(sections);
       customerData.reload(sections, true);
       ...
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多