本文介绍了在WooCommerce中将一些用户用户元数据添加为订单元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户创建一个订阅,并通过WooCommerce告诉我他需要什么.需求使用用户元数据存储在用户配置文件中.当他在前端的WooCommerce我的帐户区域中更新此内容时,它会在Wordpress客户个人资料中对其进行更新.那里没问题.

A Customer creates a subscription and tells me what he requires via WooCommerce. The requirements are stored inside of the user profile using the user meta data. When he updates this in the WooCommerce my account area on the front end, it updates it fine within the Wordpress customer profile. No problems there.

我遇到的问题是,当他通过前端WooCommerce帐户区域内的自定义字段更新需求时,订阅将不断地更新他的旧需求.我该如何使用它,以便在续订订阅时使用在帐户区域内找到的最新自定义字段数据?

The problem i have is, When he updates the requirements via a custom field inside of the WooCommerce account area on the front end, the subscription keeps renewing his old requirements over and over. How do i get it to so that when the subscription renews it uses the latest custom field data found inside the account area?

示例: https://i.stack.imgur.com/UEMpY.png

这是我目前所拥有的.在下面的此示例中,我尝试将用户元数据复制到订单元数据,并在订阅续订时对其进行更新.由于某种原因,它没有更新.也许这是错误的方法.我的PHP非常有限.有人可以建议吗?

This is what i have at the moment. In this example below, I tried to copy the user meta to the order meta and update it when the subscription renews. For some reason it's not updating. Perhaps this is the wrong way all together. My PHP is very limited. Can anyone advise?

// update subscription meta order
add_action('woocommerce_subscription_renewal_payment_complete','subscription_renew');
function subscription_renew( $order_id ) {

   $order = wc_get_order( $order_id ); // get the order id
   $user_id = $order->get_user_id(); // get the user id

   if ( $order->get_total() > 1 ) {  // not sure why this is here

   $getsend = get_user_meta( $user_id, 'send' ); // grab the user meta field 'send'
   $getcategories = get_user_meta( $user_id, 'categories' ); // grab the user meta field 'categories'

      // now do the update
      $order->update_meta_data( $user_id, 'send', $getsend ); // update user order meta field 'send'
      $order->update_meta_data( $user_id, 'categories', $getcategories ); // update user order meta field 'categories'
      $order_id = $order->save(); // save the order

      return $order_id;

   }

}

推荐答案

您的代码中有一些错误.您应该从以下位置删除 $ user_id 参数:

There are some mistakes in your code. You should remove $user_id argument from:

  // now do the update
  $order->update_meta_data( $user_id, 'send', $getsend ); // update user order meta field 'send'
  $order->update_meta_data( $user_id, 'categories', $getcategories ); // update user order meta field 'categories'

替换为:

  // now do the update
  $order->update_meta_data( 'send', $getsend ); // update user order meta field 'send'
  $order->update_meta_data( 'categories', $getcategories ); // update user order meta field 'categories'

现在应该可以更好地工作了.

Now it should better works.

更新:

当您在文档中查找 和其他一些代码示例,该挂钩具有2个参数: $ subscription $ last_order (但不是 $ order_id ).

It seems when looking to the docs and to some other code examples, that this hook has 2 arguments: $subscription and $last_order (but not $order_id).

这是一个真正有效的代码版本,将与您的自定义用户数据一起添加到上一个相关的续订订单中,作为订单元数据:

Here is a different code version that should really work, adding to the last related renewal Order, your custom user data as order meta data:

add_action( 'woocommerce_subscription_renewal_payment_complete', 'add_order_meta_from_custom_user_meta', 10, 2 );
function add_order_meta_from_custom_user_meta( $subscription, $last_order ) {
    if( ! $subscription )
        return;

    $user_id = $last_order->get_user_id(); // Get the user id

    if( $send = get_user_meta( $user_id, 'send', true ) ) {
        $last_order->update_meta_data( 'send', $send );
    }

    if( $categories = get_user_meta( $user_id, 'categories', true ) ) {
        $last_order->update_meta_data( 'categories', $categories );
    }

    if( isset($send) || isset($categories) ) {
        $last_order->save();
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.它应该更好地工作.

Code goes in function.php file of your active child theme (or active theme). It should better work.

这篇关于在WooCommerce中将一些用户用户元数据添加为订单元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:30
查看更多