Support » Plugin: WooCommerce » Get user ID when order_completed

  • Resolved martydev

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

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

    Type error: E_ERROR in row 102 file /var/www/ /data/www/web-design.com/wp-content/plugins/functions.php.  Uncaught Error: Call to a member function get_user_id() on null in /var/www/ /data/www/web-design.com/wp-content/plugins/functions.php:102
    Stack trace:
    #0 /var/www/ /data/www/web-design.com/wp-includes/class-wp-hook.php(294): order_completed(862)
    #1 /var/www/ /data/www/web-design.com/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters('', Array)
    #2 /var/www/ /data/www/web-design.com/wp-includes/plugin.php(484): WP_Hook->do_action(Array)
    #3 /var/www/ /data/www/web-design.com/wp-content/plugins/woocommerce/includes/class-wc-order.php(363): do_action('woocommerce_ord...', 862, Object(Automattic\WooCommerce\Admin\Overrides\Order))
    #4 /var/www/ /data/www/web-design.com/wp-content/plugins/woocommerce/includes/class-wc-order.php(221): WC_Order->status_transition()
    #5 /var/www/ /data/www/web-design.com/wp-content/plugins/woocommerce/includes/class-wc-order.php(333): WC_Order->save()
    #6 /var/www/ /data/www/web-design.com/wp-content/plugins/woocommerce/includes/admin/list-tables/class-wc-admin-li
    
    row 102 it is -  $user_id = $order->get_user_id(); // or $order->get_customer_id();

    why it happened?

    thx

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

    (@fevered)

    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

    (@martydev)

    omg, wrong order:

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

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

    thanks

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