0

I am having difficulties with wishlist collections, i want them to be more like regular collections of filter like regular collections but this does not seem to be the case. Methods like getSku do not work and therefor my existing templates are not working very will with these collections.

I want to basically convert this (and some other collections) to get wishlist items by category:

public function _getProductCollectionCat($category)
{
    /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
    $collectioncat = $this->_productCollectionFactory->create();

    $collectioncat = $this->_addProductAttributesAndPrices(
        $collectioncat
    )->setPageSize(
        $this->getProductsCount()
    )->setCurPage(
        1
    );

    $collectioncat->getSelect()->join('catalog_product_entity_tier_price as tier', 'price_index.entity_id = tier.entity_id');

    $collectioncat->addCategoryFilter($this->categoryRepository->get($category));

    $collectioncat->addFinalPrice()
          ->addMinimalPrice()
          ->getSelect()
          ->where('tier.all_groups = 0')
          ->where('tier.value < price_index.price');


    return $collectioncat;
} 

This however filters by products with a group price i just want to filter by if the product is in the users wishlist. Can i just join a table and filter similarly to how tier prices were filtered above?

Something Like Below:

public function getWishlistProducts() {
    /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */

    $customerId = $this->_customerSession->getCustomer()->getId();
    $wishlistId = $this->_wishlistRepository->create()->loadByCustomerId($customerId, true)->getId();


    $collectioncat = $this->_productCollectionFactory->create();

    $collectioncat = $this->_addProductAttributesAndPrices(
        $collectioncat
    )->setPageSize(
        $this->getProductsCount()
    )->setCurPage(
        1
    );

    $collectioncat->getSelect()->join('wishlist_item as wishlist', 'price_index.entity_id = wishlist.product_id');

    $collectioncat->addCategoryFilter($this->categoryRepository->get($category));

    $collectioncat->addFinalPrice()
          ->addMinimalPrice()
          ->getSelect()
          ->where('wishlist.wishlist_id = ' . $wishlistId);


    return $collectioncat;
}

1 Answer 1

0

I achieved this with the following:

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

public function getWishlistProductsCat($category) {
    /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */

    $customerId = $this->_customerSession->getCustomer()->getId();
    $wishlistId = $this->_wishlistRepository->create()->loadByCustomerId($customerId, true)->getId();

    $collectioncat = $this->_productCollectionFactory->create();

    $collectioncat = $this->_addProductAttributesAndPrices(
        $collectioncat
    )->setPageSize(
        $this->getProductsCount()
    )->setCurPage(
        1
    );

    $collectioncat->getSelect()->join('wishlist_item as wishlist', 'price_index.entity_id = wishlist.product_id');

    $collectioncat->addCategoryFilter($this->categoryRepository->get($category));

    $collectioncat->addFinalPrice()
          ->addMinimalPrice()
          ->getSelect()
          ->where('wishlist.wishlist_id = ' . $wishlistId);


    return $collectioncat;
}

Your Answer

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

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