在 Magento 中,扩展 rest/products api 以检索返回列表中产品的标签和属性的性能如何?
In Magento, what's the performance of extending the rest/products api, to retrieve the tags and attributes of the products in the returned list?
我能想到的唯一方法是检索产品,然后在脚本中调用其他 APIs 来检索我需要的信息,最后返回响应。
我对 Magento 还是有些陌生,这似乎对性能影响很大。上述解决方案在性能上是否有效,或者是否有更好的方法从 rest/products api?
中检索标签等
基本上,我正在寻找的是这些方面的东西:
当前APIreturns:
{
337: {
entity_id: "337"
type_id: "simple"
sku: "ace000"
color: "15"
gender: "93"
material: "130"
jewelry_type: null
description: "Gunmetal frame with crystal gradient polycarbonate lenses in grey. "
meta_keyword: null
short_description: "A timeless accessory staple, the unmistakable teardrop lenses of our Aviator sunglasses appeal to everyone from suits to rock stars to citizens of the world."
name: "Aviator Sunglasses"
meta_title: null
meta_description: null
regular_price_with_tax: 319.34
regular_price_without_tax: 295
final_price_with_tax: 319.34
final_price_without_tax: 295
is_saleable: true
image_url: "http://magentogs.cloudapp.net/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/a/c/ace000a_1.jpg"
}
}
我要添加以下内容(作为示例),**包含的内容显示我要添加的内容。
{
337: {
entity_id: "337"
type_id: "simple"
sku: "ace000"
color: "15"
gender: "93"
material: "130"
jewelry_type: null
description: "Gunmetal frame with crystal gradient polycarbonate lenses in grey. "
meta_keyword: null
short_description: "A timeless accessory staple, the unmistakable teardrop lenses of our Aviator sunglasses appeal to everyone from suits to rock stars to citizens of the world."
name: "Aviator Sunglasses"
meta_title: null
meta_description: null
regular_price_with_tax: 319.34
regular_price_without_tax: 295
final_price_with_tax: 319.34
final_price_without_tax: 295
is_saleable: true
image_url: "http://magentogs.cloudapp.net/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/a/c/ace000a_1.jpg"
**tags: [tag1,tag2,tag3]**
**categories: [category1,category2,category3]**
}
}
您应该扩展现有产品 REST api 模型并覆盖方法 _prepareProductForResponse
以包含标签和其他数据
您的模型定义:
Namespace_yourmodule_Model_Catalog_Api2_Product_Rest extends Mage_Catalog_Model_Api2_Product_Rest {
protected function _prepareProductForResponse(Mage_Catalog_Model_Product $product)
{
// keep the existing code as it is
// now add new code to add tags
$productData['tags'] = Mage::getModel('tag/tag')->getResourceCollection()
->addPopularity()
->addProductFilter($product->getId());
// in the end
$product->addData($productData);
}
我能想到的唯一方法是检索产品,然后在脚本中调用其他 APIs 来检索我需要的信息,最后返回响应。
我对 Magento 还是有些陌生,这似乎对性能影响很大。上述解决方案在性能上是否有效,或者是否有更好的方法从 rest/products api?
中检索标签等基本上,我正在寻找的是这些方面的东西:
当前APIreturns:
{
337: {
entity_id: "337"
type_id: "simple"
sku: "ace000"
color: "15"
gender: "93"
material: "130"
jewelry_type: null
description: "Gunmetal frame with crystal gradient polycarbonate lenses in grey. "
meta_keyword: null
short_description: "A timeless accessory staple, the unmistakable teardrop lenses of our Aviator sunglasses appeal to everyone from suits to rock stars to citizens of the world."
name: "Aviator Sunglasses"
meta_title: null
meta_description: null
regular_price_with_tax: 319.34
regular_price_without_tax: 295
final_price_with_tax: 319.34
final_price_without_tax: 295
is_saleable: true
image_url: "http://magentogs.cloudapp.net/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/a/c/ace000a_1.jpg"
}
}
我要添加以下内容(作为示例),**包含的内容显示我要添加的内容。
{
337: {
entity_id: "337"
type_id: "simple"
sku: "ace000"
color: "15"
gender: "93"
material: "130"
jewelry_type: null
description: "Gunmetal frame with crystal gradient polycarbonate lenses in grey. "
meta_keyword: null
short_description: "A timeless accessory staple, the unmistakable teardrop lenses of our Aviator sunglasses appeal to everyone from suits to rock stars to citizens of the world."
name: "Aviator Sunglasses"
meta_title: null
meta_description: null
regular_price_with_tax: 319.34
regular_price_without_tax: 295
final_price_with_tax: 319.34
final_price_without_tax: 295
is_saleable: true
image_url: "http://magentogs.cloudapp.net/magento/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/a/c/ace000a_1.jpg"
**tags: [tag1,tag2,tag3]**
**categories: [category1,category2,category3]**
}
}
您应该扩展现有产品 REST api 模型并覆盖方法 _prepareProductForResponse
以包含标签和其他数据
您的模型定义:
Namespace_yourmodule_Model_Catalog_Api2_Product_Rest extends Mage_Catalog_Model_Api2_Product_Rest {
protected function _prepareProductForResponse(Mage_Catalog_Model_Product $product)
{
// keep the existing code as it is
// now add new code to add tags
$productData['tags'] = Mage::getModel('tag/tag')->getResourceCollection()
->addPopularity()
->addProductFilter($product->getId());
// in the end
$product->addData($productData);
}