php - Custom WordPress Function - Adding items from Foreach Loop into an array and Updating Field based on array of IDs (ACF + W

admin2025-01-07  4

I've searched far and wide for a solution to the problem. I'm almost there, but can't seem to get it working completely.

Background

There are 3 post types that are involved in this function: League, User, and Product.

I'm trying to write a custom function when a customer makes a WooCommerce order, they are added to a "League". The League is determined in the Product Meta using a Custom Relationship Field (via ACF). This field returns IDs based on the league that the product is associated with.

Then, the function uses the update_field action in ACF to update a SEPARATE relationship field that is associated with a User in the User Meta.

Desired Workflow

  1. User purchases Product A, which has an ACF relationship field associated with League A.
  2. WooCommerce grabs the League ID from the Order/Product Data.
  3. The ID is used to update the ACF field on the User Table.
  4. User is successfully added to the League after purchasing.

Where I'm Stuck

I've written the function and it works as expected if you replace $value with $newvalue in the update_field action in the code below. But, it completely replaces the value in custom field in the User table.

I understand that I need to first grab the existing values, add them to an array, and update the array with the new values. Then run the update_field action.

What I've Tried

I'm grabbing the new League IDs from a foreach loop inside of the function. I can't seem to get the League IDs out of this loop and into the array to update the field.

The code below returns nothing, so nothing is getting added to the array at all. But, I know the logic works because if I manually add League IDs to the array, the function executes as expected.

Finally My Code

add_action( 'woocommerce_payment_complete', 'action_payment_complete', 10, 2 );
function action_payment_complete( $order_id, $order ) {
    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    $user_id = $order->get_user_id();
    $acf_id = 'user_' . $user_id . '';
    // get current value
    $value = get_field('field_61b9677550d4c', $user_id, false);
    foreach ( $items as $item) {

        $product_id = $item->get_product_id();
        $newvalue = get_field('field_63128891a83f9', $product_id, false);
    }

        $value[] = $newvalue;

    update_field('field_61b9677550d4c',$value, $acf_id);

}

Working Code with MANUALLY added Items into Array (just to illustrate what indeed is working)

This code updates the User Meta and adds Leagues with ID 125 and 1768 to the User Meta. This works properly, but obviously is not grabbing the League IDs from the Product Meta.

add_action( 'woocommerce_payment_complete', 'action_payment_complete', 10, 2 );
function action_payment_complete( $order_id, $order ) {
    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    $user_id = $order->get_user_id();
    $acf_id = 'user_' . $user_id . '';
    // get current value
    $value = get_field('field_61b9677550d4c', $user_id, false);
    foreach ( $items as $item) {

        $product_id = $item->get_product_id();
        $newvalue = get_field('field_63128891a83f9', $product_id, false);
    }

        $value[] = 125;
      $value[] = 1768;

    update_field('field_61b9677550d4c',$value, $acf_id);

}

I've searched far and wide for a solution to the problem. I'm almost there, but can't seem to get it working completely.

Background

There are 3 post types that are involved in this function: League, User, and Product.

I'm trying to write a custom function when a customer makes a WooCommerce order, they are added to a "League". The League is determined in the Product Meta using a Custom Relationship Field (via ACF). This field returns IDs based on the league that the product is associated with.

Then, the function uses the update_field action in ACF to update a SEPARATE relationship field that is associated with a User in the User Meta.

Desired Workflow

  1. User purchases Product A, which has an ACF relationship field associated with League A.
  2. WooCommerce grabs the League ID from the Order/Product Data.
  3. The ID is used to update the ACF field on the User Table.
  4. User is successfully added to the League after purchasing.

Where I'm Stuck

I've written the function and it works as expected if you replace $value with $newvalue in the update_field action in the code below. But, it completely replaces the value in custom field in the User table.

I understand that I need to first grab the existing values, add them to an array, and update the array with the new values. Then run the update_field action.

What I've Tried

I'm grabbing the new League IDs from a foreach loop inside of the function. I can't seem to get the League IDs out of this loop and into the array to update the field.

The code below returns nothing, so nothing is getting added to the array at all. But, I know the logic works because if I manually add League IDs to the array, the function executes as expected.

Finally My Code

add_action( 'woocommerce_payment_complete', 'action_payment_complete', 10, 2 );
function action_payment_complete( $order_id, $order ) {
    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    $user_id = $order->get_user_id();
    $acf_id = 'user_' . $user_id . '';
    // get current value
    $value = get_field('field_61b9677550d4c', $user_id, false);
    foreach ( $items as $item) {

        $product_id = $item->get_product_id();
        $newvalue = get_field('field_63128891a83f9', $product_id, false);
    }

        $value[] = $newvalue;

    update_field('field_61b9677550d4c',$value, $acf_id);

}

Working Code with MANUALLY added Items into Array (just to illustrate what indeed is working)

This code updates the User Meta and adds Leagues with ID 125 and 1768 to the User Meta. This works properly, but obviously is not grabbing the League IDs from the Product Meta.

add_action( 'woocommerce_payment_complete', 'action_payment_complete', 10, 2 );
function action_payment_complete( $order_id, $order ) {
    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    $user_id = $order->get_user_id();
    $acf_id = 'user_' . $user_id . '';
    // get current value
    $value = get_field('field_61b9677550d4c', $user_id, false);
    foreach ( $items as $item) {

        $product_id = $item->get_product_id();
        $newvalue = get_field('field_63128891a83f9', $product_id, false);
    }

        $value[] = 125;
      $value[] = 1768;

    update_field('field_61b9677550d4c',$value, $acf_id);

}
Share Improve this question asked Sep 4, 2022 at 15:16 chrischris 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You need to set $value as array, no matter if the value you get is a single value or an array. And then push the other values in the Loop.

function action_payment_complete( $order_id, $order ) {

    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    $user_id = $order->get_user_id();
    $acf_id = 'user_' . $user_id . '';

    // get current value
    $value = (array)get_field('field_61b9677550d4c', $user_id, false);

    foreach ( $items as $item) {
        $product_id = $item->get_product_id();
        $newvalue = get_field('field_63128891a83f9', $product_id, false);
        if( !empty($newvalue) ){
            // push new value to array
            $value[] = $newvalue;
        } 
    }

    update_field('field_61b9677550d4c',$value, $acf_id);
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256643a378.html

最新回复(0)