• Resolved Norm Sash

    (@normsash)


    Hi,
    I’m using WP Fusion Lite + WC + AC. I would like to re-sync the user tags whenever a WC order status is changed to “completed”. This needs to work on both the front-end processing when a user completes payment, or from the WC admin area with an administrator changing the order status to ‘completed’.

    So I have the below code snippet which I think is close and should work on both backend and frontend, but I just can’t quite get it working and I’m wondering if someone could give me a hint as to what is wrong.

    Logic: Get the $order object using the $order_id, and then extract the order’s $user_id. Then call wp_fusion get_tags method for the user.

    function func_refresh_ac_tags( $order_id) {
      $order = wc_get_order( $order_id );
      $user_id = $order->get_user_id();
    
      if( $user_id ){
    	$tags = wp_fusion()->user->get_tags( $user_id, true );
      }
    }
    add_action( 'woocommerce_payment_complete', 'func_refresh_ac_tags' );

    Thanks,
    -Norm

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author verygoodplugins

    (@verygoodplugins)

    Hi @normsash ,

    Close. I’ve found that woocommerce_payment_complete isn’t the most reliable. What I would do is

    function func_refresh_ac_tags( $order_id) {
      $order = wc_get_order( $order_id );
      $user_id = $order->get_user_id();
    
      if( $user_id ){
    	$tags = wp_fusion()->user->get_tags( $user_id, true );
      }
    }
    
    add_action( 'woocommerce_order_status_processing', 'func_refresh_ac_tags' );
    add_action( 'woocommerce_order_status_completed', 'func_refresh_ac_tags' );

    That should run both after the order is initially placed, and when it’s manually marked Completed.

    Thread Starter Norm Sash

    (@normsash)

    Awesome! That seemed to resolve the issue for me. I actually am testing with ‘woocommerce_order_status_changed’ to hook into. It’s possible that hook might fire unnecessary API calls, but so far it seems to be working good for my purposes.

    Plugin Author verygoodplugins

    (@verygoodplugins)

    That works as well.

    What we do in the full version of WP Fusion is

    update_post_meta( $order_id, 'wpf_complete', true )

    and then check that value before proceeding with the API calls.

    That way the order is locked and it can’t be sent a second time if for some reason the status gets toggled manually or by another plugin.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to sync AC tags when Woocommerce order status is complete’ is closed to new replies.