Magento - 我可以对整个产品系列进行JSON编码吗?

时间:2011-05-27 19:45:30

标签: ajax magento

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

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

1 个答案:

答案 0 :(得分:0)

部分:

  

array_merge($ product_to_encode,$ collection_to_encode);

将无效,因为array_merge返回合并的数组。在您的情况下,合并的数组不会被变量捕获..

以下部分应该有效:

  

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