简体   繁体   English

在 WooCommerce 中添加一些用户用户元数据作为订单元数据

[英]Add some user user meta data as order meta data in WooCommerce

A Customer creates a subscription and tells me what he requires via WooCommerce.客户创建订阅并通过 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.当他在前端的 WooCommerce 我的帐户区域中更新此内容时,它会在 Wordpress 客户资料中正常更新。 No problems there.那里没有问题。

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.我遇到的问题是,当他通过前端 WooCommerce 帐户区域内的自定义字段更新需求时,订阅会一遍又一遍地更新他的旧需求。 How do i get it to so that when the subscription renews it uses the latest custom field data found inside the account area?我如何让它在订阅续订时使用在帐户区域内找到的最新自定义字段数据?

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

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.我的 PHP 非常有限。 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;

   }

}

There are some mistakes in your code.您的代码中有一些错误。 You should remove $user_id argument from:您应该从以下位置删除$user_id参数:

  // 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'

replacing by:替换为:

  // 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.现在它应该更好地工作。


Update related to woocommerce_subscription_renewal_payment_complete hook:woocommerce_subscription_renewal_payment_complete挂钩相关的更新

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 ) .在查看文档和其他一些代码示例时,这个钩子似乎有 2 arguments: $subscription$last_order (但不是$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();
    }
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 It should better work.它应该更好地工作。

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

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