Support

Account

Forum Replies Created

  • 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 );
        }
    }
  • 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

  • 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?

  • Anyone? 🙁
    I can’t figure out why this code is not updating the field

  • So far I have managed to get the closest to what I need with this:

    <?php
    
    $phototext = get_field('communityphototext', get_post_thumbnail_id());
    $js_out = json_encode($phototext);
    
    ?>
    <script>
        var phototext = <?php echo $js_out; ?>;
    
    var communityImages = document.querySelectorAll('img');
    if( communityImages.length > 0 ){
      communityImages.forEach(function(image){
        image.outerHTML = '<div class="community-image">'+ phototext + image.outerHTML +'</div>';
      });
    }
        
    </script>

    I use PHP to get the ACF field and then send the variable to Javascript,
    I then use JS to programmatically add the ACF field to images.
    I then style it with CSS (not shown here)

    The problems I am facing:
    – I am not fluent in either PHP of JS and I am still learning, so this is as far as I have gotten.
    – This code is applying/showing the returned ACF value to all images that appear on the page, and not just to images that have the ACF field ‘communityphototext’ filled with some text or the field “CommunityPhoto” set as true.
    – The field I get using get_field('communityphototext', get_post_thumbnail_id()); seems to be the field of the main post image, so then it attributes the ACF value of the main post image to all images

    $value = get_field('field_name', $attachment_id);
    doesn’t seem to work to get an image ID, in php it returns nothing and when I pass it to JS it returns null.

    Thank you once again for the attention and advice, It has been quite a brain burner for me but I feel like I have never been so close to achieving this

  • Indeed I agree with you. This would just solve the issue that we need to import a few hundred products from another store. This is why I thought that purchasing 150$ of a product to do a one time import is unreasonable for me. Thank you for sharing your experience and letting me know.

Viewing 7 posts - 1 through 7 (of 7 total)