For anyone looking to use this, I’ve amended my code:
/*
ACF - add repeater with purchases to user profile after order complete
*/
function mm_add_repeater_rows_on_order_complete( $order_id ) {
// set order object
$order = wc_get_order( $order_id );
// add rows for each product
foreach( $order->get_items() as $item_id => $item ) {
// get repeater field key
$field_key = 'field_58b69a624605c';
// get user id
$user = wp_get_current_user();
$user_id = 'user_' . $user->id;
// set field value as product name
$value = get_field( $field_key, $user_id );
$value[] = array( 'mm_user_challenge' => $item['product_id'] );
// update the field
update_field( $field_key, $value, $user_id );
}
// add order note to confirm
$order->add_order_note( __( 'Challenges successfully added to user profile.', 'woocommerce' ) );
}
add_action( 'woocommerce_order_status_completed', 'mm_add_repeater_rows_on_order_complete', 10, 1 );
Please accept my apologies for the late response! That solved it, thank you John! 🙂
Update:
I’ve now hooked into when an order has been purchased and set to complete:
woocommerce_order_status_completed
I’ve also managed to set the user_id:
$user = wp_get_current_user();
$user_id = 'user_' . $user->id;
I found the issue immediately:
the_field( 'mm_challenge_test', $order_id );
actually needs to be:
the_field( 'mm_challenge_test', $order->id );
Hi John!
Ah, righto! In my case I want to dynamically populate the text field with the date of the post. Then if the post is backdated and updated, that date will then update also.
Thank you so much!
FIXED
I solved it, no worries. For anyone else wanting to implement this:
/*
Add featherlight data attribute to ACF gallery
*/
function add_featherlight_gallery_attribute( $link ) {
return str_replace( '>', ' data-featherlight="image">', $link );
}
add_filter( 'wp_get_attachment_link', 'add_featherlight_gallery_attribute', 10, 2 );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.