Magento Observateur d'événements pour la collecte des produits du groupe par catégorie
-
22-10-2019 - |
Question
Je crée un module d'observation des événements qui groupe upsell produits par catégorie,
ci-dessous est le fichier model/observer.php
de mon module,
public function updateUpsells($observer)
{
$iCurrentCategory = Mage::registry('current_category')->getId();
$event = $observer->getEvent();
$oUpsellCollection = $event->getCollection();
foreach ($oUpsellCollection->getItems() as $key => $oUpsellProduct) {
$aCategoriesIds = $oUpsellProduct->getCategoryIds();
if (!in_array($iCurrentCategory, $aCategoriesIds)) {
$oUpsellCollection->removeItemByKey($key);
}
}
}
}
Après avoir activé ce module, i peut inclure fichier upsell product's category id's in upsell.phtml
par echo $this->getItemCollection()->getItems()
En conséquence, je suis arrivé,
Array
(
[170] => Mage_Catalog_Model_Product Object
(
.........................
[_data:protected] => Array
(
[entity_id] => 170
[entity_type_id] => 10
[attribute_set_id] => 44
[type_id] => simple
[category_ids] => Array
(
[0] => 12
[1] => 26
)
.......................
)
[171] => Mage_Catalog_Model_Product Object
(
.........................
[_data:protected] => Array
(
[entity_id] => 171
[entity_type_id] => 10
[attribute_set_id] => 44
[type_id] => simple
[category_ids] => Array
(
[0] => 16
[1] => 24
)
.......................
)
)
Alors maintenant, comment puis-je regrouper ces résultats du tableau par catégorie. Exemple 2 mentionné les produits upsell affectés à 4 catégories différentes, par conséquent j'ai besoin de produits du groupe comme,
Category id 12
product1 | Product2
Category id 26
product1 | Product2
Mon fichier Upsell.phtml,
<?php
$helper = Mage::helper('rainbowsettings');
$w = $helper->getUpsell_Width(150);
$h = $helper->getUpsell_Height(150);
?>
<div class="box-collateral box-up-sell">
<h2><strong><span><?php echo $this->__('You Might Also Like') ?></span></strong></h2>
<?php
$byCategory = array();
$noCategory = array();
foreach ($this->getItemCollection()->getItems() as $item) {
$categoryIds = $item->getCategoryIds();
if (is_array($categoryIds)) { //if item has categories
foreach ($categoryIds as $categoryId) { //match the item with the categories
if (!isset($byCategory[$categoryId])) {
$byCategory[$categoryId] = array();
}
$byCategory[$categoryId][] = $item;
}
}
else {//if the item is not in a category
$noCategory[] = $item;
}
}
if (count($noCategory) > 0) {
$byCategory[0] = $noCategory;
}
?>
<?php foreach ($byCategory as $categoryId=>$items) : ?>
<?php if ($categoryId) : ?>
<?php $category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId()->load($categoryId))?>
<a href="<?php echo $category->getUrl()?>"><?php echo $category->getName()?></a>
<?php else : ?>
<span><?php echo $this->__('Not Categorized')?></span>
<?php endif;?>
<?php foreach ($items as $_link) : ?>
<!-- HTML for displaying a product goes here -->
<div id="">
<ul class="">
<?php $this->resetItemsIterator() ?>
<li class="item <?php if($_link->getData('gala_label_style') == '')echo 'corner';else echo $_link->getData('gala_label_style'); ?><?php if($i == 0):?> first<?php elseif($i == $count-1):?> last<?php endif ?>">
<div class="product-item">
<?php Mage::helper('productlabels')->display($_product); ?>
<a href="<?php echo $_link->getProductUrl() ?>" title="<?php echo $this->escapeHtml($_link->getName()) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_link, 'small_image')->resize($w,$h) ?>" width="<?php echo $w ?>" height="<?php echo $h ?>" alt="<?php echo $this->escapeHtml($_link->getName()) ?>" />
</a>
<div class="product-shop product-shop-color">
<div class="f-fix">
<div class="f-fix-content">
<h3 class="product-name"><a href="<?php echo $_link->getProductUrl() ?>" title="<?php echo $this->escapeHtml($_link->getName()) ?>"><?php echo $this->escapeHtml($_link->getName()) ?></a></h3>
<p class="sku"><?php echo $_link->getSKU()?></p>
<?php echo $this->getReviewsSummaryHtml($_link,"short") ?>
<?php echo $this->getPriceHtml($_link, true, '-upsell') ?>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
<?php endforeach;?>
<?php endforeach ;?>
</div>
La solution
Vous pouvez ajouter ceci dans le modèle upsell o l'observateur (mais dans ce cas vous devez toujours modifier le modèle de upsell).
$byCategory = array();
$noCategory = array();
foreach ($this->getItemCollection()->getItems() as $item) {
$categoryIds = $item->getCategoryIds();
$categoryFound = false;
if (is_array($categoryIds)) { //if item has categories
foreach ($categoryIds as $categoryId) { //match the item with the categories
if ($categoryId == Mage::app()->getStore()->getRootCategoryId()) {
continue;
}
if (!isset($byCategory[$categoryId])) {
$byCategory[$categoryId] = array();
}
$byCategory[$categoryId][] = $item;
$categoryFound = true;
}
}
if (!$categoryFound) {//if the item is not in a category
$noCategory[] = $item;
}
}
Maintenant, ajoutez les produits qui ne sont pas dans une catégorie à la fin du tableau afin que vous ne reproduira pas tout le code ci-dessous.
if (count($noCategory) > 0) {
$byCategory[0] = $noCategory;
}
Maintenant, vous devriez avoir les éléments divisés par catégories.
Pour les afficher font:
<?php foreach ($byCategory as $categoryId=>$items) : ?>
<?php if ($categoryId) : ?>
<?php $category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($categoryId)?>
<a href="<?php echo $category->getUrl()?>"><?php echo $category->getName()?></a>
<?php else : ?>
<span><?php echo $this->__('Not Categorized')?></span>
<?php endif;?>
<?php foreach ($items as $item) : ?>
<!-- HTML for displaying a product goes here -->
<?php endforeach;?>
<?php endforeach ;?>