Blade foreach循环输出商品筛选属性时如何移除颜色筛选及对应值
实现方法
直接在外层属性遍历循环的起始位置加跳过逻辑,匹配到颜色属性时终止当前循环轮次,即可完全隐藏整个颜色筛选模块及其下属所有选项,不会影响其他筛选属性的正常渲染。
推荐优先使用属性ID做匹配,不会受多语言翻译、属性名称修改的影响,稳定性更高;如果不方便查属性ID,也可以用属性名称做匹配。
- 按属性ID匹配(推荐):找到后台商品属性中颜色项对应的ID,替换判断语句中的ID值即可
- 按属性名称匹配:直接匹配属性的展示名称,需要保证判断值和后台配置的颜色属性名完全一致,多语言站点需要补充对应语言的名称判断
修改后的完整模板代码如下:
@foreach ($attributes as $attribute) {{-- 按ID跳过颜色属性:替换下面的数字为你站点颜色属性的实际ID --}} @if($attribute->id === 3) @continue @endif {{-- 若要按名称匹配,注释掉上面的ID判断,启用下面这行即可 --}} {{-- @if(mb_strtolower($attribute->getTranslation('name')) === 'color') @continue @endif --}} <div class="bg-white shadow-sm rounded mb-3"> <div class="fs-15 fw-600 p-3 border-bottom"> {{ translate('Filter by') }} {{ $attribute->getTranslation('name') }} </div> <div class="p-3"> <div class="aiz-checkbox-list others-attributes" id="attribute_list-{{$attribute->id}}" > @foreach ($attribute->attribute_values as $attribute_value) <label class="aiz-checkbox"> <input type="checkbox" name="selected_attribute_values[]" value="{{ $attribute_value->value }}" @if (in_array($attribute_value->value, $selected_attribute_values)) checked @endif onchange="filter()" > <span class="aiz-square-check"></span> <span>{{ $attribute_value->value }}</span> </label> @endforeach </div> <div class="ViewMore" id="ViewMore-{{$attribute->id}}" onclick="ViewMore({{$attribute->id}})">View More</div> <div class="ViewLess" id="ViewLess-{{$attribute->id}}" onclick="ViewLess({{$attribute->id}})">View Less</div> </div> </div> @endforeach
补充说明:如果使用名称匹配,你的站点是中文后台的话,把判断里的
color改成颜色即可,多语言站点可以用判断逻辑拼接多个语言的名称条件,确保所有语言环境下颜色模块都能正常隐藏。
内容的提问来源于stack exchange,提问作者Nurul Islam
相关产品推荐
相关产品推荐

