Home Forums Backend Issues (wp-admin) Add row to repeater for each product purchased and predefine post object value


Add row to repeater for each product purchased and predefine post object value

  • Hi there!

    I have a repeater assigned to users with the ‘customer’ role. The repeater has one post_object field.

    I am trying to create a new row for each product purchased by the user. In addition to this, I need to predefine the post_object value of each row to each individual product.

    End result (within user edit screen) should be:

    User has purchased 3 products, 2 cats and 1 dog:

    row #1 – post_object value set to ‘cat’ (most recent purchase)
    row #2 – post_object value set to ‘dog’
    row #3 – post_object value set to ‘cat’

    I have the following, which seems to work, however I’m not sure how to go about not defining the user ID to a specific user.

    And I don’t believe I have the correct action.

    function mm_add_repeater_rows() {
    	$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
    		'numberposts' => $order_count,
    		'meta_key' => '_customer_user',
    		'meta_value' => get_current_user_id(),
    		'post_type' => 'shop_order',
    		'post_status' => 'publish'
    	) ) );
    	foreach ( $customer_orders as $customer_order ) {
    		$order = new WC_Order();
    		$order->populate( $customer_order );
    		$order_item = $order->get_items();
    		foreach( $order_item as $product ) {
    			$product_obj = new WC_Product( $product['product_id'] );
    			$user = wp_get_current_user();
    			$field_key = 'field_58b69a624605c';
    			$user_id = 'user_25'; // needs to be undefined.
    			$value = get_field( $field_key, $user_id );
    			$value[] = array( 'mm_user_challenge' => $product['product_id'] );
    			update_field( $field_key, $value, $user_id );
    add_action( 'admin_init', 'mm_add_repeater_rows' );

    At the moment, it repeats the rows every time I load the user profile. Any advice is greatly appreciated 🙂

  • Update:

    I’ve now hooked into when an order has been purchased and set to complete:


    I’ve also managed to set the user_id:

    $user = wp_get_current_user();
    $user_id = 'user_' . $user->id;
  • 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 );
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.