如何在主页magento中显示特色产品?

时间:2014-09-08 12:31:27

标签: magento magento-1.8

我想在主页中显示特色产品。为此,我在管理员中创建了一个属性“特色产品”,并将特色产品设置为“是”。 然后我在app/code/local/MyCompany/Catalog/Block/Product/Featured.php

中创建了一个新文件
<?php
class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
    public function getFeaturedProducts() {
        $resource = Mage::getSingleton('core/resource');
        $read = $resource->getConnection('catalog_read');
        $productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
        $eavAttributeTable = $resource->getTableName('eav/attribute');
        $categoryProductTable = $resource->getTableName('catalog/category_product');

        $select = $read->select()
                        ->distinct(true)
                        ->from(array('cp'=>$categoryProductTable), 'product_id')
                        ->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
                        ->joinNatural(array('ea'=>$eavAttributeTable))
                        ->where('pei.value=1')
                        ->where('ea.attribute_code="featured"');

        $res = $read->fetchAll($select);

        return $res;
    }
}
?>

然后我在

中创建了一个新文件
app/code/local/MyCompany/Page/Block/Html.php

<?php

class MyCompany_Mage_Page_Block_Html extends Mage_Page_Block_Html
{
    public function getFeaturedProductHtml()
    {
        return $this->getBlockHtml('product_featured');
    }
}

然后我在配置标记

下的app/etc/local.xml中添加了一个新块
<blocks>
    <catalog>
        <rewrite>
            <product_featured>MyCompany_Catalog_Block_Product_Featured</product_featured>
        </rewrite>
    </catalog>
</blocks>

然后我在app/design/frontend/default/mytheme/template/catalog/product/featured.phtml

中添加了一个新文件
<?php $featured_products = $this->getFeaturedProducts(); ?>
    <?php shuffle($featured_products); ?>
    <div class="box recently" style="padding-left:15px; padding-right:15px;">
        <h3><?php echo $this->__('Featured Products') ?></h3>
        <div class="listing-type-grid  catalog-listing">
            <?php $_collectionSize = count($featured_products) ?>
            <table cellspacing="0" class="recently-list" id="product-list-table">
                <?php 
                    $i=0; 
                    foreach ($featured_products as $_res): ?>
                        <?php $_product = Mage::getModel('catalog/product')->load($_res['product_id']); ?>
                        <?php if ($i++%3==0): ?><tr><?php endif ?>
                        <td>
                            <div>
                                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                                    <img class="product-image" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
                                </a>
                            </div>
                            <p>
                                <a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $this->htmlEscape($_product->getName()) ?></a>
                            </p>
                            <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                        </td>
                        <?php if ($i%3==0 && $i!=$_collectionSize): ?></tr><?php endif ?>
                    <?php endforeach ?>

                    <?php for($i;$i%3!=0;$i++): ?>
                        <td class="empty-product">&nbsp;</td>
                    <?php endfor ?>
                    <?php if ($i%3==0): ?>&nbsp;<?php endif ?>
            </table>
            <script type="text/javascript">decorateTable('product-list-table')</script>
        </div>
    </div>

然后我使用此代码在家庭cms页面中调用此页面

{{block type="catalog/product_featured" name="product_featured" as="product_featured" template="catalog/product/featured.phtml}}

但是在清除缓存后,产品未在主页中显示只显示Featured Products标题。我不知道我在哪里弄错了。 如果有人知道这一点,请帮助我。 谢谢!

0 个答案:

没有答案