How to Implement a Custom Wishlist Sharing Feature in Magento 2

Why Add Wishlist Sharing to Your Magento 2 Store?

Wishlists are a powerful tool for boosting customer engagement and driving sales. But what if your customers could easily share their lista de deseoss with friends and family? A custom lista de deseos sharing feature can turn casual browsers into potential buyers by allowing users to send their curated product selections via email, social media, or direct links.

En esta guía,’ll walk through cómo implement a custom lista de deseos sharing feature in Magento 2. Whether you're a developer or a store owner looking to enhance functionality, this step-by-step tutorial will help you get it done.

Requisitos previos

  • Magento 2.x installed
  • Basic knowledge of PHP and Magento 2 module development
  • Access to your store’s file system (via SSH or FTP)

Step 1: Create a Custom Module

Primero, we need to create a new module to handle our lista de deseos sharing logic. Here’s how:

app/code/Magefine/WishlistShare/registration.php
<?php  
use Magento\Framework\Component\ComponentRegistrar;  

ComponentRegistrar::register(  
    ComponentRegistrar::MODULE,  
    'Magefine_WishlistShare',  
    __DIR__  
);

A continuación, create the module’s etc/module.xml file:

app/code/Magefine/WishlistShare/etc/module.xml
<?xml version="1.0"?>  
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">  
    <module name="Magefine_WishlistShare" setup_version="1.0.0">  
        <sequence>  
            <module name="Magento_Wishlist"/>  
        </sequence>  
    </module>  
</config>

Step 2: Extend the Wishlist Block

We’ll override the default lista de deseos block to add our sharing functionality. Create:

app/code/Magefine/WishlistShare/Block/Wishlist.php
<?php  
namespace Magefine\WishlistShare\Block;  

class Wishlist extends \Magento\Wishlist\Block\Customer\Wishlist  
{  
    public function getShareUrl()  
    {  
        return $this->getUrl('lista de deseos/share/index', ['lista de deseos_id' => $this->getWishlistId()]);  
    }  
}

Step 3: Create a Frontend Controller

Ahora, let’s set up a controller to handle the sharing logic:

app/code/Magefine/WishlistShare/Controller/Share/Index.php
<?php  
namespace Magefine\WishlistShare\Controller\Share;  

use Magento\Framework\App\Action\Action;  
use Magento\Framework\App\Action\Context;  
use Magento\Wishlist\Model\WishlistFactory;  

class Index extends Action  
{  
    protected $lista de deseosFactory;  

    public function __construct(  
        Context $context,  
        WishlistFactory $lista de deseosFactory  
    ) {  
        $this->lista de deseosFactory = $lista de deseosFactory;  
        parent::__construct($context);  
    }  

    public function execute()  
    {  
        $lista de deseosId = $this->getRequest()->getParam('lista de deseos_id');  
        $lista de deseos = $this->lista de deseosFactory->create()->load($lista de deseosId);  

        if (!$lista de deseos->getId()) {  
            $this->messageManager->addErrorMessage(__('Wishlist not found.'));  
            return $this->_redirect('*/');  
        }  

        // Render the shared lista de deseos page  
        $this->_view->loadLayout();  
        $this->_view->renderLayout();  
    }  
}

Step 4: Add a Sharing Link to the Wishlist Page

Override the default lista de deseos template to include a sharing button:

app/code/Magefine/WishlistShare/view/frontend/templates/lista de deseos.phtml
<?php if ($block->getWishlist()->getItemsCount()): ?>  
    <div class="lista de deseos-share">  
        <a href="<?= $block->getShareUrl() ?>" class="action share">  
            <span><?= __('Share Wishlist') ?></span>  
        </a>  
    </div>  
<?php endif; ?>

Step 5: Enable Email Sharing (Optional)

To allow users to email their lista de deseos, extend the sharing functionality:

app/code/Magefine/WishlistShare/Controller/Share/Email.php
<?php  
namespace Magefine\WishlistShare\Controller\Share;  

use Magento\Framework\App\Action\Action;  
use Magento\Framework\Mail\Template\TransportBuilder;  

class Email extends Action  
{  
    protected $transportBuilder;  

    public function __construct(  
        Context $context,  
        TransportBuilder $transportBuilder  
    ) {  
        $this->transportBuilder = $transportBuilder;  
        parent::__construct($context);  
    }  

    public function execute()  
    {  
        $emails = $this->getRequest()->getParam('emails');  
        $message = $this->getRequest()->getParam('message');  

        // Send email logic here  
    }  
}

Step 6: Test & Deploy

Después de setting up the module, run:

php bin/magento setup:actualización  
php bin/magento cache:flush

Ahora, check your lista de deseos page—you should see a "Share Wishlist" button!

Reflexiones finales

Adding a custom lista de deseos sharing feature can enhance customer engagement and drive sales. With this guide, you can implement it smoothly in Magento 2. Need a ready-made solution? Check out our Magento 2 extensions for optimized performance!

Got questions? Drop them in the comments below!