5

I want to override a block function in _createWishlistItemCollection located in folder /vendor/magento/module-wishlist/Block/AbstractBlock.php

I have used the plugin method to override the block method.

This is my di.xml file (/Vendor/Module/etc/frontend/di.xml)

<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"/>
<type name="Magento\Wishlist\Block\Share\Email\Items">
    <plugin name="multiwishlistsharewishlist"
            type="Vendor\Module\Plugin\Share\Email\Items"
            sortOrder="10"
            disabled="false"/>
</type>

<type name="Magento\Wishlist\Block\Share\Wishlist">
    <plugin name="mwsharewishlist"
            type="Vendor\Module\Plugin\Share\Wishlist"
            sortOrder="10"
            disabled="false"/>
</type>

Then I created a file in /Vendor/Module/Plugin/Share/Email/Items.php

<?php

namespace Vendor\Module\Plugin\Share\Email;

class Items
{
    /**
     * Create wishlist item collection
     *
     * @return Mage_Wishlist_Model_Resource_Item_Collection
     */


    public function beforeCreateWishlistItemCollection()    {

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/wishlist1.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Your text message');
    }


} 

The controller file Vendor\Module\Controller\Index\Send.php is below:

<?php


namespace Vendor\Module\Controller\Index;

use Magento\Framework\App\Action;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Session\Generic as WishlistSession;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Result\Layout as ResultLayout;

class Send extends \Magento\Wishlist\Controller\Index\Send
{
    public function execute()
    {

/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        if (!$this->_formKeyValidator->validate($this->getRequest())) {
            $resultRedirect->setPath('*/*/');
            return $resultRedirect;
        }

        $wishlist = $this->wishlistProvider->getWishlist();
        if (!$wishlist) {
            throw new NotFoundException(__('Page not found.'));
        }

        $sharingLimit = $this->_wishlistConfig->getSharingEmailLimit();
        $textLimit = $this->_wishlistConfig->getSharingTextLimit();
        $emailsLeft = $sharingLimit - $wishlist->getShared();

        $emails = $this->getRequest()->getPost('emails');
        $emails = empty($emails) ? $emails : explode(',', $emails);

        $error = false;
        $message = (string)$this->getRequest()->getPost('message');
        if (strlen($message) > $textLimit) {
            $error = __('Message length must not exceed %1 symbols', $textLimit);
        } else {
            $message = nl2br(htmlspecialchars($message));
            if (empty($emails)) {
                $error = __('Please enter an email address.');
            } else {
                if (count($emails) > $emailsLeft) {
                    $error = __('This wish list can be shared %1 more times.', $emailsLeft);
                } else {
                    foreach ($emails as $index => $email) {
                        $email = trim($email);
                        if (!\Zend_Validate::is($email, 'EmailAddress')) {
                            $error = __('Please enter a valid email address.');
                            break;
                        }
                        $emails[$index] = $email;
                    }
                }
            }
        }

        if ($error) {
            $this->messageManager->addError($error);
            $this->wishlistSession->setSharingForm($this->getRequest()->getPostValue());
            $resultRedirect->setPath('*/*/share');
            return $resultRedirect;
        }
//$mid = $this->getRequest()->getPost('multiwishlist_id');echo "test".$mid;exit;
        /** @var \Magento\Framework\View\Result\Layout $resultLayout */
        $resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
        $this->addLayoutHandles($resultLayout);
        $this->inlineTranslation->suspend();

        $sent = 0;

        try {
            $customer = $this->_customerSession->getCustomerDataObject();
            $customerName = $this->_customerHelperView->getCustomerName($customer);

            $message .= $this->getRssLink($wishlist->getId(), $resultLayout);
            $emails = array_unique($emails);
            $sharingCode = $wishlist->getSharingCode();

            try {

                foreach ($emails as $email) {
                    $transport = $this->_transportBuilder->setTemplateIdentifier(
                        $this->scopeConfig->getValue(
                            'wishlist/email/email_template',
                            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
                        )
                    )->setTemplateOptions(
                        [
                            'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                            'store' => $this->storeManager->getStore()->getStoreId(),
                        ]
                    )->setTemplateVars(
                        [
                            'customer' => $customer,
                            'customerName' => $customerName,
                            'salable' => $wishlist->isSalable() ? 'yes' : '',
                            'items' => $this->getWishlistItems($resultLayout),
                            'viewOnSiteLink' => $this->_url->getUrl('*/shared/index', ['code' => $sharingCode]),
                            'message' => $message,
                            'store' => $this->storeManager->getStore(),
                        ]
                    )->setFrom(
                        $this->scopeConfig->getValue(
                            'wishlist/email/email_identity',
                            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
                        )
                    )->addTo(
                        $email
                    )->getTransport();

                    $transport->sendMessage();

                    $sent++;
                }
            } catch (\Exception $e) {
                $wishlist->setShared($wishlist->getShared() + $sent);
                $wishlist->save();
                throw $e;
            }
            $wishlist->setShared($wishlist->getShared() + $sent);
            $wishlist->save();

            $this->inlineTranslation->resume();

            $this->_eventManager->dispatch('wishlist_share', ['wishlist' => $wishlist]);
            $this->messageManager->addSuccess(__('Your wish list has been shared.'));
            $resultRedirect->setPath('*/*', ['wishlist_id' => $wishlist->getId()]);
            return $resultRedirect;
        } catch (\Exception $e) {
            $this->inlineTranslation->resume();
            $this->messageManager->addError($e->getMessage());
            $this->wishlistSession->setSharingForm($this->getRequest()->getPostValue());
            $resultRedirect->setPath('*/*/share');
            return $resultRedirect;
        }
    }

    /**
     * Prepare to load additional email blocks
     *
     * Add 'wishlist_email_rss' layout handle.
     * Add 'wishlist_email_items' layout handle.
     *
     * @param \Magento\Framework\View\Result\Layout $resultLayout
     * @return void
     */
    protected function addLayoutHandles(ResultLayout $resultLayout)
    {
        if ($this->getRequest()->getParam('rss_url')) {
            $resultLayout->addHandle('wishlist_email_rss');
        }
        $resultLayout->addHandle('wishlist_email_items');
    }

    /**
     * Retrieve RSS link content (html)
     *
     * @param int $wishlistId
     * @param \Magento\Framework\View\Result\Layout $resultLayout
     * @return mixed
     */
    protected function getRssLink($wishlistId, ResultLayout $resultLayout)
    {
        if ($this->getRequest()->getParam('rss_url')) {
            return $resultLayout->getLayout()
                ->getBlock('wishlist.email.rss')
                ->setWishlistId($wishlistId)
                ->toHtml();
        }
    }

    /**
     * Retrieve wishlist items content (html)
     *
     * @param \Magento\Framework\View\Result\Layout $resultLayout
     * @return string
     */
    protected function getWishlistItems(ResultLayout $resultLayout)
    {

$mid = $this->getRequest()->getPost('multiwishlist_id');
        return $resultLayout->getLayout()
            ->getBlock('wishlist.email.items')
->setMid($mid)
            ->toHtml();
    }
}

But the block function is not getting overrided and the log value is not getting.

4

Plugins cannot be used with non-public method: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html.
In your case, we should use Preference to override these classes: Magento\Wishlist\Block\Share\Email\Items, Magento\Wishlist\Block\Share\Wishlist.

Vendor/Module/etc/frontend/di.xml

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

Vendor/Module/Block/Share/Email/Items.php

namespace Vendor\Module\Block\Share\Email;

classe Items extends \Magento\Wishlist\Block\Share\Email\Items
{
     protected function _createWishlistItemCollection() {
       ......
    }
}
  • I did exactly this for protected function _prepareLayout() in CatalogSearch/Block/Result.php, but it's not working. Anyone get this working? I can, however, successfully override non-protected functions such as getSearchQueryText() just fine. – thdoan Mar 7 '17 at 8:12
  • trying to override _prepareLayout() function of module-customer\Block\Address\Book.php. Not working .. – Sejal Shah Oct 11 '17 at 7:09
  • @thdoan did you got solution – Jaisa Nov 5 '18 at 5:43
-1

Please create a custom module and follow below steps:

Step1 :- Go to

Custom Vendor Name/Custom Module Name/etc/frontend/di.xml

<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="Custom Vendor Name\Custom Module Name\Block\Share\Email\Items"/>
</config>

Step2 :- Go to

Custom Vendor Name/Custom Module Name/Block/Share/Email/Items.php

namespace Custom Vendor Name\Custom Module Name\Block\Share\Email;

class Items extends \Magento\Wishlist\Block\Share\Email\Items
{
     protected function _createWishlistItemCollection() {
      //put your code
    }
}

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.