[英]Magento how to show out of stock in ONE category only while hiding them for all other categories?
我正在尝试显示特定类别产品的缺货状态,同时完全隐藏所有其他类别的状态。
目前,我已禁用从库存显示库存状态。 因此,所有缺货的商品都会自动隐藏。
现在状态:
我想要的是:
可能的前进方向:
如我所见,我应该启用从库存显示库存状态。 这样,产品在库存变为0后仍将可见。... 但这将适用于所有类别 ..我只希望对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();
}
}
?>
一种简单的方法是使用Magento的功能为每个类别创建自定义布局。
1)设置商店以显示即使没有库存的产品
2)创建一个新主题,它将覆盖您正在使用的主题-它只需要包含catalog / product / list.phtml的副本
3)修改原始主题的list.phtml以仅显示项目:
<?php foreach ($_productCollection as $_product): ?>
<?php if( ! $_product->isInStock()) continue ?>
...
现在您将有两个主题,您正在使用的主题是基础主题,然后是一个新的覆盖主题,其中仅包含单个文件。
现在,将新主题应用于希望显示缺货项目的类别,默认情况下,原始主题的列表视图将跳过产品缺货的情况。
可以使用“块覆盖”更好地完成此操作,我很乐于解释-但这是我能想到的最简单的解决方案:-)
这是基于安德鲁的答案。 但是,isInStock()似乎总是返回true。 因此,我用isSaleable()替换了它,如下所示:
<?php foreach ($_productCollection as $_product): ?>
<?php if( ! $_product->isSaleable()) continue ?>
...
答案并不是最好的,因为您仍将产品计入显示的总数中。
我已经修改了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() ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.