Monday, September 5, 2022
HomeWordPress Developmentphp - Customized WordPress Operate - Including objects from Foreach Loop into...

php – Customized WordPress Operate – Including objects from Foreach Loop into an array and Updating Discipline primarily based on array of IDs (ACF + WooCommerce)


I’ve searched far and huge for an answer to the issue. I am virtually there, however can not seem to get it working fully.

Background

There are 3 submit sorts which can be concerned on this operate: League, Consumer, and Product.

I am making an attempt to put in writing a customized operate when a buyer makes a WooCommerce order, they’re added to a “League”. The League is decided within the Product Meta utilizing a Customized Relationship Discipline (by way of ACF). This discipline returns IDs primarily based on the league that the product is related to.

Then, the operate makes use of the update_field motion in ACF to replace a SEPARATE relationship discipline that’s related to a Consumer within the Consumer Meta.

Desired Workflow

  1. Consumer purchases Product A, which has an ACF relationship discipline related to League A.
  2. WooCommerce grabs the League ID from the Order/Product Knowledge.
  3. The ID is used to replace the ACF discipline on the Consumer Desk.
  4. Consumer is efficiently added to the League after buying.

The place I am Caught

I’ve written the operate and it really works as anticipated if you happen to substitute $worth with $newvalue within the update_field motion within the code beneath. However, it fully replaces the worth in customized discipline within the Consumer desk.

I perceive that I have to first seize the prevailing values, add them to an array, and replace the array with the brand new values. Then run the update_field motion.

What I’ve Tried

I am grabbing the brand new League IDs from a foreach loop within the operate. I can not seem to get the League IDs out of this loop and into the array to replace the sphere.

The code beneath returns nothing, so nothing is getting added to the array in any respect. However, I do know the logic works as a result of if I manually add League IDs to the array, the operate executes as anticipated.

Lastly My Code

add_action( 'woocommerce_payment_complete', 'action_payment_complete', 10, 2 );
operate action_payment_complete( $order_id, $order ) {
    $order = wc_get_order( $order_id );
    $objects = $order->get_items();
    $user_id = $order->get_user_id();
    $acf_id = 'user_' . $user_id . '';
    // get present worth
    $worth = get_field('field_61b9677550d4c', $user_id, false);
    foreach ( $objects as $merchandise) {

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

        $worth[] = $newvalue;

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

}

Working Code with MANUALLY added Objects into Array (simply as an instance what certainly is working)

This code updates the Consumer Meta and provides Leagues with ID 125 and 1768 to the Consumer Meta. This works correctly, however clearly isn’t grabbing the League IDs from the Product Meta.

add_action( 'woocommerce_payment_complete', 'action_payment_complete', 10, 2 );
operate action_payment_complete( $order_id, $order ) {
    $order = wc_get_order( $order_id );
    $objects = $order->get_items();
    $user_id = $order->get_user_id();
    $acf_id = 'user_' . $user_id . '';
    // get present worth
    $worth = get_field('field_61b9677550d4c', $user_id, false);
    foreach ( $objects as $merchandise) {

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

        $worth[] = 125;
      $worth[] = 1768;

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

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments