2012-03-26 28 views
1

我看了一下,似乎無法找到我在此之後的確切答案,所以我發了一個新帖子。根據Magento中的屬性集名稱做些什麼

我需要根據產品集是否具有指定的特定屬性集,在產品列表頁面上顯示特定的HTML。

有4個屬性集,產品可以有'服裝','靴子','包'和'配件'。

所以,如果集合有'Bags'或'Accessories'atrribute集合,我想展示一件東西以及其他任何我想展示的東西。我的代碼到目前爲止是:

<?php 
        $bags = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityTypeId)->addFilter('attribute_set_name', 'Bags'); 
        $accessories = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityTypeId)->addFilter('attribute_set_name', 'Accessories'); 

        $size_default = $_product -> getResource() -> getAttribute('size') -> getFrontend() -> getValue($_product); 
        $size_clothes_fr = $_product -> getResource() -> getAttribute('size_clothes_fr') -> getFrontend() -> getValue($_product); 
        $size_clothes_uk = $_product -> getResource() -> getAttribute('size_clothes_uk') -> getFrontend() -> getValue($_product); 
        $size_clothes_us = $_product -> getResource() -> getAttribute('size_clothes_us') -> getFrontend() -> getValue($_product); 
        $size_clothes_it = $_product -> getResource() -> getAttribute('size_clothes_it') -> getFrontend() -> getValue($_product); 
        $size_shoes_default = $_product -> getResource() -> getAttribute('size_shoe') -> getFrontend() -> getValue($_product); 
        $size_shoes_fr = $_product -> getResource() -> getAttribute('size_shoes_fr') -> getFrontend() -> getValue($_product); 
        $size_shoes_it = $_product -> getResource() -> getAttribute('size_shoes_it') -> getFrontend() -> getValue($_product); 
        $size_shoes_uk = $_product -> getResource() -> getAttribute('size_shoes_uk') -> getFrontend() -> getValue($_product); 
        $size_shoes_us = $_product -> getResource() -> getAttribute('size_shoes_us') -> getFrontend() -> getValue($_product); 
       ?> 
       <?php if(trim($bags) || trim($accessories)) { ?> 
        <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" class="product-image"><?php if($_product->isSaleable()): ?><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(140, 255); ?>" width="140" height="255" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" id="product-image-<?php echo $_product->getId() ; ?>" /><?php endif; ?><?php if(!$_product->isSaleable()): ?><img src="<?php echo $this->getSkinUrl('images/masks/white80.png') ?>" width="140" height="255" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" id="product-image-<?php echo $_product->getId() ; ?>" style="background:url('<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(140, 255); ?>') no-repeat center center" /><?php endif; ?></a> 
       <?php } else { ?> 
       <div class="sizes"> 
        <a class="trigger" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" class="product-image"><?php if($_product->isSaleable()): ?><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(140, 255); ?>" width="140" height="255" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" id="product-image-<?php echo $_product->getId() ; ?>" /><?php endif; ?><?php if(!$_product->isSaleable()): ?><img src="<?php echo $this->getSkinUrl('images/masks/white80.png') ?>" width="140" height="255" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" id="product-image-<?php echo $_product->getId() ; ?>" style="background:url('<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(140, 255); ?>') no-repeat center center" /><?php endif; ?></a> 
        <div class="popup"> 
         <p class="size_title"><?php echo $size_default ;?></p> 
         <p class="size_title"><?php echo $size_clothes_fr ;?></p> 
         <p class="size_title"><?php echo $size_clothes_it ;?></p> 
         <p class="size_title"><?php echo $size_clothes_us ;?></p> 
         <p class="size_title"><?php echo $size_clothes_uk ;?></p> 
         <p class="size_title"><?php echo $size_shoes_default ;?></p> 
         <p class="size_title"><?php echo $size_shoes_fr ;?></p> 
         <p class="size_title"><?php echo $size_shoes_it ;?></p> 
         <p class="size_title"><?php echo $size_shoes_uk ;?></p> 
         <p class="size_title"><?php echo $size_shoes_us ;?></p> 
        </div> 
       </div> 
       <?php } ?> 

它似乎雖然工作,它總是顯示else語句不管。我顯然沒有在這裏做一些事情。任何人都可以幫忙嗎?謝謝。

回答

0

你的問題(如果你還沒有解決的話)可能是因爲你的集合沒有返回正確的數據。你得到的東西本質上是一組屬性集合,按其名稱進行過濾。每次都是一樣的,因爲你在任何時候都不涉及產品。

嘗試讓名字是這樣的:

$attSetName = Mage::getModel(’eav/entity_attribute_set’) 
    ->load($_product->getAttributeSetId()) 
    ->getAttributeSetName(); 

,然後比較像這樣的變量:

if ($attSetName == "bags" || $attSetName == "accessories") { 

如果你在返回集合打算,你也可能會遇到的問題,因爲你」重新使用「trim()」php函數。修剪是針對字符串的,所以修剪集合對象可能不會做你想做的事情。試試這個:

if (count($bags) > 0 || count($accessories) > 0) { 
相關問題