php - Magento 2.4.1 在产品列表页面上显示横幅
问题描述
我正在尝试在 magento 2.4.1 中的覆盖产品列表页面上显示一些横幅。横幅以与常规产品相同的大小显示在其位置( 页面示例)
我有我的自定义横幅集合。横幅集合:
- ID
- 内容
- 位置
横幅集合按位置排序。
我的 list.phtml:
<?php
$_productCollection = $block->getLoadedProductCollection();
$categoryId = $this->getLayer()->getCurrentCategory()->getId();
$_banners = $helper->getBanner($categoryId);
$banner_count = $_banners->getSize();
$banners = $_banners->getData();
$_page_limit = $block->getToolbarBlock()->getLimit();
?>
<?php foreach ($_productCollection as $_product): ?>
<?php $repeat = true; ?>
<?php while ($repeat): ?>
<?php if (
isset($banners[$banners_iterator]) &&
$banners[$banners_iterator]['position'] == $current_position &&
$current_position < $_page_limit
): ?>
<--- display banner with current position --->
<?php $banners_iterator++; ?>
<?php $current_position++; ?>
<?php else: ?>
<li class="item product product-item">
<---display products--->
</li>
<?php $current_position++; ?>
<?php $repeat = false; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endforeach; ?>
<?php $repeat = true; ?>
<?php while ($repeat): ?>
<?php if (
$banner_count > $banners_iterator &&
isset($banners[$banners_iterator]) &&
$current_position <= $_page_limit
): ?>
<--- display banners if they locate after all products --->
<?php $repeat = true; ?>
<?php $banners_iterator++; ?>
<?php $current_position++; ?>
<?php else: ?>
<?php $repeat = false; ?>
<?php endif; ?>
<?php endwhile; ?>
我从集合中选择数据,它运行良好。但我需要使用集合及其方法(->getContent、->getPosition 等)。而且我不应该在循环中使用 foreach 循环,因为它会减慢页面打开速度。
编辑:我想使用集合而不是数组在产品之间显示这些横幅,并且不使用两个 foreach 循环。第一个产品是默认设置,第二个是我的横幅。例如如何不工作:
foreach($product_collection as product){
foreach($banner_collection as banner){ }
}
我将不胜感激任何想法。
解决方案
推荐阅读
- sql - 将格式错误的 ISO8601 格式的 varchar 转换为时间戳时出现问题
- amazon-web-services - AWS Textract 是否支持 png 文件中的印地语文本?
- windows-7 - 自定义向导页面上控件的颜色在 Windows 7 上不正确 - Parallels (Inno Script)
- node.js - 如何根据日期字段从 MongoDb 中搜索记录
- sql - 为什么不能更改表主键 - Crate DB
- apache-spark - 如何从火花数据框中的字符串值创建表达式或条件
- php - 在 foreach 循环之外访问对象属性
- rust - 如何防止“提前输入”
- javascript - 如何处理选择输入异步 React Js?材质 UI:您提供了超出范围的值
- python - 如何在 FastText 中打印分类报告?