Magento 2 API没有返回结帐的总数和商品。

huangapple go评论44阅读模式
英文:

Magento 2 API not returning totals and items for checkout

问题

这是您提到的使用Magento 2的API创建移动应用的一部分,特别是与shipping-information API相关的问题。问题似乎出现在API未返回订单项和订单总额的情况下。您已经尝试了一些步骤,但未获得所需的值。您还尝试了调用collect-totals API和totals API,但没有收到任何值。

建议您考虑以下步骤来解决问题:

  1. 确保Magento 2网站的配置和插件是最新的,以减少潜在的错误源。

  2. 检查Magento 2的日志文件,特别是系统日志和API请求日志,以查看是否有任何错误消息或有关API请求的详细信息。

  3. 确保您的Magento 2网站与API的授权和权限设置是正确的,确保API用户有足够的权限来访问所需的信息。

  4. 考虑在Magento 2网站上手动执行相同的操作,以查看是否可以获得所需的信息。这将帮助确定问题是来自API还是Magento 2本身。

  5. 联系Magento的支持团队或社区,他们可能能够提供更详细的指导和解决方案,特别是如果问题与Magento的特定版本或插件相关。

希望这些建议对您有所帮助,可以帮助您找到问题的根本原因并解决它。

英文:

I have a fully working Magento 2 website, now I want to use the API to create a mobile application so I am using rest API to checkout and place and order.

The problem in the shipping-information API which does not return order items and order totals so I can show them in the mobile app checkout page.
The flow I am using is like this:

  1. get customer token using this API POST {{base_url}}/integration/customer/token
  2. create quote for customer using this api POST {{base_url}}/carts/mine which return quote_id
  3. add items to cart using this API POST {{base_url}}/carts/mine/items and the request body is
    {
    "cartItem":{
        "qty":1,
        "quoteId":"87846",
        "sku":"8681124614421"
        }
    }

and the response is

    {
    "item_id": 48508,
    "sku": "8681124614421",
    "qty": 2,
    "name": "MUSK Eau De Parfum 100 ml",
    "price": 644.07,
    "product_type": "simple",
    "quote_id": "87846"
    }
  1. calls estimate shipping method as follow POST {{base_url}}/carts/mine/estimate-shipping-methods
    request body:
    {
    "address": {
        "street": [
            "dsfsdf"
        ],
        "city": "aaasdd",
        "region": "",
        "country_id": "TR",
        "postcode": "12345",
        "firstname": "aaa",
        "lastname": "bbb",
        "telephone": "3213510",
        "custom_attributes": [
            {
                "attribute_code": "student",
                "value": "",
                "label": " "
            }
        ],
        "save_in_address_book": 1
    }
    }

and response is:

    [
    {
        "carrier_code": "dhl",
        "method_code": null,
        "carrier_title": "DHL",
        "amount": 0,
        "base_amount": null,
        "available": false,
        "error_message": "This shipping method is currently unavailable. If you would like to ship using this shipping method, please contact us.",
        "price_excl_tax": 0,
        "price_incl_tax": 0
    },
    {
        "carrier_code": "flatrate",
        "method_code": "flatrate",
        "carrier_title": "Flat Rate",
        "method_title": "Fixed",
        "amount": 10,
        "base_amount": 10,
        "available": true,
        "error_message": "",
        "price_excl_tax": 8.47,
        "price_incl_tax": 10
    }
    ]
  1. finally where the problem is, I call POST {{base_url}}/carts/mine/shipping-information
    with this request body:
    {
    "addressInformation": {
        "shipping_address": {
            "countryId": "TR",
            "region": "",
            "street": [
                "dsfsdf"
            ],
            "telephone": "3213510",
            "postcode": "12345",
            "city": "aaasdd",
            "firstname": "owais",
            "lastname": "hamouda",
            "saveInAddressBook": 1,
            "customAttributes": [
                {
                    "attribute_code": "student",
                    "value": "",
                    "label": " "
                }
            ]
        },
        "billing_address": {
            "customerAddressId": "206",
            "countryId": "TR",
            "regionId": "43",
            "regionCode": null,
            "region": "Turkey",
            "customerId": "492",
            "street": [
                "160 1st St."
            ],
            "company": "Sawaftech",
            "telephone": "516-555-1111",
            "fax": null,
            "postcode": "11501",
            "city": "Mineola",
            "firstname": "Jane",
            "lastname": "Doe",
            "middlename": null,
            "prefix": null,
            "suffix": null,
            "vatId": null,
            "customAttributes": []
        },
        "shipping_method_code": "flatrate",
        "shipping_carrier_code": "flatrate",
        "extension_attributes": {}
    }
    }

and the response is:

    {
    "payment_methods": [
        {
            "code": "iyzipay",
            "title": "Kredi/Banka Kartı ile Ödeme"
        },
        {
            "code": "banktransfer",
            "title": "Banka Havalesi Ödeme"
        },
        {
            "code": "cashondelivery",
            "title": "Cash On Delivery"
        },
        {
            "code": "stripe_payments",
            "title": "Stripe"
        }
    ],
    "totals": {
        "grand_total": 0,
        "weee_tax_applied_amount": null,
        "items": [],
        "total_segments": [
            {
                "code": "subtotal",
                "title": "Subtotal",
                "value": 0
            },
            {
                "code": "shipping",
                "title": "Shipping & Handling",
                "value": null
            },
            {
                "code": "tax",
                "title": "Tax",
                "value": 0,
                "extension_attributes": {
                    "tax_grandtotal_details": []
                }
            },
            {
                "code": "grand_total",
                "title": "Grand Total",
                "value": null,
                "area": "footer"
            }
        ]
    }
   }

Here as you see the totals and sub totals, grand totals, items not returned from the API.
I also tried calling the collect-totals API, and totals API and didn't get any value.
I am stuck on this issue for two weeks now, please help and advice what could be the problem, or how can I trace it or debug it if it is from an installed plugin for example.

Please note all the API calls I called it with header Authorization Bearer <customer token>
And note that the flow from the website is working correctly.

答案1

得分: 0

我一直在使用相同的API。我还没有遇到这些问题。但我已经手动更新了总价。我建议尝试以下步骤:

  1. 检查是否有其他模块覆盖了总额数据。例如,检查是否调用了以下其中一个方法:

    $quote->setTriggerRecollect(1);
    $quote->collectTotals()->save();
    $quote->save();
    
  2. 由于您在添加到购物车时获取价格,可以添加插件或创建自己的API,以查看是否可以在每个POST/PUT/DELETE请求之后手动设置总价。我已创建了一个GET请求,根据报价ID显示自定义购物车数据。也可以复制类似的方法。

英文:

I've been using the same APIs too. I haven't encountered these issues. But I have updated the total price manually. I'd recommend trying one of the following steps:

  1. Check if any other module is overriding the totals data. For example, check if one of these methods are called

     $quote->setTriggerRecollect(1);
    $quote->collectTotals()->save();
    $quote->save();
    
  2. Since you get the price when you add to cart, add a plugin or create your own API to see if you can manually set the total after each POST/PUT/DELETE request. I have created a GET request that displays custom cart data based on quote ID. Something like that could be replicated too.

答案2

得分: 0

问题是对以前开发人员的核心模块进行了修改,我不得不将源代码与 Magento 的新安装进行比较,以解决这个问题。

英文:

The problem was a modification to the core modules from previous developer, I had to compare source code with fresh install of Magento to address the issue.

huangapple
  • 本文由 发表于 2023年4月10日 21:20:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定