Reputation: 3197
I’m retrieving Woocommerce orders via the "order updated" webhook and storing specific data in a separate database.
We are also using a personalisations plugin which allows customers to add custom messages to products. This creates 2 separate line items, which would otherwise be the same product variation in the backend.
On my separate database, when creating or updating a row, I need to use order_id, variation_id as well as the id entry (from line_items => id) to check whether a record must be created or updated. This ensure that 2 identical product variations with separate custom messages aren't overwritten by each other.
On order create the following JSON response is sent.
{"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":"[email protected]","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":[]}
The line_item => id, when the order is created is 28607
On order update the following JSON response is sent.
{"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":"[email protected]","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":[]}
The line_item => id, when the order is updated is 28604
This will create an additional line item in the order on my system when in fact it is the same line item.
Is there a reason why this happens, and is there a possible workaround?
NOTE Dropping the rows and re-adding is not an options as the database on my system contains separate columns that are not related to Woocommerce.
UPDATE According to the JSON response, specifically the "date_created", it appears that the "order created" webhook fires twice but on "order updated" it only fires once.
Upvotes: 5
Views: 2103
Reputation: 1528
This is definitely an issue with the personalization plugins you mentioned, because woocommerce on its own does not create a new line item on update as evident from the code below 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'] );
}
I would strongly recommend you to disable the customisation plugin and try.
Upvotes: 3