0

I have a user meta field ('pkg_code') I would like to copy to a custom field ('log') on the users' order(s) upon update of their user profile.

I have looked through many similar posts on stackoverflow and elsewhere, trying a number of things. Most posts run this update of this kind when an order is placed, which would be fine going forward, but I need to append the user meta to previous orders right now, so the flow has to be on user profile update.

It seems maybe I have to have more than one action or an action and a filter to achieve the post meta update when user meta update occurs. But the hooks evade me. Greatly appreciate any and all thoughts.

add_action ( 'profile_update', 'sync_pkg', 11, 2 );

function sync_pkg( $user_id ) { 
    // On profile update GET pkg_code value save to variable $pkg

    $pkg = get_user_meta( $user_id, 'pkg_code', true );
    
    // with pkg value set, fetch orders belonging to the customer, and update the order with the pkg code value
    
    $args = array(
        'customer_id' => $user_id
    );
   
   $orders = wc_get_orders($args);

    foreach ( $orders as $order ) {
         
        $order_id = $order->get_id(); 
        update_post_meta($order_id, 'log', $pkg ); // add and save the custom field
        $order->save();
    }
    

}

Update I have changed $orders for $order, but that still does not product a change in the custom field in the order.

When I try update_post_meta($order_id, 'log', 'this is a test' );

That also doesn't update the custom field, so it seems there is something wrong in the posts query or I need another hook here.

I also tried replacing

$args = array(
    'customer_id' => $user_id
);
$orders = wc_get_orders($args);

with

$orders = wc_get_orders( array(
    'type' => 'shop_order', //important
    'numberposts' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    'meta_key' => '_customer_user',
    'meta_value' => $user_id
     
) );

and no dice.

Mandm
  • 1
  • 4

1 Answers1

1

I revised your code. try the below code.

Replace

foreach ( $order as $orders ) {     
    $order_id = $order->get_id(); 
    update_post_meta($order_id, 'log', $pkg ); // add and save the custom field
}

With this

foreach ( $orders as $order ) {     
    $order_id = $order->get_id(); 
    update_post_meta($order_id, 'log', $pkg ); // add and save the custom field
}

Complete code

function sync_pkg( $user_id ) { 
    // On profile update GET pkg_code value save to variable $pkg

    $pkg = get_user_meta( $user_id, 'pkg_code', true );
    
    // with pkg value set, fetch orders belonging to the customer and update the order with the pkg code value
    
    $args = array(
        'customer_id' => $user_id
    );
   
   $orders = wc_get_orders($args);

    foreach ( $orders as $order ) {     
        $order_id = $order->get_id(); 
        update_post_meta($order_id, 'log', $pkg ); // add and save the custom field
    }
    

}
add_action ( 'profile_update', 'sync_pkg', 11, 2 );
Bhautik
  • 11,125
  • 3
  • 16
  • 38
  • Thank you, I do see that was an error. However after updating that the code still doesn't work. – Mandm Feb 27 '22 at 22:22
  • @Mandm Working fine for me. – Bhautik Feb 28 '22 at 05:11
  • So how exactly is it working fine for you? This is a space for sharing knowledge, so additional information would be helpful. I had tested $orders as $order and $order as $orders both before I posted here, and neither worked. Your fix really doesn't address the problem. – Mandm Feb 28 '22 at 16:36
  • I have already pointed out the error in the code. That's it. The rest of the code is ok. I have tested your code in my local and working as aspected. – Bhautik Feb 28 '22 at 17:16
  • Thanks but, as elucidated at length, the rest of the code is not ok. Thanks anyway. – Mandm Feb 28 '22 at 17:18
  • have you debugged? – Bhautik Mar 01 '22 at 04:05
  • Yep. Nothing. Also deactivated all plugins but for woo and tested, still no results. – Mandm Mar 01 '22 at 18:07
  • hmm, I can check but i need access. – Bhautik Mar 01 '22 at 18:09