2

I want to filter wishlist product collection with a unique id. I have tried to override a method _createWishlistItemCollection located at the path /vendor/magento/module-wishlist/Block/AbstractBlock.php using preference method.

This is my di.xml file

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Wishlist\Controller\Index\Send" type="Vendor\Module\Controller\Index\Send"/>

    <preference for="Magento\Wishlist\Block\Share\Email\Items" type="Vendor\Module\Block\Share\Email\Items"/>

</config>

This is my block file

namespace Vendor\Module\Block\Share\Email;

    class Items extends \Magento\Wishlist\Block\Share\Email\Items
    {
        protected function _createWishlistItemCollection() {
            $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/testmulti.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
            $logger->info('Your text message');
            $collection = $this->_getWishlist()->getItemCollection();
            if($this->getMid()){
               $id = $this->getMid();
               if($this->getMid() == 'default')
                    $id = 0;
              $collection ->addFieldToFilter('multiwishlist_id', $id);
            }
            return $collection;

        }

    }

It is not entering into the overrided file. Plese help me with a solution.

  • did you deployed the content using setup:static-content:deploy command, and before deploying delete the contents in var/generation files – Nitin Pawar Dec 22 '16 at 7:03
  • Yes, I have deployed the content. – Vindhuja Dec 22 '16 at 7:09
0

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">
        <preference for="Magento\Wishlist\Block\Share\Email\Items" type="Vendor\Module\Block\Share\Email\Items"
</config>

Block.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Wishlist block customer items
 *
 * @author     Magento Core Team <core@magentocommerce.com>
 */
namespace Vendor\Module\Block\Share\Email;

class Items extends \Magento\Wishlist\Block\Share\Email\Items
{
       public function __construct(
         \Magento\Framework\View\Element\Template\Context $context,        
         array $data = []
       ) {
            ...
             parent::__construct($context, $data);
             echo"<pre/>"; print_r("test");exit;
         } 
}

Try This code for testing After apply code remove var/ and public/static and apply setup:static-content:deploy.

  • I have added the following in construct . – Vindhuja Dec 22 '16 at 9:35
  • I have added the following in construct . public function __construct( \Magento\Catalog\Block\Product\Context $context, \Magento\Framework\App\Http\Context $httpContext, array $data = [] ) { // $this->httpContext = $httpContext; parent::__construct( $context, $data ); // echo "<pre/>"; print_r("test");exit; } But getting error as Incompatible argument type:Required type \Magento\Framework\App\Http\Context . Actual type:array; File : /app/code/Vendor/Module/Block/Share/Email/Items.php – Vindhuja Dec 22 '16 at 9:36
  • that means your class file is being called – Ronak Chauhan Dec 22 '16 at 9:39
  • use same constructor as parent used, and remove extra injections – Ronak Chauhan Dec 22 '16 at 9:40
  • I have used the same constructor as parent used.The block function located in /magento/module-wishlist/Block/Share/Email/Items.php has no construct method.So I have used the construct function located at /vendor/magento/module-wishlist/Block/AbstractBlock.php that extends the Items.php But still getting the error as Incompatible argument type:Required type . – Vindhuja Dec 22 '16 at 9:49

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.