1

我试图显示特定类别中产品的缺货状态,同时完全隐藏所有其他类别的状态。

目前,我已禁用从库存中显示库存状态。因此,任何缺货的商品都会自动隐藏。

现在状态:

  1. 所有库存变为 0 的产品都将自动隐藏。
  2. 当产品在库存中时,它仍然不显示其状态。
  3. 所有产品都是可配置产品。

我想要的是:

  1. 已创建新类别并向其中添加产品。此类别仅添加简单产品
  2. 即使库存为 0,我也想继续展示此产品
  3. 我想将状态显示为缺货
  4. 我希望该产品不能添加到购物车中。(这应该自动成为可能,因为产品将缺货。

可能的前进方向:

如我所见,我应该启用从库存中显示库存状态。通过这样做,库存变为 0 后产品仍然可见....但这将涵盖所有类别..我只希望对 1 个类别执行此操作???

4

4 回答 4

1

试试这个解决方案。这可能会给你更好的主意。

<?php
if ($cat_id = '12')
{
    $collections = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    )
    ->addAttributeToFilter('qty', array('eq' => 0));

    foreach ($collections as $collection) {
    echo $collection->getData()->getName();
    }
}
?>
于 2012-09-13T08:48:24.673 回答
0

一种简单的方法是使用 Magento 为每个类别定制布局的能力。

1)设置商店以展示产品,即使缺货

2)创建一个新主题,它将覆盖您正在使用的基础 - 它只需要包含目录/产品/list.phtml的副本

3) 修改原主题的 list.phtml 只显示项目:

<?php foreach ($_productCollection as $_product): ?>
    <?php if( ! $_product->isInStock()) continue ?>
...

现在您将有两个主题,您正在使用的基本主题,然后是一个新的覆盖主题,其中只有一个文件。

现在将新主题应用于您希望显示缺货商品的类别,默认情况下,原始主题的列表视图将跳过缺货产品。

使用块覆盖可以做得更好,我很乐意解释 - 但这是我能想到的最简单的解决方案:-)

于 2012-09-13T10:58:34.547 回答
0

这是基于安德鲁的回答。但是, isInStock() 似乎总是返回 true。所以我用 isSaleable() 替换它,如下所示:

<?php foreach ($_productCollection as $_product): ?>
   <?php if( ! $_product->isSaleable()) continue ?>
...

但是,答案并不是最好的,因为您仍会将产品计入显示的项目总数中。

于 2013-03-04T22:08:54.143 回答
0

如果猫等于给定数字,我已经修改了 list.phtml 以生成查询。使用查询构建网格/列表

<?php
$_layer = Mage::getSingleton('catalog/layer');
$_category = $_layer->getCurrentCategory();
$_currentCategoryId= $_category->getId();
if($_currentCategoryId == 11):
?>
<div class="category-products">
    <?php  $stockCollection = Mage::getModel('cataloginventory/stock_item')->getCollection()
        ->addFieldToFilter('is_in_stock', 0);

    $productIds = array();

    foreach ($stockCollection as $item) {
        $productIds[] = $item->getOrigData('product_id');
    }
    $categoryId = 11;
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $productCollection = Mage::getModel('catalog/product')->getCollection()
        ->addIdFilter($productIds)
        ->addCategoryFilter($category)
        ->addAttributeToSelect('*')
        ->addAttributeToSort('created_at', 'desc');

    $products = array();?>



 <div class="category-products">
    <?php echo $this->getToolbarHtml() ?>
    <?php // List mode ?>
    <?php if($this->getMode()!='grid'): ?>
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
    <?php foreach ($productCollection as $_product): ?>
        <li class="item<?php if( ++$_iterator == sizeof($productCollection) ): ?> last<?php endif; ?>">
            <?php // Product Image ?>
            <p class="product-image">
                <a href="<?php echo $this->getUrl('ajax/product/quickview/id/' . $_product->getId()) ?>" rel="nofollow" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="ajax"><?php echo $this->__('Quick view') ?></a>
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>                
            </p>
            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
                    <?php if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product) ?>
                    <?php endif; ?>
                    <?php echo $this->getPriceHtml($_product, true) ?>
                    <?php if($_product->isSaleable()): ?>
                        <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <div class="clear"></div>
                    <div class="desc std">
                        <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                        <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a>
                    </div>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </div>
            </div>
        </li>
    <?php endforeach; ?>
    </ol>
    <script type="text/javascript">decorateList('products-list', 'none-recursive')</script>

    <?php else: ?>

    <?php // Grid Mode ?>

    <?php $_collectionSize = $productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <p class="product-image">
                    <a href="<?php echo $this->getUrl('ajax/product/quickview/id/' . $_product->getId()) ?>" rel="nofollow" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="ajax"><?php echo $this->__('Quick view') ?></a>
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
                            <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
                    </a>
                </p>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </div>
            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    <?php endif; ?>

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


<?php elseif(!$_productCollection->count()):
?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>

<?php else: ?>
<div class="category-products">
    <?php echo $this->getToolbarHtml() ?>
于 2014-01-12T17:17:50.553 回答