<?php declare(strict_types=1);
namespace FourtwosixTranslateOrderStates;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class FourtwosixTranslateOrderStates extends Plugin
{
public function install(InstallContext $context): void
{
$this->addStateMachineStateTranslation($context->getContext());
}
private function addStateMachineStateTranslation(Context $context)
{
$stateMachineStateTransRepo = $this->container->get('state_machine_state_translation.repository');
$allStates = $this->getAllStateMachineStates($context);
$languageIds = $this->getLanguagesIds($context);
$StateTranslatedNames = array(
'authorized' => array(
'en-GB' => "Authorized",
'de-DE' => "Autorisiert",
'it-IT' => "Autorizzato"
),
'cancelled' => array(
'en-GB' => "Cancelled",
'de-DE' => "Abgebrochen",
'it-IT' => "Annullato"
),
'chargeback' => array(
'en-GB' => "Chargeback",
'de-DE' => "Rückbuchung",
'it-IT' => "Storno"
),
'completed' => array(
'en-GB' => "Completed",
'de-DE' => "Abgeschlossen",
'it-IT' => "Completato"
),
'failed' => array(
'en-GB' => "Failed",
'de-DE' => "Fehlgeschlagen",
'it-IT' => "Fallito"
),
'in_progress' => array(
'en-GB' => "In Progress",
'de-DE' => "In Bearbeitung",
'it-IT' => "In corso"
),
'open' => array(
'en-GB' => "Open",
'de-DE' => "Offen",
'it-IT' => "Aperto"
),
'paid' => array(
'en-GB' => "Paid",
'de-DE' => "Bezahlt",
'it-IT' => "Pagato"
),
'paid_partially' => array(
'en-GB' => "Paid (partially)",
'de-DE' => "Teilweise bezahlt",
'it-IT' => "Parzialmente pagato"
),
'reminded' => array(
'en-GB' => "Reminded",
'de-DE' => "Erinnert",
'it-IT' => "Ricordato"
),
'returned' => array(
'en-GB' => "Returned",
'de-DE' => "Retour",
'it-IT' => "Restituito"
),
'returned_partially' => array(
'en-GB' => "Returned (partially)",
'de-DE' => "Teilretour",
'it-IT' => "Parzialmente restituito"
),
'shipped' => array(
'en-GB' => "Shipped",
'de-DE' => "Versandt",
'it-IT' => "Spedito"
),
'shipped_partially' => array(
'en-GB' => "Shipped (partially)",
'de-DE' => "Teilweise versandt",
'it-IT' => "Parzialmente spedito"
),
'unconfirmed' => array(
'en-GB' => "Unconfirmed",
'de-DE' => "Unbestätigt",
'it-IT' => "Non comfermato"
),
'refunded_partially' => array(
'en-GB' => "Refunded (partially)",
'de-DE' => "Teilweise erstattet",
'it-IT' => "Parzialmente rimborsato"
),
'refunded' => array(
'en-GB' => "Refunded",
'de-DE' => "Erstattet",
'it-IT' => "Rimborso"
),
'pending' => array(
'en-GB' => "Pending",
'de-DE' => "Wartet",
'it-IT' => "In attesa"
)
);
$langIds = $languageIds;
foreach($allStates as $state){
foreach($langIds as $code => $langId){
if(empty($StateTranslatedNames[$state->getTechnicalName()])){
continue;
}
$stateMachineStateTransRepo->upsert([
['languageId' => $langId, 'stateMachineStateId' => $state->getId(), 'name' => $StateTranslatedNames[$state->getTechnicalName()][$code]]
], $context);
}
}
}
private function getAllStateMachineStates(Context $context)
{
$stateMachineStateRepo = $this->container->get('state_machine_state.repository');
$states = $stateMachineStateRepo->search(new Criteria(), $context)->getElements();
return $states;
}
private function getLanguagesIds(Context $context)
{
$langCodes = ['en-GB', 'de-DE', 'it-IT'];
$languageRepo = $this->container->get('language.repository');
$langs = array();
//get foreach langcode the language id
foreach ($langCodes as $lang) {
$criteria = new Criteria();
$criteria->addAssociation('locale');
$criteria->addFilter(new EqualsFilter('locale.code', $lang));
$result = $languageRepo->search($criteria, $context);
if (($language = $result->first()) != null) {
$langs[$lang] = $language->getId();
}
}
return $langs;
}
}