在 Fishpig 博客中获取关联产品计数

Get Associated Product Count in Fishpig Blog

我正在尝试显示与 fishpig 中的博客关联的产品数量。

我正在尝试以下方法,但它是 returning 空值。

$post->getAssociatedProducts(); 

函数

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;
    }

这个return产品算吗?

您列出的函数不能 return null。仅有的 return 类型是 false 或产品集合。

我搜索了代码库,发现该方法不属于任何现有的 class,因此我不确定您是从哪里得到的。也许它来自旧版本?

要使用最新版本的扩展获取 post 的关联产品,您将使用以下内容:

// 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);