Prestashop 1.7如果是其中一个产品类别,而不是category_default

I'm currently using

      {assign var="var1" value=false}
{assign var="idCategory" value=37}
{foreach from=$cart.products item=product}
    {if $product.id_category_default == $idCategory}
        {assign var="var1" value=true}
    {/if}
{/foreach}

with all of the products using said category as their main category. I however would like to use their final, aka. the deepest of the category tree, as their default category. Which in that case would force me to turn it into an array with all the different category ideas.

Thus, I much rather would just that the if statement would be true if any of the products categories equals the value.

In principle, you should separate presentation (HTML) from application/processing (PHP). I would suggest doing that computation at PHP level and passing the result to Smarty. I am completely unfamiliar with PrestaShop, so I am making assumptions about what $cart contains, but this generic PHP code should work:

<?php

const DESIRED_CATEGORY = 37;

$cart = [
  // list some products falling into two categories, 1 and 37
  'products' => [
    'apples' => [ 'id_category_default' => 1 ],
    'oranges' => [ 'id_category_default' => 1 ],
    'bananas' => [ 'id_category_default' => 1 ],
    'cheese' => [ 'id_category_default' => 37 ],
    'yoghurt' => [ 'id_category_default' => 37 ],
    'butter' => [ 'id_category_default' => 37 ],
  ],
];

// this returns [ 1, 1, 1, 37, 37, 37 ];
$categories = array_column($cart['products'], 'id_category_default');

// this will be true if and only if at least one product is category 37
$hasCategory = in_array(DESIRED_CATEGORY, $categories);

$smarty->assign('hasCategory37', $hasCategory);

It is not recommended to use this condition in every pages.

True way:

You have to create new module to do it using PrestaShop hooks and cookies.In this way, Condition is reviewed again only if cart is changed.