<?php declare(strict_types=1);
namespace HuebertAddOrderAttributes\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderCreateSubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
protected $orderRepository;
/**
* @var SystemConfigService
*/
protected $pluginConfig;
/**
* @var EntityRepositoryInterface
*/
protected $productRepository;
public function __construct(
EntityRepositoryInterface $orderRepository,
SystemConfigService $pluginConfig,
EntityRepositoryInterface $productRepository
)
{
$this->orderRepository = $orderRepository;
$this->pluginConfig = $pluginConfig;
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents()
{
return [
CheckoutOrderPlacedEvent::class => 'onWrittenOrder'
];
}
public function onWrittenOrder(CheckoutOrderPlacedEvent $event){
$salesChannelId = $event->getSalesChannelId();
$showInCustomerComment = $this->pluginConfig->get('HuebertAddOrderAttributes.config.showInCustomerComment', $salesChannelId);
$lineItems = $event->getOrder()->getLineItems();
$comment = $event->getOrder()->getCustomerComment();
$currencySimbol = $event->getOrder()->getCurrency()->getSymbol();
if($showInCustomerComment) {
foreach ($lineItems as $lineItem) {
$critera = new Criteria();
$critera->addFilter(new EqualsFilter('id', $lineItem->getProductId()));
$productName = $this->productRepository->search($critera, $event->getContext())->first();
if($productName) {
if(isset($productName->getTranslated()['name'])) {
$productName = $productName->getTranslated()['name'];
} else {
$productName = $productName->getName();
}
}
if (array_key_exists('huebert_attributes', $lineItem->getPayload())) {
$attributes = $lineItem->getPayload()['huebert_attributes'];
$comment .= PHP_EOL.$productName . PHP_EOL;
foreach ($attributes as $attribute) {
foreach ($attribute as $attr) {
if(is_array($attr)) {
foreach ($attr as $a) {
if(is_array($a)) {
if (array_key_exists('value', $a)) {
if (is_array($a['value'])) {
if (array_key_exists('attributes_with_prices', $a['value'])) {
foreach ($a['value']['attributes_with_prices'] as $key => $value) {
$comment .= PHP_EOL . $key . ': ' . PHP_EOL;
foreach ($a['value']['attributes_with_prices'][$key] as $k => $v) {
$comment .= PHP_EOL . $k . ': ' . $v['price'] . ' ' . $currencySimbol . PHP_EOL;
}
}
}
} else {
$comment .= PHP_EOL . $a['name'] . ': ' . $a['value'] . PHP_EOL;
}
}
}
}
if(isset($attr['value']) && !is_array($attr['value'])){
$comment .= PHP_EOL . $attr['name'] . ': ' . $attr['value'] . PHP_EOL;
}
}
}
}
}
}
$this->orderRepository->update([[
'id' => $event->getOrder()->getId(),
'customerComment' => $comment,
]], $event->getContext());
}
}
}