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 wishlists with friends and family? A custom wishlist 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.
In this guide, we’ll walk through how to implement a custom wishlist 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.
Prerequisites
- 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
First, we need to create a new module to handle our wishlist sharing logic. Here’s how:
app/code/Magefine/WishlistShare/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Magefine_WishlistShare',
__DIR__
);
Next, 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 wishlist 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('wishlist/share/index', ['wishlist_id' => $this->getWishlistId()]);
}
}
Step 3: Create a Frontend Controller
Now, 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 $wishlistFactory;
public function __construct(
Context $context,
WishlistFactory $wishlistFactory
) {
$this->wishlistFactory = $wishlistFactory;
parent::__construct($context);
}
public function execute()
{
$wishlistId = $this->getRequest()->getParam('wishlist_id');
$wishlist = $this->wishlistFactory->create()->load($wishlistId);
if (!$wishlist->getId()) {
$this->messageManager->addErrorMessage(__('Wishlist not found.'));
return $this->_redirect('*/');
}
// Render the shared wishlist page
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}
Step 4: Add a Sharing Link to the Wishlist Page
Override the default wishlist template to include a sharing button:
app/code/Magefine/WishlistShare/view/frontend/templates/wishlist.phtml
<?php if ($block->getWishlist()->getItemsCount()): ?>
<div class="wishlist-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 wishlist, 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
After setting up the module, run:
php bin/magento setup:upgrade
php bin/magento cache:flush
Now, check your wishlist page—you should see a "Share Wishlist" button!
Final Thoughts
Adding a custom wishlist 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!