Wylly Jeremy
Wylly Jeremy

Reputation: 23

How do I hook into the WooCommerce Status = "checkout-draft"

my question revolves around hooking into the new WooCommerce order status draft. This happens when a customer adds products into the cart and views cart/ checkout and it is maintained until an order is placed at which point it changes to pending payment or processing.

What am I missing? I welcome all criticism on the code as much as it's a brief snippet and more-so a solution. If more information is needed please ask. See more info on the status I am talking about here: https://developer.woocommerce.com/2020/11/23/introducing-a-new-order-status-checkout-draft/

I have worked with the other statuses successfully such as

    function track_order_draft_duration($orderID, $old_status, $new_status) {
      //Order details
      global $woocommerce;
      $order = new WC_Order($orderID);

      if ($new_status == 'processing') {
        $msg = "something";
      }
    }
    add_action('woocommerce_order_status_changed', "track_order_draft_duration", 10, 3);

What I would really like to do is to check every time an order is placed into a cart and has the status of draft as shown below, log the duration before the status is changed, etc. WooCommerce dashboard

Upvotes: 1

Views: 329

Answers (1)

itzmekhokan
itzmekhokan

Reputation: 2768

Try below code snippet to do your stuff for order status - checkout-draft

function wc_track_order_draft_duration( $order ) {
    // Check for flag already set or not.
    $has_draft_logged = get_post_meta( $order->get_id(), '_draft_duration_logged', true );
    if ( $order->has_status( 'checkout-draft' ) && ! $has_draft_logged ) {

        // Do your log related code here.

        // And make sure to set a flag with order ID which prevent to override log.
        // So it will logged only once init of draft order created.

        // Once your task is done, set flag for future preventation.
        update_post_meta( $order->get_id(), '_draft_duration_logged', true );
    }
}
add_action( 'woocommerce_store_api_checkout_update_order_meta', 'wc_track_order_draft_duration' );

Upvotes: 0

Related Questions