我正在通过“订单更新”webhook检索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”,似乎“订单创建”webhook会触发两次,但在“订单更新”时,它只会触发一次。
答案 0 :(得分:3)
这绝对是您提到的个性化插件的一个问题,因为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'] );
}
我强烈建议您禁用自定义插件并尝试。