Woocommerce 订单 API 订单项 ID 在更新时更改
Posted
技术标签:
【中文标题】Woocommerce 订单 API 订单项 ID 在更新时更改【英文标题】:Woocommerce Order API Line Item ID changes on update 【发布时间】:2018-08-09 20:16:51 【问题描述】:我正在通过“订单更新”网络挂钩检索 Woocommerce 订单,并将特定数据存储在单独的数据库中。
我们还使用个性化插件,允许客户向产品添加自定义消息。这会创建 2 个单独的行项目,否则它们将是后端中的相同产品变体。
在我的单独数据库中,创建或更新行时,我需要使用 order_id、variation_id 以及 id 条目(从 line_items => id) 检查是否必须创建或更新记录。这可确保 2 个具有单独自定义消息的相同产品变体不会被彼此覆盖。
在创建订单时会发送以下 JSON 响应。
"id":10656,"parent_id":0,"number":"10656","order_key":"wc_order_5a9815fa1add6","created_via":"checkout","version":"3.1.2","status":"pending","currency":"ZAR","date_created":"2018-03-01T17:02:18","date_created_gmt":"2018-03-01T15:02:18","date_modified":"2018-03-01T17:02:18","date_modified_gmt":"2018-03-01T15:02:18","discount_total":"0.00","discount_tax":"0.00","shipping_total":"0.00","shipping_tax":"0.00","cart_tax":"60.79","total":"495.00","total_tax":"60.79","prices_include_tax":true,"customer_id":5148,"customer_ip_address":"197.89.123.74","customer_user_agent":"mozilla\/5.0 (linux; android 6.0.1; samsung sm-g900h build\/mmb29k) applewebkit\/537.36 (khtml, like gecko) samsungbrowser\/6.4 chrome\/56.0.2924.87 mobile safari\/537.36","customer_note":"","billing":"first_name":"Tasneem","last_name":"Modack","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"ZA","email":"modackt@webmail.co.za","phone":"0828736647","shipping":"first_name":"Tasneem","last_name":"Modack","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"ZA","payment_method":"payfast","payment_method_title":"Pay With Your Card or Instant EFT.","transaction_id":"","date_paid":null,"date_paid_gmt":null,"date_completed":null,"date_completed_gmt":null,"cart_hash":"666062e6d9100aa43860fb71caef1466","meta_data":["id":506232,"key":"_store_location","value":"loop-street-cbd","id":506233,"key":"_collection_date","value":"3 March, 2018","id":506234,"key":"_collection_time","value":"10:00 AM","id":506235,"key":"mailchimp_woocommerce_is_subscribed","value":"1","id":506259,"key":"_billing_vat_number","value":""],"line_items":["id":28607,"name":"Rainbow - Yes (add R50.00)","product_id":415,"variation_id":2590,"quantity":1,"tax_class":"","subtotal":"434.21","subtotal_tax":"60.79","total":"434.21","total_tax":"60.79","taxes":["id":1,"total":"60.79","subtotal":"60.79"],"meta_data":["id":194971,"key":"transform-into-a-pinata","value":"Yes (add R50.00)"],"sku":"N005-P","price":434.21,"_product_group":"rainbow-cakes"],"tax_lines":["id":28609,"rate_code":"ZA-VAT-1","rate_id":1,"label":"VAT","compound":false,"tax_total":"60.79","shipping_tax_total":"0.00","meta_data":[]],"shipping_lines":["id":28608,"method_title":"Local pickup","method_id":"local_pickup:10","total":"0.00","total_tax":"0.00","taxes":[],"meta_data":["id":194976,"key":"Items","value":"Rainbow - Yes (add R50.00) × 1"]],"fee_lines":[],"coupon_lines":[],"refunds":[]
line_item => id,订单创建时为28607
在订单更新时,会发送以下 JSON 响应。
"id":10656,"parent_id":0,"number":"10656","order_key":"wc_order_5a9815fa1add6","created_via":"checkout","version":"3.1.2","status":"pending","currency":"ZAR","date_created":"2018-03-01T17:02:18","date_created_gmt":"2018-03-01T15:02:18","date_modified":"2018-03-01T17:02:18","date_modified_gmt":"2018-03-01T15:02:18","discount_total":"0.00","discount_tax":"0.00","shipping_total":"0.00","shipping_tax":"0.00","cart_tax":"60.79","total":"495.00","total_tax":"60.79","prices_include_tax":true,"customer_id":5148,"customer_ip_address":"197.89.123.74","customer_user_agent":"mozilla\/5.0 (linux; android 6.0.1; samsung sm-g900h build\/mmb29k) applewebkit\/537.36 (khtml, like gecko) samsungbrowser\/6.4 chrome\/56.0.2924.87 mobile safari\/537.36","customer_note":"","billing":"first_name":"Tasneem","last_name":"Modack","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"ZA","email":"modackt@webmail.co.za","phone":"0828736647","shipping":"first_name":"Tasneem","last_name":"Modack","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"ZA","payment_method":"payfast","payment_method_title":"Pay With Your Card or Instant EFT.","transaction_id":"","date_paid":null,"date_paid_gmt":null,"date_completed":null,"date_completed_gmt":null,"cart_hash":"666062e6d9100aa43860fb71caef1466","meta_data":["id":506232,"key":"_store_location","value":"loop-street-cbd","id":506233,"key":"_collection_date","value":"3 March, 2018","id":506234,"key":"_collection_time","value":"1:00 PM","id":506235,"key":"mailchimp_woocommerce_is_subscribed","value":"1","id":506256,"key":"_billing_vat_number","value":""],"line_items":["id":28604,"name":"Rainbow - Yes (add R50.00)","product_id":415,"variation_id":2590,"quantity":1,"tax_class":"","subtotal":"434.21","subtotal_tax":"60.79","total":"434.21","total_tax":"60.79","taxes":["id":1,"total":"60.79","subtotal":"60.79"],"meta_data":["id":194951,"key":"transform-into-a-pinata","value":"Yes (add R50.00)"],"sku":"N005-P","price":434.21,"_product_group":"rainbow-cakes"],"tax_lines":["id":28606,"rate_code":"ZA-VAT-1","rate_id":1,"label":"VAT","compound":false,"tax_total":"60.79","shipping_tax_total":"0.00","meta_data":[]],"shipping_lines":["id":28605,"method_title":"Local pickup","method_id":"local_pickup:10","total":"0.00","total_tax":"0.00","taxes":[],"meta_data":["id":194956,"key":"Items","value":"Rainbow - Yes (add R50.00) × 1"]],"fee_lines":[],"coupon_lines":[],"refunds":[]
line_item => id,订单更新时为28604
这将在我的系统上的订单中创建一个额外的订单项,而实际上它是同一个订单项。
发生这种情况是否有原因,是否有可能的解决方法?
注意 删除行并重新添加不是一个选项,因为我系统上的数据库包含与 Woocommerce 无关的单独列。
更新 根据 JSON 响应,特别是 "date_created",“订单创建”网络钩子似乎触发了两次,但在“订单更新”时它只触发了一次。
【问题讨论】:
我想了解为什么要发送订单更新?您不会从初始订单创建事件中获取所有信息吗? @AndrewSchultz 我的 Laravel 应用程序经常更新订单状态,这反过来会触发“订单已更新”webhook。 你在使用 laravel 和 wordpress 吗?你能解释一下什么是“line_item”吗?此外,查看产生这种行为的代码会很有帮助.. @Yuri 你有使用 Woocommerce 的经验吗? "我们还使用了个性化插件,允许客户向产品添加自定义消息。" — 没有,您是否也遇到了同样的问题插件,还是禁用/停用插件?您还可以通过转到 WooCommerce > Status > Logs 查看发送到 webhook 的交付 URL 的数据。 【参考方案1】:这绝对是您提到的个性化插件的问题,因为 woocommerce 本身不会在更新时创建新的订单项,从下面的代码可以看出 https://github.com/woocommerce/woocommerce/blob/master/includes/api/legacy/v2/class-wc-api-orders.php#L867
protected function set_line_item( $order, $item, $action )
$creating = ( 'create' === $action );
//
if ( $creating )
$line_item = new WC_Order_Item_Product();
else
$line_item = new WC_Order_Item_Product( $item['id'] );
我强烈建议您禁用自定义插件并尝试。
【讨论】:
我查看了 webhook 日志,似乎创建 webhook 的订单有时会触发两次,即使在不包含个性化产品的订单上也是如此。我在 Laravel 端的控制器中有一个方法,负责创建新订单。如果订单已经存在,我基本上会阻止订单创建。我正忙着测试看看这是否有效。 好的,发布结果,因为这个问题非常独特,我会对结果感兴趣。以上是关于Woocommerce 订单 API 订单项 ID 在更新时更改的主要内容,如果未能解决你的问题,请参考以下文章
在 Woocommerce 3+ 中使用订单项以编程方式创建订单
如何从 Woocommerce 中的订单项中获取产品 sku [重复]