2011-11-25 97 views
1

我想在清除类别中列出的所有产品上覆盖“清除”图标。Magento - 在清除类别中的所有产品上覆盖“清除”图标

用户显然会浏览的产品下出现在其他类别。

所以我列出每个页面上的产品的时候想,我需要检查它所属的其他类别,并添加一些额外的标记如果它在'清除'。

我很好,以后的样式。即使我可以在产品图像下方添加 <div class="clearance-tag"></div> ,我可以为其分配背景图像并将其浮动/定位到我需要的位置。

恐怕与Magento我经常大致知道我想写什么代码/逻辑,只是不知道在哪里写它。

任何人都可以帮忙吗? 我使用Magento的1.6

回答

0

做到了。就是这样。

1)将文件从复制基:

应用程序/设计/前端/碱/默认/模板/目录/产品/ list.phtml 应用程序/设计/前端/碱/默认/模板/目录/product/view.phtml

到您的自定义设计包。

应用程序/设计/前端/ mydesignpackage /default/template/catalog/product/list.phtml 应用程序/设计/前端/ mydesignpackage /default/template/catalog/product/view.phtml

的列表中。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> 

照顾产品网格。

Next显示产品视图中的清除图标。我只是在说明旁边显示它。

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并不理想。但它为我工作,并希望它可以帮助其他人。

0

查看您的类别页面的源代码,body元素应该已经有一些有用的类的名称,如category-clearance(从网址键推断),你可以直接在你的样式表使用。添加DIV元素,你已经提出再搭配风格吧:

.category-clearance .clearance-tag { 
    background-image: url(etc...) 
} 

这是一个非常有用的技巧和类名是自动上几乎所有的网页。例如,主页由CMS模块提供,因此获得类名称cms-home - 这意味着您可以创建特定于主页的样式。

+0

谢谢,但我仍然不确定如何将DIV元素添加到每个单独产品的标记中。 情景是:用户正在浏览“家具”类别。某些产品旁边会出现一个图标,表示它们也出现在“清除”类别中。 实际上,我将“清除”项目分配给一个称为“清除”的类别,并且需要找到突出显示这些项目的方式,而不管您处于哪个类别页面。 你所描述的内容对于了解它还是有用的,它会帮助我完成各种其他事情,所以谢谢。 – elMarquis

+0

我明白你的意思了。我将不得不再想一想。 – clockworkgeek

1

首先获取产品的类别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类。

0

我设法用jQuery做到这一点。这对于某人在系统>配置>显示> HTML头下将其插入到其他脚本字段非常容易。

这适用于查找具有当前价格的产品。我对Magento的不同版本不熟悉,不知道是否需要更改选择器,但是这个原则应该可以在大多数环境中使用。

<style> 
    .special-tag { 
     position: absolute; 
     top: 0; 
     left: 0; 
     max-width: 50%; 
    } 
    .slider-products .special-tag { 
     display: none; 
    } 
    .special-tag-product { 
     position: absolute; 
     top: 40px; 
     left: 0; 
     max-width: 33%; 
    } 
</style> 
<script> 
    jQuery(document).ready(function($){ 
     $('.item-content-wrapper').has('.special-price').append('<img src = "yourimage.png" class = "special-tag"/>'); 
     $('.product-image').append(
     $('.product-shop').has('.special-price').length ? '<img src = "yourimage_big.png" class = "special-tag-product"/>' : "" 
     ); 
    }); 
</script> 

.slider-products类特定于我的网站,您可能不需要这种样式。