Support » Plugin: WooCommerce » Get user ID when order_completed

  • Resolved martydev


    Hi, I write function:

    add_action( 'woocommerce_order_status_completed', 'order_completed');
    function order_completed($order_id) {
    		$user_id = $order->get_user_id(); // or $order->get_customer_id();
    		$order = wc_get_order( $order_id );
    		$items = $order->get_items();
    		foreach ( $items as $item ) {
    			$product_name = $item->get_name();			
    		 switch ($product_name) {
    		case 'FREE':		
    		  update_user_meta($user_id, 'ads-available',  get_user_meta( $user_id, 'ads-available', true ) +1 );
    		case 'VIP':
    		  update_user_meta($user_id, 'ads-available',  get_user_meta( $user_id, 'ads-available', true ) +3 );
    		  update_user_meta($user_id, 'karusel',  get_user_meta( $user_id, 'karusel', true ) +1 );

    And when I change status order from dashboard I have error:

    Type error: E_ERROR in row 102 file /var/www/ /data/www/  Uncaught Error: Call to a member function get_user_id() on null in /var/www/ /data/www/
    Stack trace:
    #0 /var/www/ /data/www/ order_completed(862)
    #1 /var/www/ /data/www/ WP_Hook->apply_filters('', Array)
    #2 /var/www/ /data/www/ WP_Hook->do_action(Array)
    #3 /var/www/ /data/www/ do_action('woocommerce_ord...', 862, Object(Automattic\WooCommerce\Admin\Overrides\Order))
    #4 /var/www/ /data/www/ WC_Order->status_transition()
    #5 /var/www/ /data/www/ WC_Order->save()
    #6 /var/www/ /data/www/
    row 102 it is -  $user_id = $order->get_user_id(); // or $order->get_customer_id();

    why it happened?


Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support fevered – a11n


    Hey there!

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Thread Starter martydev


    omg, wrong order:

    $user_id = $order->get_user_id();
    $order = wc_get_order( $order_id );

    $order = wc_get_order( $order_id );
    $user_id = $order->get_user_id();


Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.