【发布时间】: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;
}
}
但是当我在愿望清单中添加三个项目并从中禁用一个产品时,愿望清单计数器没有正确更新,它仍然是三个。我该如何解决这个问题?
【问题讨论】: