2016-12-07 48 views
1

如何使用REST API獲取帶分頁的產品列表?我有以下代碼 -Magetno REST API - 獲取帶分頁的產品列表

require_once '../app/Mage.php'; 
Mage::app('default'); 

$productCollection = Mage::getResourceModel('catalog/product'); 
print_r($productCollection); 

輸出

[_data:protected] => Array 
       (
        [entity_type_id] => 4 
        [entity_type_code] => catalog_product 
        [entity_model] => catalog/product 
        [attribute_model] => catalog/resource_eav_attribute 
        [entity_table] => catalog/product 
        [value_table_prefix] => 
        [entity_id_field] => 
        [is_data_sharing] => 1 
        [data_sharing_key] => default 
        [default_attribute_set_id] => 4 
        [increment_model] => 
        [increment_per_store] => 0 
        [increment_pad_length] => 8 
        [increment_pad_char] => 0 
        [additional_attribute_table] => catalog/eav_attribute 
        [entity_attribute_collection] => catalog/product_attribute_collection 
      ) 

任何人都可以請幫助我。

更新按照你的建議,我已經更新代碼爲 -

require_once '../app/Mage.php'; 
Mage::app('default'); 

$pageSize = $_REQUEST['pagesize']; 
$pageNum = $_REQUEST['pagenum']; 

$productCollection = Mage::getModel('catalog/product') 
      ->getCollection() 
      ->setPageSize($pageSize) 
      ->setCurPage($pageNum) 
      ->addAttributeToSelect('*'); 

print_r($productCollection->getData()); 

,輸出是現在 -

[0] => Array 
     (
      [entity_id] => 12 
      [entity_type_id] => 4 
      [attribute_set_id] => 4 
      [type_id] => simple 
      [sku] => 20707 
      [has_options] => 1 
      [required_options] => 1 
      [created_at] => 2016-01-06 21:15:31 
      [updated_at] => 2016-10-03 00:49:21 
     ) 

但還是沒能得到產品名稱,描述,圖片等還有什麼我失蹤的東西嗎?

回答

1

在api請求中發送pagesize和pagenum的get參數。

require_once '../app/Mage.php'; 
Mage::app('default'); 

$pageSize = $_GET['pagesize'] 
$pageNum = $_GET['pagenum'] 

$productCollection = Mage::getModel('catalog/product') 
      ->getCollection() 
      ->setPageSize($pageSize) 
      ->setCurPage($pageNum); 

print_r($productCollection); 
+0

謝謝了拉維的答覆,則打印以下數組 - [0] =>數組 ( [ENTITY_ID] => 12 [entity_type_id] => 4 [attribute_set_id] => 4 [ type_id] =>簡單 [sku] => 20707 [has_options] => 1 [required_options] => 1 [created_at] => 2016-01-06 21:15:31 [updated_at] => 2016- 10-03 00:49:21 )我需要產品相關的字段,如產品名稱,描述,圖片等 –

+0

您可以更改上述代碼如下 –

+0

Mage :: getModel('catalog/product') - > getCollection() - > addAttributeToSelect('sku') - > addAttributeToSelect('name') - > setPageSize($ pageSize) - > setCurPage($ pageNum) ; –