Support

Account

Home Forums General Issues Update ACF field when WooCommerce order status switched to “Processing”

Solved

Update ACF field when WooCommerce order status switched to “Processing”

  • I’m working with WooCommerce and Advanced Custom Fields (ACF) in WordPress, and I’ve encountered a peculiar issue when trying to automatically update a custom field (order_shipping_number) in a WooCommerce order. I have two different methods for updating this field when an order’s status changes to “processing”, but only one of them works, and I’m trying to understand why.

    Method 1: Direct Update (Not Working) I tried a straightforward approach where I hook directly into the woocommerce_order_status_processing action and update the field using ACF’s update_field function:

    
    add_action( 'woocommerce_order_status_processing', 'update_product_custom_field_', 10, 1 );
    function update_product_custom_field_( $order_id ) {
        // Generate the shipping number using the order ID
        $shipping_number = 'SHIPPINGNUMBERTEST';
    
        // Update the custom field for the shipping number
        if (function_exists('update_field')) {
            update_field( 'order_shipping_number', $shipping_number, $order_id );
        }
    }

    This method seems logical, but it doesn’t update the field in the WooCommerce order.

    Method 2: Using acf/save_post (Working) However, when I use a nested action hook with acf/save_post, it works:

    
    add_action( 'woocommerce_order_status_processing', 'update_product_custom_field_');
    function update_product_custom_field_( $order_id ) {
        add_action('acf/save_post', 'update_shipping_number');
    }
    
    function update_shipping_number( $order_id ) {
        $shipping_number = 'SHIPPINGNUMBERTEST';
        update_field( 'order_shipping_number', $shipping_number, $order_id );
    }

    With this method, the field updates as expected.

    My Question: Why does the second method using acf/save_post work, but the direct approach fails to update the field? Is there something specific about how ACF handles data saving in the context of WooCommerce orders that requires the acf/save_post hook?

    Any insights or explanations would be greatly appreciated, as I’m trying to understand the underlying mechanics of ACF and WooCommerce interactions.

  • What are the steps you are performing to update the post to processing?

    When you are doing this does the field appear on the order to be edited?

  • The post is updated to Processing through native woocommerce usage ( inside the Order, it is switched to status “processing” and then pressed the button “Update”.

    The field is a standard text field, available in the Order page to be edited, this is correct.

    By the way when running the code with some form of debugging like this

    add_action( 'woocommerce_order_status_processing', 'update_product_custom_field_', 10, 1 );
    function update_product_custom_field_( $order_id ) {
        // Log start of function
        error_log("Updating custom field for order ID: $order_id");
    
        // Generate the shipping number using the order ID
        $shipping_number = 'SHIPPINGNUMBERHERE';
    
        // Check if ACF function exists and update the custom field
        if (function_exists('update_field')) {
            $result = update_field( 'order_shipping_number', $shipping_number, $order_id );
            if ($result) {
                error_log("Successfully updated order_shipping_number for order ID: $order_id");
            } else {
                error_log("Failed to update order_shipping_number for order ID: $order_id");
            }
        } else {
            error_log("ACF update_field function not found.");
        }
    }

    it prints to the logs “Successfully updated order_shipping_number for order ID:”

    which makes it seem like the update_field is running correctly, but for some reason is not saving?

  • I think that this is an order of operation issue.

    The WC woocommerce_order_status_processing fires before the WP save_post hook. ACF updates values on the save_post hook. If you try to update ACF fields on the first hook then ACF runs and updates it again and removes the value you just added.

    If the ACF field is available to enter the order_shipping_number when updating the order is there a specific reason that you want to update it with code?

  • So basically we have this field which is shipping number (order_shipping_number)
    that is normally updated manually through manual input on the order page.
    This field is normally updated before the order status is switched to “Completed”.

    On some ocasions, users will select different shipping methods,
    based on these shipping methods I am not updating the shipping number (order_shipping_number)
    automatically/programatically when it is set to “processing” (meaning the user has paid the order, so sometimes “Processing” is also switched automatically by the payment processors)
    (I use “woocommerce_order_status_processing” because this is the stage when customer has officially paid for the order.) so that there doesn’t need to be a manual input anymore but this is only for 40% of the orders. So the value still needs to be updated or changed manually sometimes, before the order is set to “completed”.

    So I understand that WP save_post hook fires after “woocommerce_order_status_processing”, overwriting the “update_field” that happened.
    But I admit I am not sure what would be the best practice to follow here given this situation

  • I don’t know the best solution.

    The first think I would try is using the first function you posted and using the field key instead of the field name in when calling update_field()

  • I eventually solved the problem by switching hooks to woocommerce_checkout_update_order_meta
    So that it saves on order creation and not on order status processing, because it works and it also is fine for the particular usecase.

    I assume on this webhook the saving of the ACF field is only running once so the issue brought up by John doesn’t happen.

    Thank you John for your feedback and knowledge as it was invaluable for me to learn more about how ACF works! some of this stuff would have been a headache to figure out on my own.

    
    add_action( 'woocommerce_checkout_update_order_meta', 'update_product_custom_field_', 10, 1 );
    function update_product_custom_field_( $order_id ) {
        // Generate the shipping number using the order ID
        $shipping_number = 'SHIPPINGNUMBERTEST';
    
        // Update the custom field for the shipping number
        if (function_exists('update_field')) {
            update_field( 'order_shipping_number', $shipping_number, $order_id );
        }
    }
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.