Support

Account

Home Forums Backend Issues (wp-admin) Can’t read data from ACF during import with WP All Import

Helping

Can’t read data from ACF during import with WP All Import

  • Can’t read data from ACF during import with WP All Import

    Hey everyone,

    First of all, I already asked the WP All Import Pro Support to help me but they had no idea and they “do not give any support to custom code”. So I really hope I can get some help here.

    I am importing a .csv file with products to my WooCommerce shop and I have a custom function that saves a json string to an acf field. (I have to save pairs of sizes for a products with a unique url for each size.)

    This is pretty straight forward and saving a string to the field works just fine but I do not get any value when I try to read from the same ACF Field so at the end of the import I only have the latest size/url pair in the json string saved.

    I tried to use get_field as well as get_post_meta, but it’s the same problem with both functions – no data.

    I am almost sure that it did work a couple of weeks ago when I first wrote the script but some update broke it and I have no idea how to fix it rn.

    Here is the code:

    <?php
    function update_product_sizes($post_id, $data ) {
    
        // The current data row from the imported .csv
        $row = json_decode(json_encode((array) $data), true);
        $size = $row["size"];
        $affiliate_url = $row["link"];
        $product_name = $row["title"];
    
        // Der ACF Field Key mit den Daten Groesse und URL
        $field_key = "field_6628d713e57dd"; // Replace with the actual field key for your ACF field
    
        // Step 1: Retrieve the ACF field value (JSON data)
        $json_data = get_field($field_key, $post_id);
        //$json_data = get_post_meta($post_id,$field_key,true);
    
        ob_start();
        print_r($json_data);
        $output = ob_get_clean();
    
        add_log_msg("Retreived ACF Data: " . $output . ", PostID: " . $post_id . "<br />");
    
        // Step 2: Decode the JSON data into a PHP array
        $data_array = $json_data ? json_decode($json_data, true) : array();
    
        // Step 3: Define the new size and URL to be added
        $new_entry = array(
            'size' => $size,  // size
            'url' => $affiliate_url  // Affiliate URL
        );
    
        // Step 4: Add the new entry to the array
        $data_array[] = $new_entry;
    
        // Step 5: Encode the PHP array back to JSON
        $new_json_data = json_encode($data_array);
    
        // Step 6: Update the field with the new JSON data
        $update = update_field($field_key, $new_json_data, $post_id);
    
        if($update) {
            $log_msg = "New Size and URL added for product: " . $product_name . ", Size: ". $size .", URL: ". $affiliate_url . ", data: ". $new_json_data . ", post ID: ". $post_id . ", Field Key: ". $field_key . "<br/>";
            add_log_msg($log_msg);        
        } else {
            $log_msg = "Update failed.";
            add_log_msg($log_msg);    
        }
    
    }
    
    // Hook into WP All Import
    add_action('pmxi_saved_post', 'update_product_sizes', 10, 2);
    
    function add_log_msg($m) {
        printf("[%s] %s", date("H:i:s"), $m);
        flush();
    }
    
    ?>

    Any help or hints on what to try is very much appreciated. Thanks!

  • I figured it out. If I do not check the checkbox for Advanced Custom Fields under the option “Update existing WooCommerce products with the data in this import file” it works as expected.

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

You must be logged in to reply to this topic.