Visualizzazione attributi relativi al singolo prodotto in un Grouped Products

Problema:

Attualmente Magento non ci consente di decidere quali attributi scegliere di visualizzare all’interno di un Grouped Products. Questo può essere molto limitante, soprattutto rischiamo di fornire troppe poche indicazioni al cliente che deve già orientarsi all’interno di un tipo di prodotto speciale.

Soluzione:

Utilizzando le classi e le funzioni fornite dal core di Magento, possiamo rimediare aggiungendo il seguente codice nella pagina /app/design/frontend/default/MY_THEME/template/catalog/product/view/type/grouped.phtml:

<table cellspacing="0" id="super-product-table">
    <col />
    <col />
    <col width="1" />
    <thead>
        <tr>         
<?     
    $attribute_array = array();
    $attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')
      ->setEntityTypeFilter('4')
      ->addSetInfo()
      ->getData();
    foreach ($attributesInfo AS $attribute){
    //print_r($attribute);
      if ($attribute['is_user_defined'] && $attribute['is_visible_on_front']){
        foreach ($_associatedProducts as $_item){
          if (isset($_item->_data[''.$attribute['attribute_code'].'']) && ($_item->_data[''.$attribute['attribute_code'].'']!='')){
            if ($attribute['frontend_label']=='') $etichetta=$attribute['attribute_code'];
            else $etichetta=$attribute['frontend_label'];
            print '<th>'. $etichetta .'</th>';
            $attribute_array[$attribute['attribute_code']] = $attribute['frontend_input'];
            break;
          }
        }
      }
    }
?>
            <th><?php echo $this->__('Product Name') ?></th>
            <th><?php echo $this->__('Price') ?></th>
            <?php if ($_product->isSaleable()): ?>
            <th><?php echo $this->__('Qty') ?></th>
            <?php endif; ?>
        </tr>
    </thead>
    <tbody>
    <?php if (count($_associatedProducts)): ?>
    <?php foreach ($_associatedProducts as $_item): ?>
        <?php $_finalPriceInclTax = $this->helper('tax')->getPrice($_item, $_item->getFinalPrice(), true) ?>
        <tr>
            <td><?php echo $this->htmlEscape($_item->getName()) ?></td>
<?
    $array_keys = array_keys($attribute_array);
    foreach ($array_keys AS $attribute){
      if (isset($_item->_data[''.$attribute.'']) && $_item->_data[''.$attribute.'']!=''){
        if($attribute_array[$attribute]=='select'){
            $model_att = Mage::getModel('eav/entity_attribute');
            $att_id = $model_att->getIdByCode('catalog_product', $attribute);
            $model_att->load($att_id);
            $model_att_options = Mage::getModel('eav/entity_attribute_source_table');       
            $model_att_options->setAttribute($model_att); 
            print '<td nowrap="nowrap">'. $model_att_options->getOptionText($_item->_data[''.$attribute.'']) .'</td>';
        }
        elseif($attribute_array[$attribute]=='boolean'){
          if ($_item->_data[''.$attribute.'']==0) print '<td nowrap="nowrap">'. $this->__('No') .'</td>';
          else print '<td nowrap="nowrap">'. $this->__('Yes') .'</td>';
        }
        else print '<td nowrap="nowrap">'. $_item->_data[''.$attribute.''] .'</td>';
      }
      else print '<td></td>';
    }
?>
            <td>
                <?php echo $this->getPriceHtml($_item, true) ?>
                <?php echo $this->getTierPriceHtml($_item) ?>
            </td>
            <?php if ($_product->isSaleable()): ?>
            <td>
            <?php if ($_item->isSaleable()) : ?>
                <input name="super_group[<?php echo $_item->getId() ?>]" value="<?php echo $_item->getQty()*1 ?>" type="text" />
            <?php else: ?>
                <?php echo $this->__('Out of stock.') ?>
            <?php endif; ?>
            </td>
            <?php endif; ?>
        </tr>
    <?php endforeach; ?>
    <?php else: ?>
       <tr>
           <td colspan="<?php if ($_product->isSaleable()): ?>4<?php else : ?>3<?php endif; ?>"><?php echo $this->__('No options of this product are available.') ?></td>
       </tr>
    <?php endif; ?>
    </tbody>
</table>

In grassetto corsivo, le parti di codice modificate ad hoc per ottenereil risultato sopra descritto. In pratica si tratta di richiamare tutti gli attributi per prodotto esistenti attraverso la chiamata:

Mage::getResourceModel('eav/entity_attribute_collection')
      ->setEntityTypeFilter('4')
      ->addSetInfo()
      ->getData();

Dopodichè questi vengono attraversati in combinazione con i prodotti presenti nello specifico grouped, eliminando tutti gli attributi di sistema e gli attributi vuoti per ogni prodotto. Ciò che rimane non è altro che una lista di etichette e valori di tutti gli attributi che hanno almeno una corrispondenza nell’elenco prodotti in esame. Non resta quindi che stampare opportunamente la tabella ed il gioco è fatto.

0 commenti

Lascia un Commento

Vuoi partecipare alla discussione?
Sentitevi liberi di contribuire!

Lascia un commento