Fix #3812: Remove woocommerce_new_order hook to prevent empty Purchase event data#3861
Conversation
… Purchase event data - Removed woocommerce_new_order hook (priority 10) which fires before order items are added - This hook was causing empty content_ids, content_name, and contents in CAPI Purchase events - woocommerce_checkout_update_order_meta (priority 30) now handles server-side tracking - Updated unit tests to use correct hook and add products to orders - Added proper cleanup for test resources (products and orders)
|
Hi @faisalahammad! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Summary
This PR fixes a bug where Purchase CAPI (Conversions API) events were sent with empty
content_ids,content_name, andcontentsarrays. The root cause was thewoocommerce_new_orderhook firing before order items were added to the order.Fixes #3812
Problem Description
The Purchase event was being triggered by the
woocommerce_new_orderhook. This hook fires immediately when an order is created, but before WooCommerce adds the order items to that order. When ourinject_purchase_event()method tried to get product data using$order->get_items(), it returned an empty array.This caused Facebook to receive Purchase events with:
content_ids: []instead of product IDscontent_name: []instead of product namescontents: []instead of product detailsImpact
Root Cause Analysis
WooCommerce Hook Timeline
Why Empty Data Occurred
Solution
Remove the
woocommerce_new_orderhook (1 line removed).The Purchase event now relies on these hooks that fire at the correct time:
woocommerce_process_shop_order_meta(priority 20)woocommerce_checkout_update_order_meta(priority 30)woocommerce_thankyou(priority 40)All scenarios (checkout flow, manual admin orders, thank you page) remain fully covered by these hooks.
Code Changes
Before (Problematic)
After (Fixed)
One line removed. Simple and clean fix that addresses the root cause.
How This Resolves the Issue
Correct Hook Timing: By removing
woocommerce_new_orderand relying onwoocommerce_checkout_update_order_meta, we ensure the Purchase event fires after order items are added.Product Data Now Available: When
inject_purchase_event()runs,$order->get_items()now returns the actual order items with product information.Complete Event Data: Facebook now receives Purchase events with:
{ "content_ids": "[\"wc_post_id_123\", \"wc_post_id_456\"]", "content_name": "[\"Product Name 1\", \"Product Name 2\"]", "contents": "[{\"id\":\"wc_post_id_123\",\"quantity\":1}, {...}]", "order_id": 789 }All Scenarios Covered:
woocommerce_checkout_update_order_metahandles itwoocommerce_process_shop_order_metahandles itwoocommerce_thankyouhandles itTest Changes
Updated 3 unit tests to reflect this change:
remove_purchase_hooks()helper - Removed reference to deleted hooktest_inject_purchase_event_sends_capi_for_server_context()- Uses correct hook + adds product to test ordertest_inject_purchase_event_sends_single_capi_for_full_checkout_flow()- Uses correct hook + adds product to test orderTest Improvement Example
Tests now actually verify that product data is correctly extracted.
Why This Solution is Best
Testing Recommendations
Manual Testing (Recommended Before Deploy)
What to Verify
Risk Assessment
Low Risk because:
event_idis shared across hooks for Facebook deduplicationBackward Compatibility
While this technically removes a hook, it is fixing a bug. The current implementation sends incorrect data (empty arrays), so removing the problematic hook improves correctness rather than breaking functionality.
All user-facing scenarios remain fully functional:
woocommerce_checkout_update_order_meta)woocommerce_process_shop_order_meta)woocommerce_thankyou)Related Issues
This fix complements recent work on Purchase event handling:
Checklist
Additional Notes
The fix is straightforward and surgical. By using the correct WooCommerce hook, we ensure Purchase events contain complete product information from the start, enabling Facebook to properly track conversions and optimize ad campaigns.