【问题标题】:Magento how to show out of stock in ONE category only while hiding them for all other categories?Magento 如何仅在一个类别中显示缺货,而在所有其他类别中隐藏它们?
【发布时间】:2012-09-13 07:25:00
【问题描述】:

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

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

现在状态:

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

我想要什么:

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

可能的前进方向:

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

【问题讨论】:

标签: magento


【解决方案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();
    }
}
?>

【讨论】:

    【解决方案2】:

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

    1) 设置商店即使缺货也能展示产品

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

    3) 修改原主题的list.phtml,只显示item:

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

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

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

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

    【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      我已修改 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() ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-06
        • 2013-03-16
        • 1970-01-01
        相关资源
        最近更新 更多