1

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;
    }
}
  • Can you describe what problem you are facing with your code? It will be helpful in finding the solution. – Jaimin Sutariya Nov 1 '18 at 8:07
  • @JaiminSutariya some how it is not appearing on the detail page – sandip Nov 3 '18 at 7:40
1

Please update your files with below code.

di.xml

<type name="Magento\Catalog\Block\Product\View\Gallery">
    <plugin name="Wishlist_Count::Count" type="Wishlist\Count\Plugin\Catalog\Product\View\Wishcount"/>
</type>

Data.php

public function renderWishlistCount(\Magento\Catalog\Model\Product $product)
{
    $html = '';

    $id    = $this->_storeManager->getStore(true)->getId();
    $model = $this->_objectManager->create('Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory');
    $wishlistCollection = $model->create()
        ->addFieldToFilter('store_id', ['like' => "%$id%"])
        ->addFieldToFilter('product_id', ['eq' => $product->getId()]);
    $html .= count($wishlistCollection);
    return $html;
}

I would recommend not to use objectManager. You should inject the class to __construct().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.