On product detail page how many times a particular product has been added to Wishlist for simple product and configurable product in single store environment?
Trying below mentioned code: Di.xml is as follows:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Block\Product\Image">
<plugin name="Wishlist_Count::Count" type="Wishlist\Count\Plugin\Catalog\Product\Wishcount"/>
</type>
</config>
Wishlist\Count\Plugin\Catalog\Product\Wishcount file code
namespace Wishlist\Count\Plugin\Catalog\Product\View;
class Wishcount
{
protected $_helper;
protected $scopeConfig;
public function __construct(
\Wishlist\Count\Helper\Data $helper,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
$this->_helper = $helper;
$this->_scopeConfig = $scopeConfig;
}
public function afterToHtml(
\Magento\Catalog\Block\Product\View\Gallery $subject,
$result
) {
$product = $subject->getProduct();
$template = $subject->getTemplate();//view/frontend/templates/product/view/gallery.phtml
$name = $subject->getNameInLayout();
if ($product && $name == "product.info.media.image") {
$result .= $this->_helper->renderWishlistCount( $product );
}
return $result;
} }
Helper class code is as follows:
namespace Wishlist\Count\Helper;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Store\Model\ScopeInterface;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $_storeManager;
protected $_counts;
protected $_registry;
protected $_resultPageFactory;
protected $_objectManager;
protected $_messageManager;
protected $_scopeConfig;
protected $_statusId = null;
private $layoutFactory;
private $_productTypeConfigurable;
public function __construct(
\Magento\Framework\Registry $registry,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\View\LayoutFactory $layoutFactory,
\Magento\ConfigurableProduct\Model\Product\Type\Configurable $catalogProductTypeConfigurable,
\Magento\Framework\App\Helper\Context $context
) {
parent::__construct($context);
$this->_registry = $registry;
$this->_resultPageFactory = $resultPageFactory;
$this->_objectManager = $objectManager;
$this->_messageManager = $messageManager;
$this->_scopeConfig = $context->getScopeConfig();
$this->_storeManager = $storeManager;
$this->layoutFactory = $layoutFactory;
$this->_productTypeConfigurable = $catalogProductTypeConfigurable;
}
public function renderWishlistCount(\Magento\Catalog\Model\Product $product)
{
$html = '';
$id = $this->_storeManager->getStore(true)->getId();
$model = $this->_objectManager->create('Magento\Wishlist\Model\Wishlist');
$html = $model->getCollection()
->addFieldToFilter('store_id', ['like' => "%$id%"])
->addFieldToFilter('product_id', ['like' => "%$product->getId()%"])
return $html;
}
}