All Addtional Attributes for Grouped Products in M

2019-04-11 17:01发布

问题:

Similar to the question asked at: Magento - Show Custom Attributes in Grouped Product table

I'd like to display attributes of simple products in the grouped product page.

However, I need it to work such that you do not explicitly specify which attributes get displayed. Instead, it displays all the attributes that would get displayed on the simple product view of that product.

I've tried variations of:

(from /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; ?>

And other variations, however, I cannot limit the attributes being displayed to just the ones I need (i.e. just the additional attributes displayed on the simple product view; those set as Viewable on Frontend). Any ideas?

回答1:

The class Mage_Catalog_Block_Product_View_Attributes method getAdditionalData() should point you toward how to limit the variables to only those that are selected as Viewable on Frontend. Its getAdditionalData method is referenced in the product view block.

The steps to solving this would be the following: 1. Create a new Magento module with the intent to override the grouped product block. 2. Create the new grouped product block, stealing liberally from the getAdditionalData() method of Mage_Catalog_Block_Product_View_Attributes. 3. Create a new template based on /template/catalog/product/view/type/grouped.phtml to back your custom block. 4. Override the block in your module's config.xml (see: Overriding catalog/breadcrumbs and catalog/products/view, Magento forums)

This has the consequence that all your grouped products in the catalog will behave this way. If you need to be more selective, then I think a reasonable thing to do would be to add a custom attribute to catalog products (preferably set up in the custom module you just created!) in order to toggle the behavior, and program a check for that toggle into your template.



回答2:

Add after $_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()
      );
    }
  }
}
?>

Add after <th><?php echo $this->__('Product Name') ?></th>:

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

Add after <td><?php echo $this->htmlEscape($_item->getName()) ?></td>:

foreach($gridattributes as $attribname=>$attribval){
    echo '<td>'.$this->htmlEscape($_item->getData($attribname)).'</td>';
}