首页 > 解决方案 > Magento 2.4.1 在产品列表页面上显示横幅

问题描述

我正在尝试在 magento 2.4.1 中的覆盖产品列表页面上显示一些横幅。横幅以与常规产品相同的大小显示在其位置( 页面示例

我有我的自定义横幅集合。横幅集合:

横幅集合按位置排序。

我的 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){ } 
} 

我将不胜感激任何想法。

标签: phpmagento2

解决方案


推荐阅读