3

次の質問に似ています: Magento - グループ化された製品テーブルにカスタム属性を表示する

グループ化された商品ページに単純な商品の属性を表示したいと考えています。

ただし、表示される属性を明示的に指定しないようにする必要があります。代わりに、その製品の単純な製品ビューに表示されるすべての属性が表示されます。

私は次のバリエーションを試しました:

(/template/catalog/product/view/type/grouped.phtml より)

<?php foreach ($_associatedProducts as $_item): ?>
  <tr>
            <td><?php echo $this->htmlEscape($_item->getName()) ?></td>

   <!-- important problem part -->
   <?php foreach ($_item->getAttributes() as $arr): ?>
    <td><?php echo $arr->getData() ?></td>
   <?php endforeach; ?>
   <!-- end of problem part -->

            <td class="a-right">
                <?php echo $this->getPriceHtml($_item, true) ?>
            </td>
            <?php if ($_product->isSaleable()): ?>
            <td class="a-center">
            <?php if ($_item->isSaleable()) : ?>
    <a href="<?php echo $_item->getUrlPath() ?>">View</a>
            <?php else: ?>
                <p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock.') ?></span></p>
            <?php endif; ?>
            </td>
            <?php endif; ?>
        </tr>
    <?php endforeach; ?>

しかし、他のバリエーションでは、表示される属性を必要なものだけに限定することはできません (つまり、単純な製品ビューに表示される追加の属性、フロントエンドで表示可能として設定されたものなど)。何か案は?

4

2 に答える 2

1

後に追加$_product = $this->getProduct();

/* CODE TO GET ATTRIBUTES */
$gridattributes = array();
$attributes = $_product->getAttributes();
foreach ($attributes as $attribute) {
  if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
    $value = $attribute->getFrontend()->getValue($_product);
    if (!$_product->hasData($attribute->getAttributeCode())) {
      $value = Mage::helper('catalog')->__('N/A');
    } elseif ((string)$value == '') {
      $value = Mage::helper('catalog')->__('No');
    } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
      $value = Mage::app()->getStore()->convertPrice($value, true);
    }

    if (is_string($value) && strlen($value)) {
      $gridattributes[$attribute->getAttributeCode()] = array(
        'label' => $attribute->getStoreLabel(),
        'value' => $value,
        'code'  => $attribute->getAttributeCode()
      );
    }
  }
}
?>

後に追加<th><?php echo $this->__('Product Name') ?></th>

foreach($gridattributes as $attrib){
    echo '<th>'.$this->htmlEscape($attrib[label]).'</th>';
}

後に追加<td><?php echo $this->htmlEscape($_item->getName()) ?></td>

foreach($gridattributes as $attribname=>$attribval){
    echo '<td>'.$this->htmlEscape($_item->getData($attribname)).'</td>';
}
于 2011-04-22T23:07:02.273 に答える
1

クラス Mage_Catalog_Block_Product_View_Attributes メソッド getAdditionalData() は、フロントエンドで表示可能として選択された変数のみに変数を制限する方法を示しているはずです。その getAdditionalData メソッドは、製品ビュー ブロックで参照されます。

これを解決する手順は次のとおりです。 1. グループ化された製品ブロックをオーバーライドする目的で、新しい Magento モジュールを作成します。2. Mage_Catalog_Block_Product_View_Attributes の getAdditionalData() メソッドから自由に盗んで、グループ化された新しい製品ブロックを作成します。3. カスタム ブロックをサポートするために、/template/catalog/product/view/type/grouped.phtml に基づいて新しいテンプレートを作成します。4. モジュールの config.xml のブロックを上書きします (参照: catalog/breadcrumbs および catalog/products/viewの上書き、Magento フォーラム)

これにより、カタログ内のグループ化されたすべての製品がこのように動作するという結果になります。より選択的にする必要がある場合は、動作を切り替えてチェックをプログラムするために、カスタム属性をカタログ製品に追加する (できれば、作成したばかりのカスタム モジュールで設定することをお勧めします!) のが合理的な方法だと思います。テンプレートに切り替えます。

于 2011-01-24T21:50:31.813 に答える