您好我的代码中有特定产品ID 的特定 * 产品 *数组,我想要获得所有 * 产品数组 * ..... 我的代码是
**<?php
@ob_start();
@session_start();
ini_set('display_errors', 1);
//for order update
include '../../../../app/Mage.php';
Mage::app('default');
echo '<pre>';
if(isset($_REQUEST['productid'])){
$productId = $_REQUEST['productid'];
}else{
$productId = '12402'; // product ID 10 is an actual product, and used here for a test
}
$product = Mage::getModel('catalog/product')->load($productId); //load the product
echo 'product_id '.'=>'.$product->getId().'<br/>';
?>**
答案 0 :(得分:3)
你想要什么? $ product不是数组,它是一个对象。如果您想拥有所有产品,可以使用以下代码:
$collection = Mage::getResourceModel('catalog/product_collection');
foreach($collection as $product) {
echo $product->getId();
}
答案 1 :(得分:2)
在这里,我已经介绍了如何获得所有产品的名称,同样地,您可以获得所需的属性。
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*');
foreach ($collection as $product) {
echo $product->getName() . "<br />";
}
答案 2 :(得分:2)
function products($category_id){
$json = array('success' => true, 'products' => array());
$category = Mage::getModel ('catalog/category')->load($category_id);
$products = Mage::getResourceModel('catalog/product_collection')
// ->addAttributeToSelect('*')
->AddAttributeToSelect('name')
->addAttributeToSelect('price')
->addFinalPrice()
->addAttributeToSelect('small_image')
->addAttributeToSelect('image')
->addAttributeToSelect('thumbnail')
->addAttributeToSelect('short_description')
->addUrlRewrite()
->AddCategoryFilter($category);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$currencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
foreach($products as $product){
$json['products'][] = array(
'id' => $product->getId(),
'name' => $product->getName(),
'description' => $product->getShortDescription(),
'pirce' => Mage::helper('core')->currency($product->getPrice(), true, false), //." ".$currencyCode,
'href' => $product->getProductUrl(),
'thumb' => (string)Mage::helper('catalog/image')->init($product, 'thumbnail')
);
}
return $json;
}
完整插件REST API,请参阅https://github.com/app-z/magento-android-web-api/blob/master/web-api.php
答案 3 :(得分:0)
也许是这样的?
$collection = Mage::getResourceModel('catalog/product_collection');
foreach ($collection as $product) {
$data = $product->getData();
foreach ($data as $d)
var_dump($d);
}
答案 4 :(得分:0)
$term = Mage::helper('catalogsearch')->getQueryText();
$query = Mage::getModel('catalogsearch/query')->setQueryText($term)->prepare();
$fulltextResource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
Mage::getModel('catalogsearch/fulltext'),
$term,
$query
);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->getSelect()->joinInner(
array('search_result' => $collection->getTable('catalogsearch/result')),
$collection->getConnection()->quoteInto(
'search_result.product_id=e.entity_id AND search_result.query_id=?',
$query->getId()
),
array('relevance' => 'relevance')
);
$productIds = array();
$productIds = $collection->getAllIds(); // as per Amit Bera s' comment
Zend_Debug::dump($productIds);