All Low add-ons are now owned by EEHarbor. Read the blog post.

Support archive

Assign variable depending on Member_ID

greendesigned 22 Apr 2010 21:51 question, complete

Just wondering if there is a method to achieve this with low variables?

I am trying to set a discount figure to CartThrob, depending on the group_id of the logged in member, so the discount % is defined as a variable, but then trying to set that as a variable within the EE template depending on the users member_id....

{exp:low_variables:parse}
{assign_variable:var_discount="
{if member_group == '8'}{member_discount_gold}
{if:elseif member_group == '7'}{member_discount_silver}
{if:elseif member_group == '6' }{member_discount_estate}
{if:else}0
{/if}
"}
{/exp:low_variables:parse}

When writing {var_discount} to the page as normal text, the variable discount shows. However when using in other EE tags those seem to break when that variable is used...

Replies

  1. Low 23 Apr 2010 13:15

    Ah, EE's template parsing order rears it's cheeky head again. Here's the order your snippet will be parsed, assuming the Low Variables you've created are the {member_discount_x} ones, and Early Parsing is turned off for those.

    - First, the {assign_variable} bit is handled, assigning the whole {if ... /if} block as a value to {var_discount}.
    - Then {var_discount} is replaced throughout your template with the said {if} block.
    - Next is regular Tag Processing. The Low Variables inside the {exp:low_variables:parse} tags are replaced. Others are ignored. Tag Processing also includes al other {exp:...} tags in the page, like the {exp:weblog:entries} tag.
    - When Tag Processing is done, Advanced Conditionals kick in. That means all {if ... if:else .../if} blocks are parsed. Note that this is done after the Tag Processing.
    - Finally, the regular Low Variables sans early parsing are replaced.

    As you can see, your approach won't work. What you need, is to get the appropriate {member_discount_x} var parsed, before the Tag Processing kicks in. I think you'll need to enable early parsing for those variables and use PHP on input in your template, with code like this:

    <?php
    global $IN, $SESS;

    $map = array(
    '6' => 'estate',
    '7' => 'silver',
    '8' => 'gold'
    );

    $var_discount = isset($map[$SESS->userdata['group_id']])
    ? $IN->global_vars['member_discount_'.$map[$SESS->userdata['group_id']]]
    : 0;
    ?>
    <!-- Template code -->
    {exp:module:method parameter="<?=$var_discount?>"}
    <!-- More template code -->


    Edit: GetSatisfaction is messing up my code a bit. Where it says '6' ?> 'estate', I mean '6' => 'estate'.

  2. greendesigned 23 Apr 2010 22:19

    Thanks Low, I spent the morning learning about the EE parsing order since posting this...

    The solution works perfectly!

    I appreciate the time you took to write that up.