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 client engagement and driving sales. But what if your clients could easily share their liste de souhaitss with friends and family? A custom liste de souhaits sharing fonctionnalité can turn casual blignesers into potential buyers by allowing utilisateurs to send their curated product selections via e-mail, social media, or direct links.

Dans ce guide, nous’ll walk through comment implement a custom liste de souhaits sharing fonctionnalité in Magento 2. Whether you're a développeur or a propriétaire de boutique looking to enhance fonctionality, this étape-by-étape tutorial will help you get it done.

Prérequis

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

Step 1: Create a Custom Module

Premièrement, we need to create a new module to handle our liste de souhaits sharing logic. Here’s how:

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

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

Ensuite, create the module’s etc/module.xml fichier:

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 liste de souhaits block to add our sharing fonctionality. Create:

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

class Wishlist extends \Magento\Wishlist\Block\Customer\Wishlist  
{  
    public fonction getShareUrl()  
    {  
        return $this->getUrl('liste de souhaits/share/index', ['liste de souhaits_id' => $this->getWishlistId()]);  
    }  
}

Step 3: Create a Frontend Controller

Maintenant, let’s set up a contrôleur 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 $liste de souhaitsFactory;  

    public fonction __construct(  
        Context $context,  
        WishlistFactory $liste de souhaitsFactory  
    ) {  
        $this->liste de souhaitsFactory = $liste de souhaitsFactory;  
        parent::__construct($context);  
    }  

    public fonction execute()  
    {  
        $liste de souhaitsId = $this->getRequest()->getParam('liste de souhaits_id');  
        $liste de souhaits = $this->liste de souhaitsFactory->create()->load($liste de souhaitsId);  

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

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

Step 4: Add a Sharing Link to the Wishlist Page

Override the default liste de souhaits template to include a sharing button:

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

Step 5: Enable Email Sharing (Optional)

To allow utilisateurs to e-mail their liste de souhaits, extend the sharing fonctionality:

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 fonction __construct(  
        Context $context,  
        TransportBuilder $transportBuilder  
    ) {  
        $this->transportBuilder = $transportBuilder;  
        parent::__construct($context);  
    }  

    public fonction execute()  
    {  
        $e-mails = $this->getRequest()->getParam('e-mails');  
        $message = $this->getRequest()->getParam('message');  

        // Send e-mail logic here  
    }  
}

Step 6: Test & Deploy

Après setting up the module, run:

php bin/magento setup:mise à jour  
php bin/magento cache:flush

Maintenant, check your liste de souhaits page—you should see a "Share Wishlist" button!

Réflexions finales

Adding a custom liste de souhaits sharing fonctionnalité can enhance client 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!