简体   繁体   English

自定义 WordPress Function - 将 Foreach 循环中的项目添加到数组中并根据 ID 数组更新字段(ACF + WooCommerce)

[英]Custom WordPress Function - Adding items from Foreach Loop into an array and Updating Field based on array of IDs (ACF + WooCommerce)

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.此 function 涉及 3 种帖子类型:联盟、用户和产品。

I'm trying to write a custom function when a customer makes a WooCommerce order, they are added to a "League".当客户下订单 WooCommerce 时,我正在尝试编写自定义 function,它们被添加到“联盟”。 The League is determined in the Product Meta using a Custom Relationship Field (via ACF).联赛是在 Product Meta 中使用自定义关系字段(通过 ACF)确定的。 This field returns IDs based on the league that the product is associated with.此字段根据产品关联的联赛返回 ID。

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.然后,function 使用 ACF 中的 update_field 操作来更新与用户元中的用户关联的单独关系字段。

Desired Workflow所需的工作流程

  1. User purchases Product A, which has an ACF relationship field associated with League A.用户购买产品 A,该产品具有与联赛 A 关联的 ACF 关系字段。
  2. WooCommerce grabs the League ID from the Order/Product Data. WooCommerce 从订单/产品数据中获取联赛 ID。
  3. The ID is used to update the ACF field on the User Table. ID 用于更新用户表上的 ACF 字段。
  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 .我已经编写了 function,如果您在下面代码的 update_field 操作中将 $value 替换为 $newvalue,它会按预期工作。 But, it completely replaces the value in custom field in the User table.但是,它完全替换了 User 表中自定义字段中的值。

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.然后运行 update_field 操作。

What I've Tried我试过的

I'm grabbing the new League IDs from a foreach loop inside of the function.我正在从 function 内部的 foreach 循环中获取新的联赛 ID。 I can't seem to get the League IDs out of this loop and into the array to update the field.我似乎无法从这个循环中获取联盟 ID 并进入数组以更新字段。

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.但是,我知道逻辑有效,因为如果我手动将联赛 ID 添加到数组中,function 会按预期执行。

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.此代码更新用户元并将 ID 为 125 和 1768 的联赛添加到用户元。 This works properly, but obviously is not grabbing the League IDs from the Product Meta.这可以正常工作,但显然没有从产品元中获取联赛 ID。

 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); }

Solved.解决了。 Here's the working 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 = array();
    $newvaluearray = array();
    $oldvalue = get_field('field_61b9677550d4c', $acf_id, false);
    

    foreach ( $items as $item) {
        $product_id = $item->get_product_id();
        $league_item = get_field('field_63128891a83f9', $product_id);
            foreach ( $league_item as $league_id) {
                $newvaluearray[] = $league_id;
            }

    }
    $new = array_merge_recursive($newvaluearray, $oldvalue);

    update_field('field_61b9677550d4c',$new, $acf_id);
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM