简体   繁体   中英

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. 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.

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?

Example: 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. 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:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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