Magento Group Upsell -Produkte nach Kategorie
-
22-10-2019 - |
Frage
Ich bin ein Neuling von Magento, Standard verwendet $this->getItemCollection()->getItems()
(upsell.phtml) Anzeige aller verkauften Produkte.
Ich frage mich, wie man diese Ergebnisse nach seiner Kategorie gruppiert.
Beispiel, ich habe 4 up verkaufen Produkte,
3 Products from category A,
1 Product from category B
Im Blick muss ich so anzeigen,
Category A,
Product 1 | product 2 | Product 3
Category B,
Product 4
Bisher hat ein Modul erstellt, die Struktur unten ist,
local
Fugen
Accessories
etc
config.php
Model
Observer.php
Config.xml meines Moduls
<?xml version="1.0"?>
<config>
<modules>
<Fugen_Accessories>
<version>1.0</version>
</Fugen_Accessories>
</modules>
<frontend>
<events>
<catalog_product_upsell>
<observers>
<Fugen_Accessories>
<class>Fugen_Accessories/observer</class>
<method>updateUpsells</method>
</Fugen_Accessories>
</observers>
</catalog_product_upsell>
</events>
</frontend>
</config>
Mein observer.php
class Fugen_Accessories
{
public function __construct()
{
// leave empty
}
public function updateUpsells(Varien_Event_Observer $oObserver)
{
$iCurrentCategory = Mage::registry('current_category')->getId();
$oUpsellCollection = $oObserver->getCollection();
foreach ($oUpsellCollection->getItems() as $key => $oUpsellProduct) {
$aCategoriesIds = $oUpsellProduct->getCategoryIds();
if (!in_array($iCurrentCategory, $aCategoriesIds)) {
$oUpsellCollection->removeItemByKey($key);
}
}
}
}
Lösung
Schauen Sie sich an Mage_Catalog_Block_Product_List_Upsell::_prepareData()
.
Es wirft eine Veranstaltung mit der Upsell Products Collection aus.
Mage::dispatchEvent('catalog_product_upsell', array(
'product' => $product,
'collection' => $this->_itemCollection,
'limit' => $this->getItemLimit()
));
So können wir Beobachter erstellen, an verbunden sind catalog_product_upsell
Event- und Änderung von Upsell -Produkten, wie wir es wünschen.
<?xml version="1.0"?>
<config>
<modules>
<Fugen_Accessories>
<version>1.0</version>
</Fugen_Accessories>
</modules>
<global>
<models>
<fugen_accessories>
<class>Fugen_Accessories_Model</class>
</fugen_accessories>
</models>
</global>
<frontend>
<events>
<catalog_product_upsell>
<observers>
<fugen_accessories>
<class>fugen_accessories/observer</class>
<method>updateUpsells</method>
</fugen_accessories>
</observers>
</catalog_product_upsell>
</events>
</frontend>
</config>
In der Beobachter Methode erstellen
class Fugen_Accessories_Model_Observer
{
public function updateUpsells(Varien_Event_Observer $oObserver)
{
$iCurrentCategory = Mage::registry('current_category')->getId();
$oUpsellCollection = $oObserver->getCollection();
foreach ($oUpsellCollection->getItems() as $key => $oUpsellProduct) {
$aCategoriesIds = $oUpsellProduct->getCategoryIds();
if (!in_array($iCurrentCategory, $aCategoriesIds)) {
$oUpsellCollection->removeItemByKey($key);
}
}
}
}
Vergessen Sie auch nicht, Modulinitialisierungskonfiguration in hinzuzufügen app/etc/modules
Fugen_accessories.xml
<?xml version="1.0"?>
<config>
<modules>
<Fugen_Accessories>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Catalog/>
</depends>
</Fugen_Accessories>
</modules>
</config>
Passt auf Die Lösung wurde nicht getestet.