步骤1:全局模块配置
在 app / etc / modules / 目录中创建自定义块配置xml文件。我将其命名为 Lollicupstore_ShowCategoryName.xml。
<?xml version="1.0"?>
<config>
<modules>
<Lollicupstore_ShowCategoryName>
<active>true</active>
<codePool>local</codePool>
</Lollicupstore_ShowCategoryName>
</modules>
</config>
步骤2:创建目录
为您的模块创建正确的目录:
app/code/local/Lollicupstore
app/code/local/Lollicupstore/ShowCategoryName
app/code/local/Lollicupstore/ShowCategoryName/Block
app/code/local/Lollicupstore/ShowCategoryName/etc
步骤3:模块配置
添加您的模块配置文件。这应该命名为 config.xml, 并放置在 app / code / local / Lollicupstore/ShowCategoryName /etc内。
<?xml version="1.0"?>
<config>
<modules>
<Lollicupstore_ShowCategoryName>
<version>0.1.0</version>
</Lollicupstore_ShowCategoryName>
</modules>
<global>
<blocks>
<lollicupstoreshowcategoryname>
<class>Lollicupstore_ShowCategoryName_Block</class>
</lollicupstoreshowcategoryname>
</blocks>
</global>
</config>
步骤4:创建块类
现在,您可以创建您的块类。在此示例中,我们将其简称为 Category .php 并将其放置在 app / code / local / Lollicupstore/ShowCategoryName/ Block内。
如果商品分类名称是“Close Out”,就描红,这里根据id = 5 来判断。
D:\www\lollicupStore2\app\code\local\Lollicupstore\ShowCategoryName\Block\Category.php
<?php
/**
* Created by PhpStorm.
* User: lollicup
* Date: 2019/9/30
* Time: 10:55
*/
class Lollicupstore_ShowCategoryName_Block_Category extends Mage_Core_Block_Template
{
public function getCategoryName()
{
$productId = $this->getProduct();
$_product = Mage::getModel('catalog/product')->load($productId);
$cats = $_product->getCategoryIds();
$category = "";
foreach ($cats as $category_id){
$_cat = Mage::getModel('catalog/category')->load($category_id);
if($category_id == 5){
$category = $category."<font color=\"#FF0000\">Close Out</font>".", ";
}else{
$category = $category.$_cat->getName() .", ";
}
}
return substr($category,0,-2);
}
}
步骤5:创建视图脚本
创建一个视图脚本模板文件以用于您的块。我只是将其放置为 app / design / frontend / lollicupstore /default/ template/showcategoryname / 文件夹中的category.phtml文件 。
D:\www\lollicupStore2\app\design\frontend\lollicupstore\default\template\showcategoryname\category.phtml
<?php
echo $this->getCategoryName();
?>
步骤6:以编程方式将代码块嵌入到你需要展示的页面
6.1 嵌入到后台订单详情页面
D:\www\lollicupStore2\app\design\adminhtml\default\default\template\sales\order\view\items\renderer\default.phtml
<tr<?php if (!$this->canDisplayGiftmessage()): ?> class="border"<?php endif; ?>>
<td>
......
<h5>Category:
<?php
echo $this->getLayout()->createBlock('lollicupstoreshowcategoryname/category')->setProduct($_item->getProductId())->setTemplate('showcategoryname/category.phtml')->toHtml();
?>
</h5>
</td>
......
6.2 嵌入到后台发票详情页面
D:\www\lollicupStore2\app\design\adminhtml\default\default\template\sales\order\invoice\view\items\renderer\default.phtml
<?php $_item = $this->getItem() ?>
<?php $_item->setStoreId($_item->getInvoice()->getStoreId()) ?>
<?php $this->setPriceDataObject($_item) ?>
<tr class="border">
<td><?php echo $this->getColumnHtml($_item, 'name') ?>
<h5>Category:
<?php
echo $this->getLayout()->createBlock('lollicupstoreshowcategoryname/category')->setProduct($_item->getProductId())->setTemplate('showcategoryname/category.phtml')->toHtml();
?>
</h5>
</td>
6.3 嵌入到前台购物车页面
<?php
$_item = $this->getItem();
$isVisibleProduct = $_item->getProduct()->isVisibleInSiteVisibility();
$canApplyMsrp = Mage::helper('catalog')->canApplyMsrp($_item->getProduct(), Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_BEFORE_ORDER_CONFIRM);
?>
......
<div class="product-cart-sku">
<span class="label"><?php echo $this->__('SKU'); ?>:</span> <?php echo $this->escapeHtml($_item->getSku()); ?>
</div>
<div class="product-cart-sku">
<span class="label"><?php echo $this->__('CATEGORY'); ?>:</span>
<?php
echo $this->getLayout()->createBlock('lollicupstoreshowcategoryname/category')->setProduct($_item->getProductId())->setTemplate('showcategoryname/category.phtml')->toHtml();
?>
</span>
</div>
......
6.4 嵌入到前台个人中心订单详情页面
D:\www\lollicupStore2\app\design\frontend\lollicupstore\default\template\sales\order\items\renderer\default.phtml
<?php $_item = $this->getItem()?>
<tr class="border" id="order-item-row-<?php echo $_item->getId() ?>">
......
<td data-rwd-label="<?php echo $this->__('SKU') ?>">
<?php
echo $this->getLayout()->createBlock('lollicupstoreshowcategoryname/category')->setProduct($_item->getProductId())->setTemplate('showcategoryname/category.phtml')->toHtml();
?>
</td>
......
D:\www\lollicupStore2\app\design\frontend\lollicupstore\default\template\sales\order\items.phtml
<table class="data-table linearize-table-large" id="my-orders-table" summary="<?php echo $this->__('Items Ordered') ?>">
<col />
<col width="1" />
<col width="1" />
<col width="1" />
<col width="1" />
<thead>
<tr>
<th><span><?php echo $this->helper('sales')->__('Image') ?></span></th>
<th><?php echo $this->__('Product') ?></th>
<th><?php echo $this->__('SKU') ?></th>
<th><?php echo $this->__('Category') ?></th>
<th class="a-right"><?php echo $this->__('Price') ?></th>
<th class="a-center"><?php echo $this->__('Qty') ?></th>
<th class="a-right"><?php echo $this->__('Subtotal') ?></th>
</tr>
</thead>