1

我正在开发一个页面,显示所有具有特价的产品。

如果我有一个“正常”的设置,这会很容易,但我的商店只显示分组产品。所有简单产品都只是与分组产品相关联的 SKU。分组产品包含描述、产品名称等所有一般信息,简单产品包含 SKU、价格、增值税等信息。

考虑到这一点,我想我会拉出所有有特价的简单产品并加载它们的“父”分组产品。

这可行,但出现了一个新问题:分组产品与多个特价 SKU 相关联。这意味着,该产品将在页面上显示两次、三次甚至更多。考虑到我的代码,这是有道理的:

<?php $storeId = Mage::app()->getStore()->getStoreId(); ?>

<?php
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$special= Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', array('neq'=>1))
    ->addAttributeToFilter('special_price', array('neq'=>''))
    ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
    ->addAttributeToFilter('special_to_date', array('or'=> array(
    0 => array('date' => true, 'from' => $todayDate),
    1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
    ->addAttributeToSort('special_from_date', 'desc');
$special->load();
?>

<div class="cat-misc">
    <?php $_columnCount = 4;?>

    <?php foreach ($special as $_product): ?>
    <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
    <?php endif ?>

        <?php $parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($_product->getId()); ?>
        <?php if(isset($parentIds[0])):?>
            <?php foreach ($parentIds as $parentId): ?>
                <?php $parent = Mage::getModel('catalog/product')->load($parentId); ?>
                <?php if ($parent->getStoreId() == $storeId): ?>
                    <?php $_product = Mage::getModel('catalog/product')->load($parentId);?>
                    <?php endif; ?>
                <?php endforeach; ?>
        <?php endif; ?>

    <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">

        <div class="produtos_wrapper">
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(158, 158); ?>" width="158" height="158" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a>

            <?php
            $newFromDate = Mage::getModel('catalog/product')->load($_product->getID())->getNewsFromDate();
            $newToDate = Mage::getModel('catalog/product')->load($_product->getID())->getNewsToDate();

            $now = date("Y-m-d H:m:s");

            if($newFromDate < $now && $newToDate > $now) {
                echo "<div id='simbolo_novo' class='simbolo_novo'></div>";
            }
            ?>

            <div class="titulos_wrapper">
                <p class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a><br /><span class="subtitulo"><?php echo nl2br($_product->getShortDescription()); ?></span> </p>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
            </div>
        </div>


        <div class="actions">
            <?php if($_product->isSaleable()): ?>

            <?php else: ?>
            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
            <?php endif; ?>

            <?php if ($this->helper('wishlist')->isAllow()) : ?>

            <?php endif; ?>
            <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>

            <?php endif; ?>
            </ul-->
        </div>
    </li>
    <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>

    <?php endforeach; ?>

    <div class="toolbar-bottom">
        <?php echo $this->getToolbarHtml() ?>
    </div>
</div>

我的问题是,我如何限制这个脚本只显示一次分组产品?

4

1 回答 1

3

你可以这样做:

$shown_skus = array() ; //Create an array to trace already displayed products

foreach($products as $product){
  if (!in_array($product['sku'], $shown_skus, true)){ //If there is no sku in array
    $shown_skus[] = $product['sku'] ; //Add the sku to the array
    //And display the product as it has not been displayed yet.
  }
}

如果该解决方案适合您,请报告。

于 2013-04-11T14:40:15.957 回答