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

问题描述 投票:0回答:2

我正在使用

      {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}

所有产品都使用所述类别作为主要类别。但是我想用他们的决赛,又名。类别树中最深的,作为其默认类别。在这种情况下会迫使我把它变成一个包含所有不同类别想法的数组。

因此,如果任何产品类别等于该值,我更倾向于if语句为真。

php prestashop smarty prestashop-1.7
2个回答
0
投票

原则上,您应该将表示(HTML)与应用程序/处理(PHP)分开。我建议在PHP级别进行计算并将结果传递给Smarty。我对PrestaShop完全不熟悉,所以我假设$cart包含什么,但这个通用的PHP代码应该有效:

<?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);

0
投票

不建议在每个页面中使用此条件。

真实的方式:

您必须使用PrestaShop挂钩和cookie创建新模块才能执行此操作。这样,只有在更改购物车时才会再次审核Condition。

© www.soinside.com 2019 - 2024. All rights reserved.