我已经重写了Magento Core(M1.9(的Api模型,并想测试它是如何工作的。它也返回可能记录,我想像在 Sql 中设置条件"按 Desc |Asc"和"LIMIT"在我的测试中。但我不知道我应该在哪里放置提到的条件。这是我的测试代码:
$username = 'testapi';
$apikey= 'password';
$client = new Zend_XMLRPC_Client('https://www.magentohost.com/index.php/api/xmlrpc');
$session = $client->call('login', array($username, $apikey));
$filters = array(
array(
'category_id' => 163,
'internal_rating' => 6
//array('product_id'=>'Order by ASC')
));
try {
$message = $client->call('call', array($session, 'catalog_product.list', $filters));
var_dump($message);
} catch (Exception $fault) {
echo $fault->getMessage();
}
如有任何建议,我将不胜感激
请尝试测试以下代码:
$filters = array(
array(
'category_id' => 163,
'internal_rating' => 6,
'order' => 'product_id',
'dir' => 'asc',
'limit' => 100
));
我还没有自己测试过。我从链接中获取了过滤器参数的格式 http://devdocs.magento.com/guides/m1x/api/rest/get_filters.html也许它也适用于您的情况。
你必须重写这个类并覆盖项目函数类 = Mage_Catalog_Model_Product_Api函数 = 项目
public function items($filters = null, $store = null, $extra = [])
{
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($this->_getStoreId($store))
->addAttributeToSelect('name');
if(isset($extra["cur_page"])) {
$collection->setCurPage($extra["cur_page"]);
}
if(isset($extra["page_size"])) {
$collection->setPageSize($extra["page_size"]);
}
if(isset($extra["order"])) {
$collection->setOrder($extra["order"]["field"], $extra["order"]["type"]);
}
然后你可以通过调用它
$filters = [];
$extra = ["cur_page"=>1,"page_size"=>"3","order"=>["field"=>"name", "type"=>"desc"]];
$result= $proxy->call($sessionId, 'catalog_product.list',[$filters,null,$extra]);
我试图通过GET方法将参数限制,目录,顺序传输到url请求中,如下所示:
$client = new
Zend_XMLRPC_Client('https://www.magentohost.com/index.php/api/xmlrpc?
limit=1&dir=desc&order=created_at');
$session = $client->call('login', array($username, $apikey));
$filters = array(
array(
'category_id' => 174
));
并在重写的类Mage_Catalog_Model_Product_Api方法项中
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($this->_getStoreId($store))
->addAttributeToSelect('name')
->addAttributeToSelect('content_downloaded')
;
if (isset($_GET['order']) && isset($_GET['dir'])){
$order = htmlspecialchars(strip_tags($_GET['order']));
$dir = (strtoupper($_GET['dir']) == 'DESC' ) ? 'DESC' : 'ASC';
$collection->setOrder($order, $dir);
}
if (isset($_GET['limit'])){
$limit = intval($_GET['limit']);
$collection->getSelect()->limit($limit);
}
它进行了谷歌测试。它与HAKIM建议的解决方案类似。