Magento-我可以对整个产品集合进行JSON编码吗



这就是我目前所拥有的。它看起来有关系吗?或者有更简单的方法吗?

protected function _encodedProductCollection()
{
    $productcollection = $this->_getProductCollection();
    $productmodel = Mage::getModel('catalog/product');
    $categorymodel = Mage::getModel('catalog/category');
    $collection_to_encode = array();
    foreach( $productcollection as $product )
    {
        $product_to_encode = array();
        $thisproduct = $productmodel->load($product->getId());
        $productsname = $thisproduct->getName();
        $productsid = $thisproduct->getId();
        $productcategories = $thisproduct->getCategoryIds();
        $productscategoryname = $categorymodel->load($productcategories[0])->getName();  //we'll start with the first category name.
        //$productsimageurl = $thisproduct->getImageUrl();  //gets the image url if we need it later
        //$altcategorymethod = ($thisproduct->getCategory() ? $thisproduct->getCategory()->getName() : 'No Category');  //we may use this one instead.

        $product_to_encode[] = array(
            'id' => $productsid,
            'name' => $productsname,
            'category' => $productscategoryname );
        array_merge($product_to_encode, $collection_to_encode); 
    }
    $encoded_products = json_encode($collection_to_encode);
    return $encoded_products;  //leave this if we leave it as a function - we may port it all over to the template itself.
}

零件:

array_merge($product_to_encode,$collection_to_encade);

不起作用,因为array_merge返回合并后的数组。在您的情况下,合并的数组没有被变量捕获。。

以下部分应使其发挥作用:

$collection_to_encode=array_merge($product_to_encode,$collection_to_encode);

最新更新