如何避免使用此脚本重复产品(Magento)

时间:2013-04-11 14:36:49

标签: php magento zend-framework

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

如果我有“正常”设置,这很容易,但我的商店只显示分组产品。所有简单产品仅仅是与分组产品相关的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>

我的问题是,如何限制此脚本仅显示一次Grouped Product?

1 个答案:

答案 0 :(得分: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.
  }
}

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