0

I have an array in my wordpress template where i want to then target a specific status of the logged in user. Below is what i currently have with the $args containing a status with all the status values stored.

$user_id = get_current_user_id();
$args = array( 
    'status' => array( 'wcm-active', 'wcm-delayed', 'wcm-complimentary', 'wcm-pending', 'wcm-paused', 'wcm-expired', 'wcm-cancelled' ),
);  
$active_memberships = wc_memberships_get_user_memberships( $user_id, $args );

I then want to create some if statements in my theme template that targets one of the specific user statuses (e.g. if user status is delayed, show this content. Below is what i have but it doesnt seem to be correct, so i think i have something wrong when targeting the key:

if ($active_memberships = wc_memberships_get_user_memberships( $user_id, $args[0][1] )) {
   show content
}
1

The multidimensional array in this case is a multi-dimensional associative array, or a key-value pair. The key being 'status' and the value being your array of strings.

In the conditional check you have attempted to access values by a numerical index, which in this case do not exist as nothing has been set to use those indexes. To access the values set above you would do something like this: $args['status'][1];

Associative arrays and Arrays are treated as the same type in php, the only difference being that associative array keys that are strings would be accessed that in mind. As such there are built in php functions to 'iterate' over associative arrays, such as foreach, which may give the impression that they are distinctly different beasts when they are not.

For more information check out the php docs here: http://php.net/manual/en/language.types.array.php And specifically this example: http://php.net/manual/en/language.types.array.php#example-61

Update

All that being said, in the code above it seems like it's failing due to an issue with the conditional checks.

When the if statement is executed, it is re-assigning the variable to the result of the function to the right of the equals sign. So it is setting $active_memberships to be equal to the result of wc_memberships_get_user_memberships() function. If the result of the function is not false or NULL, or some other falsey value, it will pass the check while setting $active_memberships to the new value. By setting this to a new value it is kind of destroying the purpose of that variable, as I imagine you want to use it to store all the memberships of the user and then check for each membership later to show particular content or do something else in code.

So it may be that you are intending to just set $active_memberships up once with your args, then test for the presence of any memberships and then any particular memberships. For example:

if (!empty($active_memberships)) {
   // show content if the current user has any memberships

   if ( in_array('wcm-active', $active_memberships) ) {
       // show content if 'wcm-active' is present in the active_memberships
   }

   if ( in_array('wcm-delayed', $active_memberships) ) {
       // show content if 'wcm-delayed' is present in the active_memberships
   }

}

It's up to you to decide how best to structure these controls, but this should get you progressing a bit further!

So there's some learning in this post. How to access associative array values and a bit about conditional statements - that you can set variables during the evaluation of a conditional statement. You can do this in if statements much in the same way a for loop would set a variable on each iteration. This way of setting a variable is often unclear to a person reading such code though, so I would tend to avoid it.

You can read more on php's control structures here: http://php.net/manual/en/language.control-structures.php

And comparison operators here: http://php.net/manual/en/language.operators.comparison.php

  • Thanks for the reply. Im still having issues unfortunately though. Ive used your logic & done something like: if ($active_memberships = wc_memberships_get_user_memberships( $user_id, $args['status']['wcm-active'] )) { echo 'Active'; } if ($active_memberships = wc_memberships_get_user_memberships( $user_id, $args['status']['wcm-delayed'] )) { echo 'Delayed'; } However its outputting both statements even though the status should only show Delayed. Iv var_dumped the variable and its showing the below, so not sure what im missing: ["status"]=> string(11) "wcm-delayed" – Futwill Dec 5 '16 at 16:36
  • The conditional statement is actually setting a variable not checking it. I'll edit my answer to include that – DarrenK Dec 5 '16 at 16:59
  • Hi @DarrenK , sorry im still having issues. Am i not right in saying that with your code above, it is setting varible $active_memberships just to the string 'wcm-delayed'? I have tried your code and var_dumped the variable and its just outputting that text. I would have thought that your original code that you provided was correct (but with the double equals rather than single) would work, but obviously im not the expert (hence asking on here). Sorry to keep pestering you... This one is becoming a bit of a pain! Thanks – Futwill Dec 5 '16 at 17:34
  • I guess I didn't clearly understand your question! The way that you are checking it is the thing that needs to be changed. I'm guess you want to see if the current user has any of these memberships, which are returned as an array into $active_memberships. Then all you need to do is check to see if it is set, or check to see if there is any particular value present in the array. Updating answer to reflect this. – DarrenK Dec 6 '16 at 9:50
  • Iv finally found a solution, using a bit of your previous notes. Iv simplified the $args array so for each conditional statement i only include the one status, rather than storing all of them in another array. Below is what i have: $user_id = get_current_user_id(); $args = array( 'status' => 'wcm-active' ); $active_memberships = wc_memberships_get_user_memberships( $user_id, $args ); if ($active_memberships == wc_memberships_get_user_memberships( $user_id, $args['status']['wcm-active'] )) { echo 'This content only shows for active statuses'; } Thanks again for your help – Futwill Dec 6 '16 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.