• Resolved wilsonmike

    (@wilsonmike)


    Hello,

    The below script returns “No Match” when pulled out with shortcode to run daily on a page. I am unsure where the syntax error is, possibly when setting user roles?

    function user_info_echo() { 
    	$user_ID= get_current_user_id();
    	$users = new WP_User( $user_ID );
    	$role1='';
    	if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
        foreach ( $user->roles as $role )
            $role1= $role;
    }
    
    	
    echo '<li>' . 'First Name' . ' ' . 'Current Date' . ' | ' . 'Hire Date'  .' Shirt Qty</li>' . PHP_EOL;	
    foreach ($users as $user) {
    
    	$first_name = $user->user_firstname;
    	$last_name = $user->user_lastname; 
    	$hire_date = get_user_meta($user->ID, 'wpcf-hire-date', true);
    	$hire_date = date("m-d", strtotime($hire_date));
    	$shirt_quantity = get_user_meta($user->ID, 'wpcf-shirt-quantity', true);
    	$current_date = date("m-d");
    
    if($current_date==$hire_date && $role1 !=="warehouse")
    {
    	echo '<li>Match ' . $first_name . ' ' . $last_name . ' ' . $current_date . ' | ' . $hire_date . ' ' . $shirt_quantity . '</li>' . PHP_EOL;
    	$shirt_quantity_new = $shirt_quantity +1;
    	update_user_meta( $user->ID, 'wpcf-shirt-quantity', $shirt_quantity_new );
    } elseif($current_date==$hire_date && $role1=="warehouse") {
    	echo '<li>Match ' . $first_name . ' ' . $last_name . ' ' . $current_date . ' | ' . $hire_date . ' ' . $shirt_quantity . '</li>' . PHP_EOL;
    	$shirt_quantity_new = $shirt_quantity +3;
    	update_user_meta( $user->ID, 'wpcf-shirt-quantity', $shirt_quantity_new );
    } else {
    	echo '<li>No Match ' . $first_name . ' ' . $last_name . ' ' . $current_date . ' | ' . $hire_date . ' ' . $shirt_quantity . '</li>' . PHP_EOL;
    }	
    	
    	
    }
    
    	
    }
    // Register shortcode
    add_shortcode('echo_userinfo', 'user_info_echo');

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Unrelated to the no match issue which you’ve apparently resolved, but shortcode handler functions must never echo out anything! All output should be collected into a single return value. Echoing out can cause the output to end up in the wrong place on the page, even though it might work OK in some situations.

    silly_shortcode() {
      $output = 'Foo';
      $output .= 'bar';
      return $output;
    }

    WP will then output “Foobar” in place of the shortcode.

Viewing 1 replies (of 1 total)
  • The topic ‘WooCommerce Update Quantities on User Role’ is closed to new replies.