magento api分页获取最近更新订单的方法

2,421 人次阅读
没有评论

共计 4969 个字符,预计需要花费 13 分钟才能阅读完成。

首先创建文件test.php,代码如下(其中$page为当前页码,$size为每页记录数):

[php]

$url = ‘http://127.0.0.1/magento/api/soap/?wsdl’;
 $appKey = ‘API账号’;
 $appSecret = ‘API密码’;
 $client = new SoapClient($url, array("trace" => true, "connection_timeout" => 600));
 $sessionKey = $client->login($appKey,$appSecret);
 $filterData = array(array(‘page’=>$page,’page_size’=>$size,’updated_at’=>array(‘from’=>$start, ‘to’=>$end)));
 $Res = $client->call($sessionKey,’sales_order.list’,$filterData);
 $total = $Res[‘total’];
 for ($i = 0; !empty($Res[$i]); $i++) {

……
 }

[/php]

然后还要修改magento的API文件app/code/core/Mage/Sales/Model/Order/Api.php为如下所示:

[php]

/**
     * Retrieve list of orders by filters
     *
     * @param array $filters
     * @return array
     */
    public function items($filters = null)
    {
        //TODO: add full name logic
        $billingAliasName = ‘billing_o_a’;
        $shippingAliasName = ‘shipping_o_a’;

        $collection = Mage::getModel("sales/order")->getCollection()
            ->addAttributeToSelect(‘*’)
            ->addAddressFields()
            ->addExpressionFieldToSelect(
                ‘billing_firstname’, "{{billing_firstname}}", array(‘billing_firstname’=>"$billingAliasName.firstname")
            )
            ->addExpressionFieldToSelect(
                ‘billing_lastname’, "{{billing_lastname}}", array(‘billing_lastname’=>"$billingAliasName.lastname")
            )
            ->addExpressionFieldToSelect(
                ‘shipping_firstname’, "{{shipping_firstname}}", array(‘shipping_firstname’=>"$shippingAliasName.firstname")
            )
            ->addExpressionFieldToSelect(
                ‘shipping_lastname’, "{{shipping_lastname}}", array(‘shipping_lastname’=>"$shippingAliasName.lastname")
            )
            ->addExpressionFieldToSelect(
                    ‘billing_name’,
                    "CONCAT({{billing_firstname}}, ‘ ‘, {{billing_lastname}})",
                    array(‘billing_firstname’=>"$billingAliasName.firstname", ‘billing_lastname’=>"$billingAliasName.lastname")
            )
            ->addExpressionFieldToSelect(
                    ‘shipping_name’,
                    ‘CONCAT({{shipping_firstname}}, " ", {{shipping_lastname}})’,
                    array(‘shipping_firstname’=>"$shippingAliasName.firstname", ‘shipping_lastname’=>"$shippingAliasName.lastname")
            );

        if (is_array($filters)) {
            try {
                foreach ($filters as $field => $value) {
                    if ($field != ‘page’ && $field != ‘page_size’) {
                    if (isset($this->_attributesMap[‘order’][$field])) {
                        $field = $this->_attributesMap[‘order’][$field];
                    }

$collection->addFieldToFilter($field, $value);
                    }
                }
            } catch (Mage_Core_Exception $e) {
                $this->_fault(‘filters_invalid’, $e->getMessage());
            }
        }

$result = array();

  $result[‘total’] = $collection->count();

        $collection = Mage::getModel("sales/order")->getCollection()
            ->addAttributeToSelect(‘*’)
            ->addAddressFields()
            ->addExpressionFieldToSelect(
                ‘billing_firstname’, "{{billing_firstname}}", array(‘billing_firstname’=>"$billingAliasName.firstname")
            )
            ->addExpressionFieldToSelect(
                ‘billing_lastname’, "{{billing_lastname}}", array(‘billing_lastname’=>"$billingAliasName.lastname")
            )
            ->addExpressionFieldToSelect(
                ‘shipping_firstname’, "{{shipping_firstname}}", array(‘shipping_firstname’=>"$shippingAliasName.firstname")
            )
            ->addExpressionFieldToSelect(
                ‘shipping_lastname’, "{{shipping_lastname}}", array(‘shipping_lastname’=>"$shippingAliasName.lastname")
            )
            ->addExpressionFieldToSelect(
                    ‘billing_name’,
                    "CONCAT({{billing_firstname}}, ‘ ‘, {{billing_lastname}})",
                    array(‘billing_firstname’=>"$billingAliasName.firstname", ‘billing_lastname’=>"$billingAliasName.lastname")
            )
            ->addExpressionFieldToSelect(
                    ‘shipping_name’,
                    ‘CONCAT({{shipping_firstname}}, " ", {{shipping_lastname}})’,
                    array(‘shipping_firstname’=>"$shippingAliasName.firstname", ‘shipping_lastname’=>"$shippingAliasName.lastname")
            );

        if (is_array($filters)) {
            try {
                foreach ($filters as $field => $value) {
                    if ($field != ‘page’ && $field != ‘page_size’) {
                    if (isset($this->_attributesMap[‘order’][$field])) {
                        $field = $this->_attributesMap[‘order’][$field];
                    }

$collection->addFieldToFilter($field, $value);
                    }
                }
            } catch (Mage_Core_Exception $e) {
                $this->_fault(‘filters_invalid’, $e->getMessage());
            }
        }
        if (is_array($filters) && $filters[‘page’] && $filters[‘page_size’]) {
   $collection->setPage($filters[‘page’]);
   $collection->setPageSize($filters[‘page_size’]);
        }

foreach ($collection as $order) {
            $result[] = $this->_getAttributes($order, ‘order’);
        }

return $result;
    }

[/php]

正文完
 0