Support » Plugin: Advanced Custom Fields: Extended » Trigger bidirectionnal update during import with Wp All Import

  • Resolved Dexter0015

    (@improvedline)


    Hi,

    I have ACF Pro and I have a hierarchy build between 2 post types :

    – Vente (cpt) – the “parent”
    – Product (woocommerce product) – the “child”

    So a single vente is linked to several products.

    I use the ACF relation field on both sides to implement the relation.

    And with the help of your plugin, I manage to set the relation as bidirectionnal.
    Thanks for that, it’s works great.

    My issue is that I need to import a lot of products on the website.
    To do so i use WP All Import (pro) to import products and affect the correct vente to each product.

    After import, the vente is correctly linkged to the product, from the product side, however, from the vente side, the products are not linked.

    Since a single vente can be linked to up to 600 products, it would be a pain to affect each product to the vente from the vente form.

    I tried to use the filter :

    add_filter('acfe/bidirectional/force_update', '__return_true');

    but it doesn’t seems to do anything in this context (I don’t even know if it’s triggered at all).

    Any idea how I can trigger the bidirectional update during the import?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    The Bidirectional Setting works with all native ACF functions such as update_field() (See documentation). When using those functions, it will update the value on both side.

    If you use field names that are not unique (ie: you have multiple fields that have the same name the bidirectional field), you could try to use field key instead of field name in the the update_field() function. Example: update_field('field_61bb43173492f', 12, $post_id).

    This is because ACF will search for the field key reference related to the field name in the database, as meta values (that hold the correct reference) don’t exist yet on new posts. Then ACF will stop at the first field that has that name, and if that’s not the correct field it won’t understand it has to update it bidirectionally.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter Dexter0015

    (@improvedline)

    Hi,

    Thanks for your quick answer.

    I manage to achieve what I wanted to do by forcing the field to be updated each time a product is imported, and it seems to do the trick.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Okay then, I’m glad to hear it works as expected!

    Have a nice day!

    Regards.

    Hi there,

    I’m interested in how did you enforced ACF fields to be updated bidirectionally after import process, I’m walking in similar shoes as you were recently.

    Thanks and regards.

    Thread Starter Dexter0015

    (@improvedline)

    Hi,

    In my addon, I attached a function on the “pmxi_saved_post” action hook
    see the documentation here: https://www.wpallimport.com/documentation/advanced/action-reference/#pmxi_saved_post

    add_action( 'pmxi_saved_post', [$this, 'update_saved_post_acf_fields'], 10, 3);

    And here is the content of the function:

    public function update_saved_post_acf_fields( $post_id, $xml_node, $is_update ) {
    
        // Only for specified post type (if needed)
        if( get_post_type( $post_id ) != 'specific_post_type' ) {
            return;
        }
    
        // Ignore if acf is not installed/activated
        if( ! function_exists( 'get_field' ) || ! function_exists( 'update_field' ) ) {
            return;
        }
    
        // Force update of field_name's value
        $field_value = get_field( 'field_name', $post_id );
    
        if( $field_value ) {
            // 1 - Reset value
            $new_field_value = [];
            update_field( 'field_name', $new_field_value, $post_id );
            // 2 - Re update value (to ensure value is indeed updated)
            update_field( 'field_name', $field_value, $post_id );
        }
    }

    Hi,

    Thanks for quick answer, really appreciate it.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.