0

I'm trying to getting the customer details in wishlist instead of showing the customer_id, I do in following way

/**
 * @var \Magento\Wishlist\Model\WishlistFactory
 */
protected $_wishlistFactory;

public function __construct(
    \Magento\Wishlist\Model\WishlistFactory $wishlistFactory){
    $this->_wishlistFactory = $wishlistFactory;
}

public function getWishlistByCustomerId($customerId)
{
    $wishlistCollections = $this->_wishlistFactory->create()->getCollection();
    return $wishlistCollections->getData();
}

form this I got whole list wishlists with customer_id

[
{
    "wishlist_id": "2",
    "customer_id": "8",
    "shared": "0",
    "sharing_code": "138258069f07f9e03395ac6c71a02eff",
    "updated_at": "2014-01-03 12:41:46"
},
{
    "wishlist_id": "3",
    "customer_id": "2",
    "shared": "0",
    "sharing_code": "0b29c50279346cba61010443e0410cb8",
    "updated_at": "2013-12-06 10:48:04"
}
]

but my acceptation is how to show it in like

[
{
    "wishlist_id": "2",
    "customer_id": {
                        //TODO Customer Details
                    },
    "shared": "0",
    "sharing_code": "138258069f07f9e03395ac6c71a02eff",
    "updated_at": "2014-01-03 12:41:46"
}
]

Is it possible to show this, I have try buy looping the wishlist and send the customer_id to \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface but it take more time. Is there any shortest and coolest way to show the stuffs of foriegn keys.

Thanks in advance

1 Answer 1

0

If you want do that on frontend, when Customer is logged in, get it from session:

protected $_customerSession;

public function __construct(
        \Magento\Customer\Model\Session $customerSession,
    ) {
        $this->_customerSession = $customerSession;
    }

public function getCurrentCustomer(){
 if($this->_customerSession->isLoggedIn()):
        echo $customerGroup=$this->_customerSession->getCustomer();
    endif;
}

Otherwise You have to load Customer.

Where do you want use this function?
It may be important to solve it better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.