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

问题描述 投票:0回答:1

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

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.
}
php ajax magento
1个回答
0
投票

部分:

array_merge($product_to_encode, $collection_to_encode);

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

以下部分应该可以使其工作:

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

© www.soinside.com 2019 - 2024. All rights reserved.