【问题标题】:Display magento products by category ID按类别 ID 显示 magento 产品
【发布时间】:2013-10-21 19:57:04
【问题描述】:

我需要知道如何在一个页面中显示产品,例如(购物车,低于总数)仅按 ID 显示的少数产品。例如:id 为 2、3、4 和 5 的产品。

<div class="freeProducts voucher code">
    <?php

    $categoryid = 64;

    $category = new Mage_Catalog_Model_Category();
    $category->load($categoryid);
    $collection = $category->getProductCollection();
    $collection->addAttributeToSelect('*');

    foreach ($collection as $_product) { ?>

    <a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a> <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>

    <?php } ?>
</div>

此时我可以看到每个产品的图片和标题。我需要显示添加到购物车和价格。

有人可以帮忙吗?

【问题讨论】:

  • 到目前为止你尝试了什么?你能给我们看一些代码吗?
  • 你有什么问题?
  • @admdrew 我需要显示特定类别的所有产品。在第一次尝试时,我开始寻找如何按 ID 显示产品,但我意识到将产品添加到一个类别中然后显示该类别中的所有产品是很好的。我添加了我的代码,我需要一些信息

标签: magento e-commerce php mage


【解决方案1】:

获取特定类别的产品

$categoryIds = array(2,4);//category id

$collection = Mage::getModel('catalog/product')
                             ->getCollection()
                             ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                             ->addAttributeToSelect('*')
                             ->addAttributeToFilter('category_id', array('in' => $categoryIds))

获取特定产品 ID 的产品

$productids = array(52,62);//product ids
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addFieldToFilter('entity_id',array( 'in' => $productids));

然后用 phtml 写这个

<?php $_collectionSize = $collection->count() ?>
    <?php //$_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($collection as $product): ?>
        <?php if ($i++%4==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; ?>">
                <a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>">
                        <img src="<?php echo Mage::helper('catalog/image')->init($product, 'small_image')->resize(197, 167); ?>" alt="<?php echo $product->getName()?>" border="0" />
                    </a>
                <h2 class="product-name"><a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>"><?php echo $product->getName() ?></a></h2>
               <div class="price-box">
               <?php echo Mage::helper('core')->currency($product->getPrice(),true,false);?>
               </div>
                <div class="actions">
                    <?php if($product->isSaleable()): ?>
                        <button class="button" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add/')?>product/<?php echo $product->getId() ?>/')" title="<?php echo $this->__('Köp');?>" type="submit"><span><span><?php echo $this->__('Köp');?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>

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

希望对你有帮助

【讨论】:

  • 您好!您提供的代码对我帮助很大,但我有一个问题。仅列出了 12 个产品(对于 2 个类别数组),如果数组中只有 1 个类别 id,则它不会列出任何内容。你有什么想法吗?
【解决方案2】:
// print_r($productslist)
 $category_id = 14; // if you know static category then enter number

$catagory_model = Mage::getModel('catalog/category')->load($category_id); 

$collection = Mage::getResourceModel('catalog/product_collection');

$collection->addCategoryFilter($catagory_model); //category filter

$collection->addAttributeToFilter('status',1); //only enabled product

$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched

//$collection->getSelect()->order('rand()'); //uncomment to get products in random order     

$collection->addStoreFilter();          

if(!empty($collection))

{

        foreach ($collection as $_product):?>

        <a href="<?php echo $_product->getProductUrl();?>"><img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(197, 167); ?>" />   </a>  

 <?php   endforeach;

}else

    {

        echo 'No products exists';

}              

【讨论】:

    【解决方案3】:
    $categoryid = 123;
    $_category = Mage::getModel('catalog/category')->load($categoryid);
    $productCollection = Mage::getResourceModel('catalog/product_collection')
    ->addCategoryFilter($_category);
    

    【讨论】:

      【解决方案4】:
          <?php 
       require "../app/Mage.php";
       Mage::app();
          ?>
      
      
        <table>
        <?php 
       $ris_arr = array(39,77,78,79);
       foreach ($ris_arr as &$value) { 
        ?>
       <tr><td style="background-color:#foo;margin-top:50px;">
       <?php 
      $ris_value = $value;
       $category_id = $ris_value; // if you know static category then enter number
      
      $catagory_model = Mage::getModel('catalog/category')->load($category_id); 
      
        $collection = Mage::getResourceModel('catalog/product_collection')->setPageSize(4);;
      
         $collection->addCategoryFilter($catagory_model); //category filter
      
         $collection->addAttributeToFilter('status',1); //only enabled product
      
         $collection->addAttributeToSelect(array('name','url','small_image','price','sku')); //add product attribute to be fetched
      
           //$collection->getSelect()->order('rand()'); //uncomment to get products in random order     
      
          $collection->addStoreFilter();          
      
          if(!empty($collection))
      
          { ?>
         <div style="width:100%">
           <?php  foreach ($collection as $_product):?>
            <div style="width:200px;float:left">
           <?php $pro_url= $_product->getProductUrl(); 
        $pro_url2=str_replace("rishabh_daily_report_cron2.php/","",$pro_url);?>
         <a href="<?php echo $pro_url2;?>">
            <img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(197, 167); ?>" />   </a> 
               <?php 
            echo $_product->getName()."<br>";
        //echo $_product->getSku()."<br>";
        echo $_product->getSpecialPrice();
          ?>
              </div>
      
          <?php   endforeach; ?>
          </div>
           <?php
               }else
      
            {
      
          echo 'No products exists';
      
             }
             echo "</td></tr>";  
             } 
            ?>      
             <table>     
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-25
        • 2017-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多