Support

Account

Home Forums Add-ons Repeater Field Adding new row of repeater in PHP

Solving

Adding new row of repeater in PHP

  • Hi
    I have a product with attached repeater field. On product save, I want to add a new row.
    repeater field has 4 dates in it (in format ddmmYY).

    Currently, there is no repeater data in the product (I can see repeater and button to add a new row).

    So, for a test I do following (in ‘save_post_product’ hook):
    rows = [[
    'event_date' => "30062025",
    'shipping_date' => "30062025",
    'return_send_date' => "30062025",
    'return_arrival_date' => "30062025",
    ]];
    $status = update_field('rental_info', $rows, $post_id);

    $status is true, but new row is not appearing in the product.
    I tried to use subfield IDs instead of names. Result is same. Also tried to use field ID instead of repeater name. Same.

    Any ideas why new row is not being created?

  • Goodday rudolfl,

    The only thing that i can think of is that your save_post_product() function is called before the acf/save_post function. if that is the case it posiable that your value is overwritten.

    I recommend setting your save_post_product() to mabye prioity 999 to check if this is true?
    I hope this will help if not can you send me the full function of you doing this?

    With kind regards,
    Mauro

  • I am unable to post reply with my code. Submitting, screen refreshes and reply is not posted.
    Tried different machines and browsers.

  • This is my test code I am using at the moment

    add_action('save_post_product', 'rl_acf_test', 999);
    function rl_acf_test($post_id) {
        // Avoid infinite loop when using update_field()
        remove_action('save_post_product', __FUNCTION__);
    
        // Check if stock is managed
        $product = wc_get_product($post_id);
        if (!$product ) {
            return;
        }
    
        // Update the repeater
                $rows = [[
                        'event_date' => "30062025",
                        'shipping_date' => "30062025",
                        'return_send_date' => "30062025",
                        'return_arrival_date' => "30062025",
                ]];
        //$status = update_field($repeater_name, $new_rows, $post_id);
        //$status = update_field($acfFieldObject['key'], $rows, $post_id);
        $status = update_field('rental_info', $rows, $post_id);
        error_log("Status is: " . var_export($status,true));
        $product->save();
    }
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.