duanhuantong8278 2019-02-23 04:10
浏览 47

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.

  • 写回答

2条回答 默认 最新

  • douduoting8408 2019-02-23 17:13
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序