Magento 2:使用 REST API 创建订单

问题描述 投票:0回答:1

我使用带有令牌身份验证的 REST API 将数据从 M1 导出到全新安装的 M2.2.0 安装。

客户和产品一切正常,但我无法理解如何在不创建购物车的情况下创建订单。

我的意思是,我已经有了订单中所需的所有数据,之前应该不需要创建购物车,正如我找到的所有解决方案中所建议的那样(也在官方的 magento guide 中)。

我遵循上述指南中的相同步骤,但尝试发布有关订单的数据时,我不断收到错误:

"Fatal Error: 'Uncaught Error: Call to a member function getMethodInstance() on null in \/var\/www\/html\/app\/code\/Magento\/Payment\/Observer\/SalesOrderBeforeSaveObserver.php:24\nStack trace:\n#0 \/var\/www\/html\/lib\/internal\/Magento\/Framework\/Event\/Invoker\/InvokerDefault.php(72): Magento\\Payment\\Observer\\SalesOrderBeforeSaveObserver->execute(Object(Magento\\Framework\\Event\\Observer))\n#1 \/var\/www\/html\/lib\/internal\/Magento\/Framework\/Event\/Invoker\/InvokerDefault.php(60): Magento\\Framework\\Event\\Invoker\\InvokerDefault->_callObserverMethod(Object(Magento\\Payment\\Observer\\SalesOrderBeforeSaveObserver), Object(Magento\\Framework\\Event\\Observer))\n#2 \/var\/www\/html\/lib\/internal\/Magento\/Framework\/Event\/Manager.php(66): Magento\\Framework\\Event\\Invoker\\InvokerDefault->dispatch(Array, Object(Magento\\Framework\\Event\\Observer))\n#3 \/var\/www\/html\/generated\/code\/Magento\/Framework\/Event\/Manager\/Proxy.php(95): Magento\\Framework\\Event\\Manager->dispatch('sales_order_sav...', Array)\n#4 \/var\/www\/html\/lib\/internal\/Magento\/Framework\/Model\/AbstractModel.php(694):' in '\/var\/www\/html\/app\/code\/Magento\/Payment\/Observer\/SalesOrderBeforeSaveObserver.php' on line 24"

这是我用来进行尝试的示例 JSON:

{
    "entity": {
        "customer_email": "[email protected]",
        "customer_firstname": "mario",
        "customer_lastname": "rossi",
        "base_grand_total": 120,
        "discount_amount": 0,
        "grand_total": 120,
        "increment_id": "200001414155",
        "shipping_amount": 7,
        "status": "processing",
        "store_id": 1,
        "subtotal": 100,
        "tax_amount": 20,
        "items": {
            "description": "prodotto mio",
            "item_id": 3,
            "price": 10,
            "product_id": 3,
            "product_type": "simple",
            "qty_ordered": 2,
            "sku": "002",
            "tax_amount": 2,
            "weight": 1
        }
    },
    "billing_address": {
        "firstname": "giuseppa",
        "lastname": "rossi",
        "company": "mycompany",
        "city": "rome",
        "region": "california",
        "region_id": 12,
        "postcode": "12312",
        "telephone": "1234556677",
        "country_id": "US",
        "street": ["via prima", "via seconda"]
    },
    "payment": {
        "amount_paid": 120,
        "method": "checkmo"
    },
    "status_histories": [{
        "comment": "new order",
        "status": "processing"
    }],
    "extension_attributes": {
        "shipping_assignments": [{
            "shipping": {
                "address": [{
                    "firstname": "franca",
                    "lastname": "vardididi",
                    "company": "mycompany",
                    "city": "rome",
                    "region": "california",
                    "region_id": 12,
                    "postcode": "12312",
                    "telephone": "1234556677",
                    "country_id": "US",
                    "street": ["via prima", "via seconda"]
                }],
                "method": "checkmo",
                "total": {
                    "base_shipping_amount": 7
                }
            },
            "items": [{
                "description": "prodotto mio",
                "item_id": 3,
                "price": 10,
                "product_id": 3,
                "product_type": "simple",
                "qty_ordered": 2,
                "sku": "002",
                "tax_amount": 2,
                "weight": 1
            }]
        }]
    }
}

编辑

在这个问题中使用json我成功创建了订单。

magento2 magento2.2 magento-rest-api
1个回答
3
投票

我在问题编辑中提到的问题中找到了解决方案。我会将其发布在这里,以防链接损坏。

'{
  "entity": {
    "base_currency_code": "EUR",
    "base_discount_amount": 0,
    "base_grand_total": 38,
    "base_shipping_amount": 5,
    "base_shipping_incl_tax": 5,
    "base_shipping_tax_amount": 0,
    "base_shipping_discount_amount": 0,
    "base_subtotal": 33,
    "base_subtotal_incl_tax": 33,
    "base_total_due": 0,
    "base_total_paid" : 38,
    "base_to_global_rate": 1,
    "base_to_order_rate": 1,
    "discount_tax_compensation_amount": 0,
    "base_discount_tax_compensation_amount": 0,
    "shipping_discount_tax_compensation_amount": 0,
    "customer_is_guest": 0,
    "customer_email": "[email protected]",
    "customer_firstname": "Nicola",
    "customer_group_id": 1,
    "customer_id": 4,
    "customer_lastname": "Neri",
    "customer_note_notify": 1,
    "discount_amount": 0,
    "email_sent": 1,
    "global_currency_code": "EUR",
    "grand_total": 38,
    "order_currency_code": "EUR",
    "shipping_amount": 5,
    "shipping_tax_amount": 0,
    "shipping_description": "Flat Rate - Fixed",
    "shipping_discount_amount": 0,
    "shipping_incl_tax": 5,
    "state": "pending",
    "status": "processing",
    "store_currency_code": "EUR",
    "store_to_base_rate": 0,
    "store_to_order_rate": 0,
    "store_id": 1,
    "subtotal": 33,
    "subtotal_incl_tax": 33,
    "total_due": 0,
    "total_paid" : 38,
    "total_item_count": 1,
    "total_qty_ordered": 1,
    "tax_amount": 0,
    "weight": 1,
    "items": [{
      "base_original_price": 33,
      "base_price": 33,
      "base_price_incl_tax": 33,
      "base_row_total": 33,
      "base_row_total_incl_tax": 33,
      "name": "il prodotto",
      "original_price": 33,
      "price": 33,
      "price_incl_tax": 33,
      "product_id": 11,
      "product_type": "simple",
      "qty_ordered": 1,
      "row_total": 33,
      "row_total_incl_tax": 33,
      "sku": "002",
      "store_id": 1,
      "weight": 1
    }],
    "billing_address": {
      "address_type": "billing",
      "city": "Urbino",
      "company": "",
      "country_id": "IT",
      "customer_address_id": 4,
      "email": "[email protected]",
      "firstname": "franco",
      "lastname": "Ciani",
      "postcode": "61029",
      "region": "ca",
      "street": ["via milano 21"],
      "telephone": "071982939"
    },
    "payment": {
      "amount_ordered": 38,
      "amount_paid":38,
      "base_amount_ordered": 38,
      "base_amount_paid" : 38,
      "base_shipping_amount": 5,
      "method": "checkmo",
      "shipping_amount": 5
    },
    "status_histories": [{
      "comment" : "new order",
      "status": "processing"
    }],
    "extension_attributes": {
      "shipping_assignments": [{
        "shipping": {
          "address": {
            "address_type": "shipping",
            "city": "Urbino",
            "company": "mycompany",
            "country_id": "IT",
            "customer_address_id": 4,
            "email": "[email protected]",
            "firstname": "mario",
            "lastname": "Rossi",
            "postcode": "12345",
            "region": "CA",
            "street": ["via roma 823"],
            "telephone": "3469730878"
          },
          "method": "flatrate_flatrate",
          "total": {
            "base_shipping_amount": 5,
            "base_shipping_incl_tax": 5,
            "shipping_amount": 5,
            "shipping_incl_tax": 5
          },
          "extension_attributes": []
        },
        "items": [{
          "base_original_price": 33,
          "base_price": 33,
          "base_price_incl_tax": 33,
          "base_row_total": 33,
          "name": "un belllissimo prodotto",
          "original_price": 33,
          "price": 33,
          "price_incl_tax": 33,
          "product_id": 11,
          "product_type": "simple",
          "qty_ordered": 1,
          "row_total": 33,
          "row_total_incl_tax": 33,
          "sku": "002",
          "store_id": 1,
          "weight": 1
        }],
        "extension_attributes": []
      }],
      "applied_taxes": [],
      "item_applied_taxes": [],
      "converting_from_quote": false
    }
  }
}'
© www.soinside.com 2019 - 2024. All rights reserved.