Magento电子商务 - 如何在CMS页面中调用和显示产品的详细信息?

时间:2011-09-10 13:42:53

标签: magento e-commerce

我希望能够以与产品页面中显示的相同的方式插入和显示产品,但能够插入CMS页面(包含属性,价格,订单按钮等)

我已经成功地通过插入产品页面的源代码的复制部分来创建单个页面,但考虑到我计划完成相当多的页面,这是一个非常耗时且耗时的过程。

因此,我希望能够将产品详细信息调用到CMS页面中。

我以为我可以使用view.phtml并将其插入所需的CMS页面,但我无法弄清楚如何定义product_id ......

提前感谢大家的意见

1 个答案:

答案 0 :(得分:3)

嗯,CMS Macro就像这样:

{{block type="catalog/product" template="catalog/product/line-item.phtml" sku_id="CI 101"}}

引用模板目录/ product / line-item.phtml:

<?php //Template_Name/catalog/product/line-item.phtml
      //{{block type="catalog/product" template="catalog/product/line-item.phtml" sku_id="CI 100"}}
      //Feed template SKU for product listing 
?>

<?php $_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$this->getData('sku_id')); ?>

<?php /* get special freight messages from custom variables */
    $freightfree = Mage::getModel('core/variable')->loadByCode('free_freight_text')->getValue('plain');
    $hazmat = Mage::getModel('core/variable')->loadByCode('hazmat_text')->getValue('plain');
    $ormd = Mage::getModel('core/variable')->loadByCode('ormd_text')->getValue('plain');
?>

<!-- <div class="single-product"> -->
<div class="listing-type-list catalog-listing">
<div class="listing-item last">
    <?php $specialshipping = $_product->getAttributeText('special_shipping_group') ?>

        <?php // Product Image ?>
        <div class="product-image">
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getSmallImageLabel()) ?>">
                <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
            </a>
        </div>

        <?php // Product description ?id= echo $_product->getId();?>
        <div class="product-shop">
            <h2><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName())?></a></h5>
            <?php if($_product->getRatingSummary()): ?>
            <?php echo $this->getReviewsSummaryHtml($_product) ?>
            <?php endif; ?>
            <?php echo $this->getPriceHtml($_product, true) ?>
            <?php if(!$_product->getNotforsale()): ?>
                <?php if(!$_product->getReplace_add_button()): ?>
                    <?php if($_product->isGrouped()): ?>
                        <button class="form-button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><?php echo $this->__('View Selection') ?></span></button>
                    <?php elseif($_product->getHasOptions()): ?>
                        <button class="form-button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><?php echo $this->__('View Options') ?></span></button>
                    <?php else: ?>
                        <button class="form-button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><?php echo $this->__('Add to Cart') ?></span></button>
                    <?php endif; ?>
                <?php else: ?>
                    <button class="form-button" onclick="setLocation('<?php echo $_product->getProductUrl() ?>')"><span><?php echo $_product->getAttributeText('replace_add_button') ?></span></button>                    
                <?php endif; ?>
            <?php endif; ?>
            <div class="clear"></div>
            <?php  /* display special freight messages from custom variables */ ?>
            <?php if($specialshipping == "Free Ground" || $specialshipping == "Free Gnd ORMD"): ?>
                <?php echo '<span class="regular-price"><span class="freightfree">' . $freightfree . '</span></span>' ?>
            <?php endif; ?>
            <?php if($specialshipping == "ORM-D"): ?>
                <?php echo '<span class="freightfree">' . $ormd . '</span>' ?>
            <?php elseif($specialshipping == "Free Gnd ORMD"): ?>
                <?php echo '<br><span class="freightfree">' . $ormd . '</span>' ?>
            <?php elseif($specialshipping == "HazMat"): ?>
                <?php echo '<span class="freightfree">' . $hazmat . '</span>' ?>
            <?php endif; ?>
            <div class="description">
                <?php echo nl2br($_product->getShortDescription()) ?>
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><small><?php echo $this->__('Learn More') ?></small></a>
            </div>
            <p class="add-to">
                Brand Name: <?php echo $_product->getBrand() ?>
            </p>
        </div>
 </div>
 </div>

请注意,这会引入您可能在系统中找不到的几个自定义属性和变量。最重要的是CMS宏

{{block type="catalog/product" template="catalog/product/line-item.phtml" sku_id="CI 101"}}

和这一行:

<?php $_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$this->getData('sku_id')); ?>

根据需要在CMS页面上多次使用宏。