logTom
9. November 2017 um 16:49
1
Hi,
we developed a plugin that adds an attribute to articles and article variants.
The attribute is there to disable some variants of products in one of the subshops.
Now we have an article with 3 variants.
Test variant1 for 30 € (this variant should be disabled in the subshop)
Test variant2 for 40 €
Test variant3 for 50 €
Because the cheapest variant is disabled the “priceStartingFrom” if the article is wrong in the subshop.
Via these hooks
‘sArticles::sGetArticleById::after’
‘sArticles::sGetArticlesByCategory::after’
we modified the article listings to incorporate this.
Now we can’t get this working with the aritcle slider widget using a product stream.
<?php
/**
* Shopware 5
* Copyright (c) shopware AG
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* "Shopware" is a registered trademark of shopware AG.
* The licensing of the program under the AGPLv3 does not imply a
This file has been truncated. show original
here at Line #165
$requestedProducts = $collection->getBatchResult()->get($key);
My first guess is to modify the price of the products in $requestedProducts if they have variants with the given attributes.
Are there any hooks or events to do this easily? I can’t see a solution other than direct manipulate the code.
I also tried to modify the price in the theme at
shopware/themes/Frontend/Bare/frontend/listing/product-box/product-price.tpl
but I can’t figure out how to get the article variants in there.
Any ideas how to handle that?
logTom
14. November 2017 um 15:03
3
pump! Is something not understandable?
Hi,
I had a similar problem, needed to modify articles within the article slider component. I also requested a simple hook event but there isn’t for this component
https://forum.shopware.com/discussion/49152/eventcomponenthandler-kein-eventfilter-in-shopware-5-3#latest (if you are able to read german)
So I needed to decorate the StructConverter
// in your Bootstrap
public function install(){
$this->subscribeEvent(
'Enlight_Bootstrap_AfterInitResource_shopware_emotion.emotion_struct_converter',
'decorateEmotionStructConverter'
);
}
public function decorateEmotionStructConverter()
{
$coreService = Shopware()->Container()->get('shopware_emotion.emotion_struct_converter');
$eventManager = Shopware()->Container()->get('events');
$converter = Shopware()->Container()->get('legacy_struct_converter');
$structConverter = new \MyPlugin\ComponentHandler\StructConverter($coreService, $eventManager, $converter);
Shopware()->Container()->set('shopware_emotion.emotion_struct_converter', $structConverter);
}
// the decorated struct converter
// excluded my modifyProduct function
class StructConverter
{
/**
* @var \Shopware\Bundle\EmotionBundle\Service\StructConverter
*/
private $service;
/**
* @var \Enlight_Event_EventManager
*/
private $eventManager;
/**
* @var LegacyStructConverter
*/
private $converter;
/**
* StructConverter constructor.
* @param \Shopware\Bundle\EmotionBundle\Service\StructConverter $service
* @param \Enlight_Event_EventManager $eventManager
* @param LegacyStructConverter $converter
*/
public function __construct(\Shopware\Bundle\EmotionBundle\Service\StructConverter $service, \Enlight_Event_EventManager $eventManager, LegacyStructConverter $converter)
{
$this->service = $service;
$this->eventManager = $eventManager;
$this->converter = $converter;
}
/**
* @param Emotion $emotion
*
* @return array
*/
public function convertEmotion(Emotion $emotion)
{
return $this->service->convertEmotion($emotion);
}
/**
* @param Element $element
*
* @return array
*/
public function convertEmotionElement(Element $element)
{
$elementArray = json_decode(json_encode($element), true);
$elementArray['component']['xType'] = $element->getComponent()->getType();
$elementArray['component']['cls'] = $element->getComponent()->getCssClass();
$elementArray['data'] = array_merge($element->getConfig()->getAll(), $element->getData()->getAll());
$elementArray['data']['objectId'] = md5($element->getId());
switch ($element->getComponent()->getType()) {
case BannerSliderComponentHandler::COMPONENT_NAME:
return $this->service->convertEmotionElement($element);
case BannerComponentHandler::COMPONENT_NAME:
return $this->service->convertEmotionElement($element);
case ArticleComponentHandler::COMPONENT_NAME:
if (!$element->getData()->get('product')) {
return $this->service->convertEmotionElement($element);
}
$elementArray['data']['categoryId'] = (int) $elementArray['article_slider_category'];
$product = $this->converter->convertListProductStruct($element->getData()->get('product'));
$product = $this->modifyProduct($product);
$elementArray['data'] = array_merge($elementArray['data'], $product);
break;
case ArticleSliderComponentHandler::COMPONENT_NAME:
if (!$element->getData()->get('products') || !array_filter($element->getData()->get('products'))) {
break;
}
$elementArray['data']['categoryId'] = (int) $elementArray['article_slider_category'];
$products = $this->converter->convertListProductStructList($element->getData()->get('products'));
foreach ($products as $key=>$product) {
$products[$key] = $this->modifyProduct($product);
}
$elementArray['data']['values'] = array_values($products);
break;
case CategoryTeaserComponentHandler::COMPONENT_NAME:
return $this->service->convertEmotionElement($element);
case ManufacturerSliderComponentHandler::COMPONENT_NAME:
return $this->service->convertEmotionElement($element);
case BlogComponentHandler::COMPONENT_NAME:
return $this->service->convertEmotionElement($element);
}
return $this->eventManager->filter('Legacy_Struct_Converter_Convert_Emotion_Element', $elementArray, ['element' => $element]);
}
}
I hope this may help you.
Greetings
logTom
20. November 2017 um 08:05
5
Thanks you. I didn’t use your code because it’s legacy and not using the 5.2 plugin system. But it pointed me in the right direction that I need to use a decorator.