按类别 ID 显示 magento 产品

2021-12-19 00:00:00 e-commerce php magento mage

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

<?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 } ?>

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

有人可以帮忙吗?

解决方案

从特定类别获取产品

$categoryIds = array(2,4);//类别id$collection = Mage::getModel('目录/产品')->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);//产品id$collection = Mage::getResourceModel('catalog/product_collection');$collection->addFieldToFilter('entity_id',array('in' => $productids));

然后在 phtml 中写这个

count() ?><?php//$_columnCount = $this->getColumnCount();?><?php $i=0;foreach ($collection 作为 $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()?>"边框="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 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 其他:?><p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p><?php endif;?>

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

希望对你有帮助

I need to know how can I display products in a page like (cart, below total) only few products by ID. Eg: products with id 2,3,4 and 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>

At this moment I can see the image for each product, and the title. I need to display the ADD TO CART and price.

Anyone can help please?

解决方案

get Product from specific category

$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))

get Product for specific product id

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

then write this in 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 ?>

hope this help you

相关文章