2

I Want currently logged user all wishlist products id's json array via ajax call ?

2

Add this below code in your ajax controller's function :

protected $customerSession;
protected $wishlist;
protected $jsonFactory;

public function __construct(
    ...
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Wishlist\Model\Wishlist $wishlist,
    \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
    \Magento\Framework\App\Action\Context $context
    ...
) {
    ...
    $this->customerSession = $customerSession;
    $this->wishlist = $wishlist;
    $this->jsonFactory = $jsonFactory;
    parent::__construct($context);
    ...
}

public function yourFunction(){
    $resultJson = $this->jsonFactory->create();
    $customer_id = $this->customerSession->getCustomer()->getId();
    $wishlist_collection = $this->wishlist->loadByCustomerId($customer_id, true)->getItemCollection();
    $product_ids = [];
    foreach ($wishlist_collection as $item) {
        $product_ids[] = $item->getProduct()->getId();
    }
    return $resultJson->setData(['product_ids' => $product_ids]);;
}

Hope, it maybe helpful for you.

  • it returns product_ids json array ? – Shafeel Sha Jul 22 at 6:37
  • Yes. But, not json array. Let me return json array. – Rohan Hapani Jul 22 at 6:38
  • okay its fine , i'll magne it – Shafeel Sha Jul 22 at 6:39
  • Check my updated answer. Now, it will return json array. – Rohan Hapani Jul 22 at 6:42
  • Hai @Rohan Hapani , l'm trying on it – Shafeel Sha Jul 22 at 9:36

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.