Code Help




The code below is used to crate the 'Soil pH' underlined above, the problem is that there are more than one variable most of the time i.e. I would like to to say' Acid, Neutral, Alkaline rather than just the first value it comes across checked in the Product Features. This section was created by the web developer who built the site. I was hoping this is a quick for statement that someone can help me out with! I've tried myself but just cant get my head around it!

Thanks in advance (Cart v3.x)

{if $product.product_features.44.value != "" || $product.product_features.44.variant_id != ""}

  • Soil pH

    {if $product.product_features.44.value != ""} {$product.product_features.44.value|unescape} {else} {assign var="varid" value=$product.product_features.44.variant_id}

    {assign var="varlist" value=$product.product_features.44.variants}

    {$varlist.$varid.variant|unescape} {/if}

  • {/if}

    What is the type of this product feature?

    It's a Multipal Checkbox, is that what you mean?

    Can anyone give me some advice on this please?

    Try this code:

    {if $product.product_features.44.value != "" || $product.product_features.44.variant_id != ""}
    
  • Soil pH {if $product.product_features.44.value != ""} {$product.product_features.44.value|unescape} {else} {foreach from=$product.product_features.44.variants item=var name=vars} {$var.variant|unescape}{if !$smarty.foreach.vars.last}, {/if} {/foreach} {/if}
  • {/if}

    Thanks very much eComLabs, really appreciate that, I don't think I was far off getting it working myself on a couple of occasions but just couldn't quite get it!

    Nearly worked just has to add an extra if statement so it only picked up selected values rather than a full list of the variants.

    {if $product.product_features.4.value != "" || $product.product_features.4.variant_id != ""}
    
  • Soil Type {if $product.product_features.4.value != ""} {$product.product_features.4.value|unescape} {else} {foreach from=$product.product_features.4.variants item=var name=vars} {if $var.selected}{$var.variant|unescape}{if !$smarty.foreach.vars.last}, {/if}{/if} {/foreach} {/if}
  • {/if}

    One slight irritation is that this

    {if !$smarty.foreach.vars.last}, {/if}
    

    only works if its the last variable not the last selected variable... I'm not that clued up on smarty is it possible to get this to work for last selected variable rather than just the last option selected or not?

    Try this solution:

    {if $product.product_features.4.value != "" || $product.product_features.4.variant_id != ""}
    
  • Soil Type {if $product.product_features.4.value != ""} {$product.product_features.4.value|unescape} {else} {assign var="list_started" value=false} {foreach from=$product.product_features.4.variants item=var name=vars} {if $var.selected}{if $list_started}, {else}{assign var="list_started" value=true}{/if}{$var.variant|unescape}{/if} {/foreach} {/if}
  • {/if}

    That does the trick, got a rouge space before the comma but I can live with that!

    Thanks again

    OK. Use the {strip} tag in this case

    {if $product.product_features.4.value != "" || $product.product_features.4.variant_id != ""}
    
  • Soil Type {strip} {if $product.product_features.4.value != ""} {$product.product_features.4.value|unescape} {else} {assign var="list_started" value=false} {foreach from=$product.product_features.4.variants item=var name=vars} {if $var.selected}{if $list_started}, {else}{assign var="list_started" value=true}{/if}{$var.variant|unescape}{/if} {/foreach} {/if} {/strip}
  • {/if}