我想要在我的clearance类别中列出的所有产品上覆盖一个“clearance”图标。
用户显然会浏览它所在的其他类别下的产品。
所以我想,当我在每个页面上列出产品时,我需要检查它属于哪些其他类别,如果它处于“清除”状态,则添加一些额外的标记。
我可以在之后设计它的样式。即使我可以在产品图像下面添加<div class="clearance-tag"></div>,我也可以为它分配一个背景图像,并将其浮动/定位到我需要的位置。
我担心使用Magento时,我通常大致知道我想要编写什么代码/逻辑,只是不知道在哪里编写它。
有人能帮上忙吗?我正在使用Magento 1.6
发布于 2011-11-29 06:22:46
做到了。下面是如何实现的。
1)从base复制文件:
app/design/frontend/base/default/template/catalog/product/list.phtml app/design/frontend/base/default/template/catalog/product/view.phtml
添加到自定义设计包中。
app/design/frontend/mydesignpackage/default/template/catalog/product/list.phtml app/design/frontend/mydesignpackage/default/template/catalog/product/view.phtml
在list.phtml中查找行(在第86行附近)
<?php // Grid Mode ?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>紧跟在后面,插入以下代码:
<?php
//get all categories associated with this product
$categories = $_product->getCategoryIds();
$clearance_category = 134; //clearance category id
if(in_array($clearance_category, $categories)) {
$clearancetag='<div class="clearance-tag">Clearance</div>';
}
else {
$clearancetag='';
}
?>再往下走,找到真正的长线:
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>并在结束</a>标记之前插入您的变量:
null, true) ?>" /><?php echo $clearancetag ?></a>它负责产品网格。
旁边显示产品视图中的间隙图标。我将在描述的旁边显示它。
在view.phtml中查找以下行:
<?php echo $this->getReviewsSummaryHtml($_product, false, true)?>
<?php echo $this->getChildHtml('alert_urls') ?>
<?php echo $this->getChildHtml('product_type_data') ?>
<?php echo $this->getTierPriceHtml() ?>
<?php echo $this->getChildHtml('extrahint') ?>然后立即粘贴代码:
<?php
//get all categories associated with this product
$categories = $_product->getCategoryIds();
$clearance_category = 134; //clearance category id
if(in_array($clearance_category, $categories)) {
echo '<div class="clearance-tag">CLEARANCE</div>';
}
?>我相信有更好的方法来做到这一点,并且硬编码你的类别id并不理想。但它对我很有效,希望它能帮助其他人。
发布于 2011-11-29 06:26:52
首先获取产品的类别Id,然后循环此数组;
<?php
/* get categories from product */
$categoryIds = $product->getCategoryIds();
/* looping the array of the category ids */
foreach($categoryIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
?>这段代码给你categoryId,如果产品有一个指定的类别,那么你可以设置一些css类。
发布于 2011-11-26 03:46:44
查看类别页面的源代码,body元素应该已经有一些有用的类名,比如category-clearance (从URL键推导而来),您可以直接在样式表中使用它。按照您已经建议的方式添加DIV元素,然后使用以下命令设置样式:
.category-clearance .clearance-tag {
background-image: url(etc...)
}这是一个非常有用的技巧,几乎所有页面上的类名都是自动的。例如,主页由CMS模块提供服务,因此获得一个类名cms-home -这意味着您可以创建特定于主页的样式。
https://stackoverflow.com/questions/8271826
复制相似问题