Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhook "Order Created" is a bit confusing as to when it fires off. #37958

Open
5 tasks done
slash1andy opened this issue Apr 24, 2023 · 2 comments
Open
5 tasks done

Webhook "Order Created" is a bit confusing as to when it fires off. #37958

slash1andy opened this issue Apr 24, 2023 · 2 comments
Labels
focus: webhook Issues related to WooCommerce webhooks. plugin: woocommerce Issues related to the WooCommerce Core plugin. priority: normal The issue/PR is of normal priority—not many people are affected or there’s a workaround, etc. team: Proton type: enhancement The issue is a request for an enhancement.

Comments

@slash1andy
Copy link
Member

Prerequisites

  • I have carried out troubleshooting steps and I believe I have found a bug.
  • I have searched for similar bugs in both open and closed issues and cannot find a duplicate.

Describe the bug

When using the Checkout Block, the webhook "order created" is fired off for a draft checkout, once the customer hits the checkout page.

This can cause some confusion and issues with some integrations, as I would not expect an order to be created there at that time.

We've switched to using an action based webhook that fires off on woocommerce_order_status_processing instead. This also is not without issues, as that usually won't send the actual order items / content.

Expected behavior

I would expect order creation to not happen until the customer clicked "checkout", so no order would be created until then.

For ease of use, echoing what Mike Jolley said in slack I'd say that it may make sense to swap the webhook system to use a status based hook, or create a separate webhook that takes status into consideration e.g. a webhook that fires once the order is processing or complete only.

Actual behavior

Triggering a webhook on order.created fires off when a draft checkout is created when using WooCommerce Block checkout.

Steps to reproduce

Create a webhook on order.created on a site using WooCommerce Block checkout.
Go to checkout and see that it fires off the webhook even before completing checkout.

WordPress Environment

`

WordPress Environment

WordPress address (URL): https://store.a8c.com
Site address (URL): https://store.a8c.com
WC Version: 7.6.0
REST API Version: ✔ 7.6.0
WC Blocks Version: ✔ 10.0.2
Action Scheduler Version: ✔ 3.5.4
Log Directory Writable: ✔
WP Version: 6.2
WP Multisite: –
WP Memory Limit: 512 MB
WP Debug Mode: ✔
WP Cron: –
Language: en_US
External object cache: ✔

Server Environment

Server Info: nginx
PHP Version: 8.0.28
PHP Post Max Size: 2 GB
PHP Time Limit: 1200
PHP Max Input Vars: 6144
cURL Version: 7.86.0
OpenSSL/1.1.1n

SUHOSIN Installed: –
Max Upload Size: 1 GB
Default Timezone is UTC: ✔
fsockopen/cURL: ✔
SoapClient: ✔
DOMDocument: ✔
GZip: ✔
Multibyte String: ✔
Remote Post: ✔
Remote Get: ✔

Database

WC Database Version: 7.6.0
WC Database Prefix: wp_
Total Database Size: 872.38MB
Database Data Size: 501.97MB
Database Index Size: 370.41MB
wp_woocommerce_sessions: Data: 1.02MB + Index: 0.02MB + Engine InnoDB
wp_woocommerce_api_keys: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_attribute_taxonomies: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_woocommerce_downloadable_product_permissions: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_woocommerce_order_items: Data: 11.52MB + Index: 3.52MB + Engine InnoDB
wp_woocommerce_order_itemmeta: Data: 179.73MB + Index: 126.30MB + Engine InnoDB
wp_woocommerce_tax_rates: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_woocommerce_tax_rate_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_shipping_zones: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_woocommerce_shipping_zone_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_shipping_zone_methods: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_woocommerce_payment_tokens: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_woocommerce_payment_tokenmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_log: Data: 2.02MB + Index: 0.09MB + Engine InnoDB
wp_a8c_cron_control_jobs: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_actionscheduler_actions: Data: 1.02MB + Index: 1.08MB + Engine InnoDB
wp_actionscheduler_claims: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_actionscheduler_groups: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_actionscheduler_logs: Data: 1.02MB + Index: 0.59MB + Engine InnoDB
wp_advanced_notification_triggers: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_advanced_notifications: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_cart_notices: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_commentmeta: Data: 0.05MB + Index: 0.03MB + Engine InnoDB
wp_comments: Data: 43.58MB + Index: 22.09MB + Engine InnoDB
wp_links: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_options: Data: 2.31MB + Index: 1.13MB + Engine InnoDB
wp_postmeta: Data: 215.75MB + Index: 174.33MB + Engine InnoDB
wp_posts: Data: 7.52MB + Index: 6.77MB + Engine InnoDB
wp_term_relationships: Data: 1.52MB + Index: 1.42MB + Engine InnoDB
wp_term_taxonomy: Data: 0.05MB + Index: 0.03MB + Engine InnoDB
wp_termmeta: Data: 0.05MB + Index: 0.03MB + Engine InnoDB
wp_terms: Data: 0.05MB + Index: 0.03MB + Engine InnoDB
wp_usermeta: Data: 11.52MB + Index: 10.03MB + Engine InnoDB
wp_users: Data: 0.39MB + Index: 0.44MB + Engine InnoDB
wp_wc_admin_note_actions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_wc_admin_notes: Data: 0.06MB + Index: 0.00MB + Engine InnoDB
wp_wc_category_lookup: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_wc_customer_lookup: Data: 0.38MB + Index: 0.22MB + Engine InnoDB
wp_wc_download_log: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_wc_order_bundle_lookup: Data: 1.52MB + Index: 1.14MB + Engine InnoDB
wp_wc_order_composite_lookup: Data: 0.02MB + Index: 0.09MB + Engine InnoDB
wp_wc_order_coupon_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_wc_order_product_lookup: Data: 14.55MB + Index: 17.06MB + Engine InnoDB
wp_wc_order_stats: Data: 2.52MB + Index: 2.42MB + Engine InnoDB
wp_wc_order_tax_lookup: Data: 0.13MB + Index: 0.13MB + Engine InnoDB
wp_wc_product_attributes_lookup: Data: 0.14MB + Index: 0.16MB + Engine InnoDB
wp_wc_product_download_directories: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_wc_product_meta_lookup: Data: 0.34MB + Index: 0.52MB + Engine InnoDB
wp_wc_rate_limits: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_wc_reserved_stock: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_wc_tax_rate_classes: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_wc_webhooks: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_woocommerce_bundled_itemmeta: Data: 0.13MB + Index: 0.16MB + Engine InnoDB
wp_woocommerce_bundled_items: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_exported_csv_items: Data: 2.52MB + Index: 0.02MB + Engine InnoDB

Post Type Counts

attachment: 1722
custom_css: 1
customize_changeset: 2
jp_pay_product: 1
nav_menu_item: 51
order_shipment: 82
page: 21
post: 14
product: 304
product_variation: 1012
revision: 259
shop_order: 17866
shop_order_refund: 23
slide: 1
wc_membership_plan: 5
wc_user_membership: 352
wcpf_item: 4
wcpf_project: 1
wp_global_styles: 2
wp_navigation: 1
wp_template: 5
wp_template_part: 4

Security

Secure connection (HTTPS): ✔
Hide errors from visitors: ✔

Active Plugins (16)

a11n SSO Helper: by Brandon Kraft – 1.2.0
Custom functions for a8c Store: by Andrew Wikel – 1.0
Storefront Powerpack: by WooCommerce – 1.6.2
Swag Retail Value Totals: by –
Swag Store Custom Analytics: by WooCommerce – 0.1.0
WooCommerce Blocks: by Automattic – 10.0.2
WooCommerce Checkout Field Editor: by WooCommerce – 1.7.7
WooCommerce Cost of Goods: by SkyVerge – 2.12.0
WooCommerce Min/Max Quantities: by WooCommerce – 4.0.6
WooCommerce Product Bundles: by WooCommerce – 6.17.3
WooCommerce Shipment Tracking: by WooCommerce – 1.9.0
WooCommerce Variation Swatches and Photos: by Element Stark – 3.1.6
WooCommerce Waitlist: by Neil Pie – 2.3.5
WooCommerce: by Automattic – 7.6.0

Inactive Plugins (0)

Dropin Plugins (2)

db.php: db.php
object-cache.php: object-cache.php

Must Use Plugins (38)

Admin Notice: by WordPress VIP – 0.1.0
Advanced Post Caching: by Automattic – 0.2
Akismet Anti-Spam: by Automattic – 4.2.4
Async Publish Actions: by Automattic –
blogpublic-notice.php: by –
HTTP Concat: by Automattic – 1.0
Jetpack: by Automattic – 12.0
Lightweight Term Count Update: by Automattic
Alley Interactive – 0.1.0

prometheus.php: by –
Query Monitor: by John Blackbourn – 3.10.1
REST API Enhancements: by Erick Hitter
Automattic – 0.1

Rewrite Rules Inspector: by Automattic
Daniel Bachhuber – 1.3.1

VaultPress: by Automattic – 2.2.1
VIP AllOptions Safeguard: by Automattic –
VIP Back-compat: by Automattic – 1.0
VIP Cache Manager: by Automattic – 1.1
VIP Client mu-plugins: by Automattic –
VIP Codebase Manager: by Automattic – 1.0.0
VIP Cron Enhancements: by Automattic – 1.0
VIP Dashboard: by Scott Evans
Filipe Varela
Pau Argelaguet – 3.0.0

VIP Feed Cache: by Automattic –
VIP File Service: by Automattic – 0.2
VIP Force Two Factor: by Automattic –
VIP Go Core Modifications: by Automattic –
VIP Go Plugin Compat: by Automattic – 1.0
VIP Hosting Miscellaneous: by Automattic – 1.1
VIP Init: by Automattic –
VIP Mail: by Automattic – 1.0
VIP Parse.ly Integration: by Automattic – 1.0
VIP Performance: by Automattic – 1.0
VIP Plugins: by Automattic – 1.0.0
VIP Schema: by Automattic –
VIP Security: by Automattic – 1.2
VIP Stats: by Automattic –
WooCommerce: VIP Specific Changes: by Automattic – 1.0.0
WordPress Importer: by wordpressdotorg – 0.6.4
WordPress VIP Support: by Automattic – 3.1.0
WP-CLI for VIP Go: by Automattic –

Settings

API Enabled: ✔
Force SSL: –
Currency: USD ($)
Currency Position: left
Thousand Separator: ,
Decimal Separator: .
Number of Decimals: 2
Taxonomies: Product Types: bundle (bundle)
composite (composite)
external (external)
grouped (grouped)
simple (simple)
variable (variable)

Taxonomies: Product Visibility: exclude-from-catalog (exclude-from-catalog)
exclude-from-search (exclude-from-search)
featured (featured)
outofstock (outofstock)
rated-1 (rated-1)
rated-2 (rated-2)
rated-3 (rated-3)
rated-4 (rated-4)
rated-5 (rated-5)

Connected to WooCommerce.com: –
Enforce Approved Product Download Directories: –
Order datastore: WC_Order_Data_Store_CPT

WC Pages

Shop base: #7 - /shop/
Cart: #8 - /cart/
Checkout: #33221 - /temp-checkout/
My account: #10 - /my-account/
Terms and conditions: ❌ Page not set

Theme

Name: Storefront
Version: 3.9.1 (update to version 4.2.0 is available)
Author URL: https://woocommerce.com/
Child Theme: ❌ – If you are modifying WooCommerce on a parent theme that you did not build personally we recommend using a child theme. See: How to create a child theme
WooCommerce Support: ✔

Templates

Overrides: /var/www/wp-content/plugins/woo-gutenberg-products-block/templates/notices/error.php
/var/www/wp-content/plugins/woo-gutenberg-products-block/templates/notices/notice.php
/var/www/wp-content/plugins/woo-gutenberg-products-block/templates/notices/success.php

Product Bundles

Database Version: 6.17.3
Loopback Test: ✔
Template Overrides: –

Admin

Enabled Features: activity-panels
analytics
coupons
customer-effort-score-tracks
import-products-task
experimental-fashion-sample-products
shipping-smart-defaults
shipping-setting-tour
homescreen
marketing
multichannel-marketing
mobile-app-banner
navigation
onboarding
onboarding-tasks
remote-inbox-notifications
remote-free-extensions
payment-gateway-suggestions
shipping-label-banner
subscriptions
store-alerts
transient-notices
woo-mobile-welcome
wc-pay-promotion
wc-pay-welcome-page

Disabled Features: block-editor-feature-enabled
minified-js
new-product-management-experience
product-variation-management
settings

Daily Cron: ✔ Next scheduled: 2023-04-24 21:14:49 +00:00
Options: ✔
Notes: 23
Onboarding: completed

Action Scheduler

Complete: 3,026
Oldest: 2023-03-24 17:12:08 +0000
Newest: 2023-04-24 16:48:00 +0000

Failed: 63
Oldest: 2023-03-31 16:35:56 +0000
Newest: 2023-04-24 16:48:00 +0000

Pending: 3
Oldest: 2023-04-25 16:48:00 +0000
Newest: 2023-04-25 16:48:00 +0000

Status report information

Generated at: 2023-04-24 17:07:54 +00:00
`

Isolating the problem

  • I have deactivated other plugins and confirmed this bug occurs when only WooCommerce plugin is active.
  • This bug happens with a default WordPress theme active, or Storefront.
  • I can reproduce this bug consistently using the steps above.
@github-actions github-actions bot added the status: awaiting triage This is a newly created issue waiting for triage. label Apr 24, 2023
@ObliviousHarmony ObliviousHarmony added type: bug The issue is a confirmed bug. focus: webhook Issues related to WooCommerce webhooks. plugin: woocommerce Issues related to the WooCommerce Core plugin. status: prioritization and removed status: awaiting triage This is a newly created issue waiting for triage. labels May 1, 2023
@peterfabian
Copy link
Contributor

Thanks for reporting. I would actually say this is an enhancement rather than a bug.

I would suggest we do the following:

  • document somewhere around the webhook docs that orders can be created also in draft status and what the implications of this are for order.created
  • create a new webhook that would be fired once the order has been paid and it would include order details (I don't think we can easily change the semantics of order.created as we never know if someone isn't using it to track orders in different ways)

Alternatively, would it make sense to go more general and allow filtering of webhook content so that ppl would be allowed to add their own data to the webhooks?

As an alternative here, would using order.updated be an option in combination with a check for the desired order status?

@peterfabian peterfabian added type: enhancement The issue is a request for an enhancement. priority: normal The issue/PR is of normal priority—not many people are affected or there’s a workaround, etc. and removed type: bug The issue is a confirmed bug. status: prioritization labels May 22, 2023
@slash1andy
Copy link
Member Author

Thanks for looking at this.

I would agree that it's kinda on the line between bug and improvement, with me tagging it as a bug because I believed the plain reading of "Order Created" didn't meet what was happening here, but improvement is also valid.

Your suggestions seem ideal for what I would expect, with clearer docs and then an easy way to trigger on a "ready to ship" order (for lack of a better term).

As far as making the webhooks more general and filterable, I'd say that they are already mostly where I'd expect, unless there's a use case I am missing.

Here's what I ended up doing to get a webhook firing off on a "ready to ship" order, set to trigger on 'action.woocommerce_order_status_processing' :

`/**

  • Set the proper resource payload for a custom action webhook

  • @param int $target_webhook_id

  • @param string $desired_resource 'order', 'product', 'coupon', or 'customer'
    */
    function set_resource_for_webhook_payload_by_webhook_id($target_webhook_id, $desired_resource) {

    // Set the desired_resource payload for this webhook ('order', 'product', 'coupon', 'customer')
    add_filter('woocommerce_webhook_resource', function($resource, $webhook_id) use ($target_webhook_id, $desired_resource) {
    if($webhook_id == $target_webhook_id) {
    return $desired_resource;
    }
    return $resource;
    }, 10, 2);

    // Need to ensure our event (i.e., action) is seen as valid, as we've changed the default 'action.' prefix for the topic above
    add_filter('woocommerce_valid_webhook_events', function($valid_events) use ($target_webhook_id) {
    try {
    $topic = wc_get_webhook($target_webhook_id)->get_topic();

         list($resource, $event) = explode('.', $topic);
    
         if(!empty($event)) {
             $valid_events[] = $event;
         }
    
         return $valid_events;
    
     } catch (Exception $e) {
         return $valid_events;
     }
    

    }, 10);

}

// Change the Webhook ID to suit and data source of order, product, coupon or customer
add_action('init', function(){
set_resource_for_webhook_payload_by_webhook_id( 3, 'order' );
});`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus: webhook Issues related to WooCommerce webhooks. plugin: woocommerce Issues related to the WooCommerce Core plugin. priority: normal The issue/PR is of normal priority—not many people are affected or there’s a workaround, etc. team: Proton type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

No branches or pull requests

4 participants