How to Get Wishlist Data Using Customer ID in Magento 2- Magento source24

 

Hello magento friends todays in this blog post im share with you to How to Get Wishlist Data Using Customer ID in Magento 2.


How to Get Wishlist Data Using Customer ID in Magento 2- Magento source24


In Magento 2, you can retrieve the wishlist data for a specific customer using their customer ID. To do this, you can use Magento's dependency injection mechanism to get the necessary classes and then utilize the built-in wishlist repository to fetch the data. Here's a step-by-step guide on how to achieve this programmatically:


  1. Create a custom module (if you haven't already) or use an existing one. Let's assume your module is named Vendor_Module.
  2. Create a PHP class (e.g., WishlistData) within your module to handle the retrieval of wishlist data. This class should have a constructor to inject the required dependencies.
  3. Inject the necessary classes (\Magento\Wishlist\Model\WishlistFactory and \Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory) into the WishlistData class. These classes will help you fetch wishlist and wishlist item data, respectively.
  4. Create a public method (e.g., getWishlistDataByCustomerId) that accepts a customer ID as a parameter and returns the wishlist data.
  5. Use the injected dependencies to load the wishlist collection and items based on the provided customer ID.
  6. Return the wishlist data in a suitable format (e.g., array, JSON, etc.) from the getWishlistDataByCustomerId method.


Here's an example implementation for the WishlistData class:



// app/code/Vendor/Module/Model/WishlistData.php

namespace Vendor\Module\Model;

use Magento\Wishlist\Model\WishlistFactory;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;

class WishlistData
{
    protected $wishlistFactory;
    protected $itemCollectionFactory;

    public function __construct(
        WishlistFactory $wishlistFactory,
        CollectionFactory $itemCollectionFactory
    ) {
        $this->wishlistFactory = $wishlistFactory;
        $this->itemCollectionFactory = $itemCollectionFactory;
    }

    public function getWishlistDataByCustomerId($customerId)
    {
        $wishlistData = [];

        // Load customer's wishlist based on customer ID
        $wishlist = $this->wishlistFactory->create()->loadByCustomerId($customerId);
        
        if ($wishlist->getId()) {
            $wishlistData['wishlist_id'] = $wishlist->getId();
            $wishlistData['customer_id'] = $customerId;
            $wishlistData['items'] = [];

            // Load wishlist items
            $itemCollection = $this->itemCollectionFactory->create();
            $itemCollection->addFieldToFilter('wishlist_id', $wishlist->getId());

            foreach ($itemCollection as $item) {
                // Customize the data you want to fetch from the wishlist item
                $itemData = [
                    'product_id' => $item->getProductId(),
                    'product_name' => $item->getProduct()->getName(),
                    'product_sku' => $item->getProduct()->getSku(),
                    // Add more fields as needed
                ];
                $wishlistData['items'][] = $itemData;
            }
        }

        return $wishlistData;
    }
}



Now, you can use this class and the getWishlistDataByCustomerId method to fetch the wishlist data for a specific customer ID from anywhere in your Magento codebase.

Remember to adjust the class and method names, as well as the module structure, according to your specific module's setup.







Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post