【问题标题】:Magetno REST API - get product list with paginationMagento REST API - 使用分页获取产品列表
【发布时间】:2016-12-07 06:37:38
【问题描述】:

如何使用 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
        )

但仍然无法获得产品名称、描述、图像等。我还缺少其他什么吗?

【问题讨论】:

    标签: php rest magento-1.9


    【解决方案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);
    

    【讨论】:

    • 非常感谢 Ravi 的回复,它正在打印以下数组 - [0] => Array ( [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 ) 我需要产品相关领域,如产品名称、描述、图片等。
    • Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('sku') ->addAttributeToSelect('name')->setPageSize($pageSize) ->setCurPage ($pageNum);
    • 您可以在 getcollection() 之后使用 ->addAttributeToSelect('feildname') 添加任何需要的文件
    • 我已经更新了类似的代码 - $productCollection = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('name') ->setPageSize($pageSize) -> setCurPage($pageNum); print_r($productCollection->getData());但输出仍然和上面一样,输出中没有添加名称字段。
    • 这取决于您的目录设置尝试使用 addFieldToSelect 而不是 addAttributeToSelect 。 addFieldToSelect 用于平面模型,addAttributeToSelect 用于 EAV 模型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 2017-10-17
    • 2014-08-20
    相关资源
    最近更新 更多