In:
/magento/app/design/frontend/base/default/template/catalog/product/view/type/grouped.phtml
Vedrai che usano questo:
<?php
$_associatedProducts = $this->getAssociatedProducts();
Dal momento che il file è di tipo phtml Mage_Catalog_Block_Product_View_Type_Grouped
, possiamo andare a:
/magento/app/code/core/Mage/Catalog/Block/Product/View/Type/Grouped.php
e vedere che Mage_Catalog_Block_Product_View_Type_Grouped::getAssociatedProducts()
fa questo:
<?php
$this->getProduct()->getTypeInstance(true)->getAssociatedProducts($this->getProduct());
Quindi possiamo tranquillamente assumere che $this->getProduct()
restituisce un oggetto prodotto, e sostituirlo con la variabile $product
in questo modo:
<?php
if ($product->getTypeId() == 'grouped'){
// how do I now get associated products of $product?
$associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
}
Se fossi per ottimizzare completamente il vostro codice, ho 'd scrivere in questo modo:
<?php
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('type_id', array('eq' => 'grouped'));
foreach ($products as $product) {
$associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
// Do something with $associatedProducts
}
fonte
2011-12-20 22:35:54
Grazie potrei arrivare a questo lavoro ... Voglio eseguire un'iterazione su tutti i prodotti, e SE è raggruppato su iterate sui prodotti associati: voglio ancora tutti i singoli prodotti, non solo i prodotti associati dei prodotti raggruppati. Il tuo codice "ottimizzato" non includerebbe singoli prodotti regolari, ho ragione? – Owen
Corretto, otterrebbe solo una raccolta di prodotti raggruppati. Quello che vuoi usare è $ product-> isGrouped(). Quindi con la raccolta di tutti i prodotti, utilizzare: foreach ($ prodotti come $ prodotto) {if ($ product-> isGrouped()) {/ * Ottieni raccolta prodotti associati * /}} –