Question from our agency regarding a webshop built with Shopware 5: How can you add the EANs of the variant products of a main product to the variant selection? The goal here was to add the EAN as an additional identifier to the respective option, which could then be controlled via an automated size selection. In other words: If I select a specific size using a size calculator, this size is selected via the corresponding EAN (jQuery), and the associated variant product is then loaded. This can also be applied to the more general question: How can I add properties of main or variant products or any additional information to the product detail page of my Shopware 5 online shop?
For this purpose, we developed a small plugin that provides us with the additional information on the product detail page. Here, we use the event
onPostDispatchDetail
from
Enlight_Controller_Action_PostDispatchSecure_Frontend_Detail
To do this, we declare our service in the file (the manufacturer abbreviation / plugin name can be chosen freely, while the plugin structure remains the same)
custom/plugins/EcoProductDetailOptionIds/Resources/services.xml
as follows:
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="eco.product_detail_option_ids.subscriber.frontend"
class="EcoProductDetailOptionIds\Subscriber\Frontend">
<argument>%eco_product_detail_option_ids.plugin_dir%</argument>
<argument type="service" id="dbal_connection" />
<tag name="shopware.event_subscriber" />
</service>
</services>
</container>
So erstellen wir dann die Datei
/web/stageware1/custom/plugins/EcoProductDetailOptionIds/Subscriber
mit folgendem Inhalt:
<?php
namespace EcoProductDetailOptionIds\Subscriber;
use Enlight\Event\SubscriberInterface;
use Enlight_Event_EventArgs;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
/**
* @package EcoProductDetailOptionIds\Subscriber
*/
class Frontend implements SubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'Enlight_Controller_Action_PostDispatchSecure_Frontend_Detail' => 'onPostDispatchDetail',
];
}
/**
* @var \Doctrine\DBAL\Connection
*/
private $connection;
/**
* @var string $pluginPath
*/
protected $pluginPath;
public function __construct(
$pluginPath,
Connection $connection
) {
$this->pluginPath = $pluginPath;
$this->connection = $connection;
}
public function onPostDispatchDetail(\Enlight_Event_EventArgs $args)
{
$controller = $args->getSubject();
$view = $controller->View();
$vars = $view->getAssign();
$sArticle = $vars['sArticle'];
if (!$sArticle || !isset($sArticle['sConfigurator']) || count($sArticle['sConfigurator']) > 1) {
return;
}
$ergebnis = $this->connection->createQueryBuilder()
->select('articlesdetails.id, articlesdetails.ean')
->from('s_articles_details', 'articlesdetails')
->join('articlesdetails', 's_article_configurator_option_relations', 'rel', 'rel.article_id = articlesdetails.id')
->addSelect('rel.option_id AS option_id')
->where('articlesdetails.articleID = :articlesId')
->setParameter('articlesId', $sArticle['articleID'])
->execute()
->fetchAll(\PDO::FETCH_ASSOC);
foreach ($ergebnis as $ergvalue) {
$ergebnis_array[$ergvalue['option_id']] = $ergvalue['ean'];
}
foreach ($sArticle['sConfigurator'][0]['values'] as $ecokey => $ecovalue) {
$ecovalue['EANId']='';
if (isset($ergebnis_array[($ecovalue['optionID'])])) $ecovalue['EANId']=$ergebnis_array[($ecovalue['optionID'])];
$sArticle['sConfigurator'][0]['values'][$ecokey] = $ecovalue;
}
$view->assign('sArticle', $sArticle);
}
}
So, using the article ID of the main product and the tables
s_articles_details
and
s_article_configurator_option_relations
we retrieve the corresponding variant products and their associated EAN and assign them to the respective options, which we can then output in the template
themes/Frontend/IRGENDEINTHEMENAME/frontend/detail/config_variant.tpl
using the "EANId" index. Done! Seen at our web agency in a webshop with Shopware 5.6.
