0

Magento Enterprise v1.14.2

I'd like to use the standard catalog/product/list.phtml template to display a custom set of products. In this case, I want to display the contents of the customer's wishlist and I'm editing template/wishlist/shared.phtml.

I get the product IDs from the customer's wishlist, create a product collection, create a product_list block, but the block just refuses to output only the products in my collection - instead, it outputs all products on the website.

<?php 
    if ($this->hasWishlistItems()) {

        // Get product IDs from wishlsit
        foreach ($this->getWishlistItems() as $item) {
            $productIds[] = $item->getProductId();
        }

        // Create collection of those products
        $collection = Mage::getModel('catalog/product')
                          ->getCollection()
                          ->addFieldToFilter("entity_id", array("in" => $productIds));

        // Create product_list block using the list.phtml template
        $block = Mage::getModel('core/layout')
                     ->createBlock('catalog/product_list')
                     ->setCollection($collection)
                     ->setTemplate('catalog/product/list.phtml');

        echo $block->toHtml(); // Showing ALL PRODUCTS on website!

    }
?>

I've tried using setProductCollection() and setLoadedProductCollection() but neither works, and I'm pretty sure setCollection() is the correct function anyway.

What's going on?

0

Turns out it was a mostly unrelated fault. An SQL error was being generated by my collection's query string because of a Magento bug; the same issue faced in this question: "random" order clause added to my collection

To fix it, I added a setVisibility line to the collection as per the above link.

I was also missing addAttributeToSelect which is unrelated.

My final code:

    $collection = Mage::getModel('catalog/product')
                  ->getCollection()
                  ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
                  ->addAttributeToSelect("*")
                  ->addFieldToFilter("entity_id", array("in" => $productIds));

    $block = Mage::getModel('core/layout')
                 ->createBlock('catalog/product_list')
                 ->setTemplate('catalog/product/list.phtml')
                 ->setCollection($collection);

    return $block->toHtml();

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.