php - How to create order in Shopware 6 via API
Get the solution ↓↓↓I am migrating a shop form an old platform to Shopware 6. I need to move over orders and I am having problems doing so. I am using the API that is being called by a script that has access to the old platform database. I have already pushed nearly all other entities (customers, products, media, categories, links between those) using the API and it works fine. In the documentation it seems one should create a cart first and then via an action create it into an order, then add order address, payment and transition the states via actions to what is required.
There is no exact guide for it so I ended up trying to push a POST request with order data toorder
endpoint like that:
array:12 [
"orderNumber" => "100000546"
"price" => array:6 [
"netPrice" => 47.3519
"totalPrice" => 50.32
"positionPrice" => 50.32
"calculatedTaxes" => array:1 [
0 => array:3 [
"tax" => 3.314633
"taxRate" => 7
"price" => 50.32
]
]
"taxRules" => array:1 [
0 => array:2 [
"taxRate" => 7
"percentage" => 100
]
]
"taxStatus" => "gross"
]
"shippingCosts" => array:5 [
"unitPrice" => 4.95
"totalPrice" => 4.95
"quantity" => 1
"calculatedTaxes" => array:1 [
0 => array:3 [
"tax" => 0.3465
"taxRate" => 7
"price" => 4.95
]
]
"taxRules" => array:1 [
0 => array:2 [
"taxRate" => 7
"percentage" => 100
]
]
]
"billingAddressId" => "9f27c3c98dfe4e47ad2a9493da5807bc"
"currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca"
"languageId" => "2fbb5fe2e29a4d70aa5854ce7ce3e20b"
"salesChannelId" => "d1d7fc6faedb4600afd0cfe68f627f6d"
"orderDateTime" => "2014-11-03 08:27:43"
"currencyFactor" => 1
"stateId" => "8089405ce1e046f1819823d847125d25"
"lineItems" => array:1 [
0 => array:6 [
"identifier" => "5158757ba32b489faf70fcd7c4578604"
"productId" => "5158757ba32b489faf70fcd7c4578604"
"quantity" => 10
"label" => "Some product"
"position" => 1
"price" => array:5 [
"unitPrice" => 3.64
"totalPrice" => 36.3551
"quantity" => 10
"calculatedTaxes" => array:1 [
0 => array:3 [
"tax" => 0.2548
"taxRate" => 7
"price" => 36.3551
]
]
"taxRules" => array:1 [
0 => array:2 [
"taxRate" => 7
"percentage" => 100
]
]
]
]
]
"orderCustomer" => array:6 [
"customerId" => "cafedb542f2f488e99375192092c9032"
"salutationId" => "08f9cf197c0643ac98590948cf5dd9f8"
"firstName" => "xx
"lastName" => "xx
"email" => "xx
"customerNumber" => "10020"
]
]
This creates an order in the database but I cannot see it in the orders listing in admin (admin#/sw/order/index
) - in fact it breaks the list when I try to change list page. If I open another order and then replace the order id with the one I created I can open it but it does not have the details box. I can see only Items, Status and Documents. I suspected that it is due to the fact that there is no payment and shipment information linked so I added in alsoorder_address
entity via the API and then tried to createorder_delivery
so I have state for the shipment. Unfortunately I get this when I try to create such entity with a state:
0 => {#8643
+"code": "FRAMEWORK__WRITE_CONSTRAINT_VIOLATION"
+"status": "400"
+"detail": "This field is write-protected. (Got: "crud" scope and "system" is required)"
+"template": "This field is write-protected. (Got: "%s" scope and "%s" is required)"
+"meta": {#8655
+"parameters": array:2 [
0 => "crud"
1 => "system"
]
}
+"source": {#8664
+"pointer": "/0/stateIdstateId"
}
}
And I am stuck. I need help. Can I add some switch to the request to force write (create a token withsystem
scope?) into thestateId
field? Or I should perform action? But then again I cannot perform an action without havingorder_delivery
entity already set with valueopen
...
Am I doing it right anyways? By trying to create entities directly (these orders serve just as an archive, they are all completed already)?
Answer
Solution:
Maybe you should use migration plugins from Shopware for sw5 & sw6. They are described there Migration process. And you can find those plugins on GitHub https://github.com/shopware/SwagMigrationAssistant & https://github.com/shopware/SwagMigrationConnector
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.