我想通过编辑 Magento的API items() function
来获取产品的图片网址。我使用$product-> getImageUrl()
来获取网址但我错了网址。
我得到的网址是默认图片,我们为产品放置了没有图片的图片(图片即将推出)喜欢Image的网址。
我使用 XML-RPC 从 Android客户端调用该功能。
我正在获得该产品的其他详细信息,但我获得的产品的网址是错误的。而且,我得到的不同产品的所有网址都是相同的。
仅供参考,我在回复中获得的网址如下:
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;
}
答案 0 :(得分:4)
我最近正在处理图片,我相信我对它有很好的了解。
我不认为Josua所说的是“真正的”正确答案。他的答案可以解决你的问题,但我无法忍受看到误导性的信息。
至于他的第一个选择,它是“正确的”。
让我分解代码:
Mage_Catalog_Model_Product_Media_Config
public function getMediaUrl($file)
{
$file = $this->_prepareFileForUrl($file);
if(substr($file, 0, 1) == '/') {
return $this->getBaseMediaUrl() . $file;
}
return $this->getBaseMediaUrl() . '/' . $file;
}
public function getBaseMediaUrl()
{
return Mage::getBaseUrl('media') . 'catalog/product';
}
protected function _prepareFileForUrl($file)
{
return str_replace(DS, '/', $file);
}
如您所见,只需添加media/ + catalog/product + $file
。
$ file取自数据库,值类似于/e/x/example.jpeg
您上传的产品图片存储在这些文件夹中。
现在,针对$product-> getImageUrl()
为您提供错误网址的问题仍然未知。
Josua为第二种选择建议的代码:
$this->helper('catalog/image')
->init($product, $type)
->resize(163, 100);
与$product->getImageUrl()
几乎相同,只是resize
Mage_Catalog_Model_Product
public function getImageUrl()
{
return (string)$this->_getImageHelper()->init($this, 'image')->resize(265);
}
因此,对于他的第二个选项,它将使用旧代码给出相同的结果。 我不知道他为什么建议第二种选择,我认为他从不检查这些功能背后的原因(不是一个好主意,因为它可能导致错误的信息)
当您呼叫$product->getImageUrl()
时,它会尝试从缓存加载您的图像(如果存在),如果不存在,它将从数据库加载图像(对于路径,然后将从媒体中查找正确的图像)文件夹)并创建缓存。如果无法找到图像或发生错误,它将获得占位符图像。
我的建议是检查是否有异常抛出。您需要使用旧代码$product->getImageUrl()
。打开app/code/core/Mage/Catalog/Helper/Image.php
然后转到:
Mage_Catalog_Helper_Image
public function __toString()
{
try {
if( $this->getImageFile() ) {
$this->_getModel()->setBaseFile( $this->getImageFile() );
} else {
$this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
}
if( $this->_getModel()->isCached() ) {
return $this->_getModel()->getUrl();
} else {
if( $this->_scheduleRotate ) {
$this->_getModel()->rotate( $this->getAngle() );
}
if ($this->_scheduleResize) {
$this->_getModel()->resize();
}
if( $this->getWatermark() ) {
$this->_getModel()->setWatermark($this->getWatermark());
}
$url = $this->_getModel()->saveFile()->getUrl();
}
} catch( Exception $e ) {
//put log to show error message
Mage::log($e->getMessage());
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
}
return $url;
}
如果抛出异常,请将Mage::log($e->getMessage());
置于日志中。很可能是因为抛出异常而调用占位符图像。
我只是建议您确保您的图片/其他内容没有任何问题,因为事实上您已通过直接从media/catalog/product/...
获取图片来解决您的问题
Josua代码的另一个更正:
注意$ full_product = Mage :: getModel('catalog / product') - > load($ product_id);
完全没必要,因为在foreach($ collection as $ product)中,产品对象将被加载,因此不需要另外加载产品(也是$ product_id未定义)
更新,只需修复您的代码:
public function items($filters = null, $store = null)
{
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($this->_getStoreId($store))
->addAttributeToSelect(array('name','image'));
//->addAttributeToSelect('name'); add another select, either image / small_image / thumbnail, modify it as you need
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;
}
答案 1 :(得分:4)
请写一下,请尝试这种方式..您将获得顶部的解决方案
您可以使用此代码获取图像,只需通过它就可以获得图像
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;
}
希望它能起作用
如果您有任何疑问,请告诉我,我会帮助您!
答案 2 :(得分:1)
你的代码太棒了!
是的,您可以通过以下方式获取图片网址
'url_path' => Mage::getModel('catalog/product_media_config')
->getMediaUrl($product->getImage());//getSmallImage(), getThumbnail()
或通过调用其他选项:
$type = 'small_image';
'url_path' => $this->helper('catalog/image')
->init($product, $type)
->resize(163, 100);
可以通过' image'进行更改small_image'或者' thumbnail'
默认:强>
基本图片:265x265像素
小图片:135x135像素
缩略图:75x75像素
更简单的选项(详细):
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();
$full_product = Mage::getModel('catalog/product')->load($product_id);
$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' => $full_product->getImageUrl(),
// 'product_path' => $full_product->getProductUrl()
// you can call $full_product->getData();
);
}
return $result;
}