我试图通过编辑 Magento 的 API items() 函数
获取产品的图片 URL。我使用了 $product->getImageUrl()
来获取 URL 但我得到了错误的 URL。
我得到的 URL 是我们为没有图像的产品 放置的默认图像(图像即将推出像图片的 url)。
我正在使用 XML-RPC 从 Android Client 调用函数。
我得到的产品的其他详细信息是正确的,但是我得到的产品的 URL 是错误的。而且,我获得的不同产品的所有URL 都是相同的。
仅供引用,我在响应中获得的 URL 如下:
http://192.168.1.237/machinetest/media/catalog/product/cache/0/image/265x/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/image.jpg
我正在编辑的功能如下:
public function items($filters = null, $store = null)
{
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($this->_getStoreId($store))
->addAttributeToSelect('name');
if (is_array($filters)) {
try {
foreach ($filters as $field => $value) {
if (isset($this->_filtersMap[$field])) {
$field = $this->_filtersMap[$field];
}
$collection->addFieldToFilter($field, $value);
}
} catch (Mage_Core_Exception $e) {
$this->_fault('filters_invalid', $e->getMessage());
}
}
$result = array();
foreach ($collection as $product) {
//$result[] = $product->getData();
$result[] = array( // Basic product data
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'category_ids'=> $product->getCategoryIds(),
'url_path' => $product-> getImageUrl() // Added the Method here
);
}
return $result;
}
最佳答案
只要写下请尝试这种方式..你会得到最上面的解决方案
您可以使用此代码获取图片,只需通过它即可获取图片
public function items($filters = null, $store = null)
{
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($this->_getStoreId($store))
->addAttributeToSelect('name');
if (is_array($filters)) {
try {
foreach ($filters as $field => $value) {
if (isset($this->_filtersMap[$field])) {
$field = $this->_filtersMap[$field];
}
$collection->addFieldToFilter($field, $value);
}
} catch (Mage_Core_Exception $e) {
$this->_fault('filters_invalid', $e->getMessage());
}
}
$result = array();
foreach ($collection as $product) {
// $result[] = $product->getData();
$_product = Mage::getModel('catalog/product')->load($product->getId());
$_image=$_product->getImageUrl();
$result[] = array( // Basic product data
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'category_ids'=> $product->getCategoryIds(),
'image_url_path' => $_image
);
}
return $result;
}
希望一切顺利 如果您有任何疑问,请告诉我,我会帮助您!
关于php - 如何编辑Magento的API函数 "items()"获取商品的ImageUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12210540/