Search code examples
phpmagentoblogsfishpig

Get Associated Product Count in Fishpig Blog


I'm trying to display the count of products which is associated with a blog in fishpig.

I'm trying the below method but it's returning null value.

$post->getAssociatedProducts(); 

Function

public function getAssociatedProducts($post)
    {
        if ($post instanceof Fishpig_Wordpress_Model_Post) {
            $productIds = $this->_getAssociatedWpEntityIds($post->getId(), 'product', 'post', 'post_id');

            try {
                foreach($post->getParentCategories() as $category) {
                    $productIds = array_merge($productIds, $this->_getAssociatedWpEntityIds($category->getId(), 'product', 'category', 'category_id'));
                }
            }
            catch (Exception $e) {
                $this->log($e->getMessage());
            }
            if (count($productIds) > 0) {
                $collection = Mage::getResourceModel('catalog/product_collection');     
                Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
                $collection->addAttributeToFilter('status', 1);
                $collection->addAttributeToFilter('entity_id', array('in' => $productIds));

                return $collection;
            }
        }

        return false;
    }

Will this return the product count?


Solution

  • The function you listed cannot return null. The only return types are false or a collection of products.

    I have searched the code base and that method is not part of any class that exists so I'm not sure where you got it from. Maybe it's from an old version?

    To get the associated products for a post using the latest version of the extension, you would use the following:

    // Get the associations helper
    $associationsHelper = Mage::helper('wordpress/associations');
    
    // Load a product collection based on $post
    $products = $associationsHelper->getAssociatedProductsByPost($post);
    
    // Get the number of products
    $productCount = count($products);