custom/plugins/fourtwosixtranslateorderstates/src/FourtwosixTranslateOrderStates.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixTranslateOrderStates;
  3. use Shopware\Core\Framework\Plugin;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. class FourtwosixTranslateOrderStates extends Plugin
  9. {
  10.     public function install(InstallContext $context): void
  11.     {
  12.         $this->addStateMachineStateTranslation($context->getContext());
  13.     }
  14.     private function addStateMachineStateTranslation(Context $context)
  15.     {
  16.         $stateMachineStateTransRepo $this->container->get('state_machine_state_translation.repository');
  17.         $allStates $this->getAllStateMachineStates($context);
  18.         $languageIds $this->getLanguagesIds($context);
  19.         $StateTranslatedNames = array(
  20.             'authorized' => array(
  21.                 'en-GB' => "Authorized",
  22.                 'de-DE' => "Autorisiert",
  23.                 'it-IT' => "Autorizzato"
  24.             ),
  25.             'cancelled' => array(
  26.                 'en-GB' => "Cancelled",
  27.                 'de-DE' => "Abgebrochen",
  28.                 'it-IT' => "Annullato"
  29.             ),
  30.             'chargeback' => array(
  31.                 'en-GB' => "Chargeback",
  32.                 'de-DE' => "Rückbuchung",
  33.                 'it-IT' => "Storno"
  34.             ),
  35.             'completed' => array(
  36.                 'en-GB' => "Completed",
  37.                 'de-DE' => "Abgeschlossen",
  38.                 'it-IT' => "Completato"
  39.             ),
  40.             'failed' => array(
  41.                 'en-GB' => "Failed",
  42.                 'de-DE' => "Fehlgeschlagen",
  43.                 'it-IT' => "Fallito"
  44.             ),
  45.             'in_progress' => array(
  46.                 'en-GB' => "In Progress",
  47.                 'de-DE' => "In Bearbeitung",
  48.                 'it-IT' => "In corso"
  49.             ),
  50.             'open' => array(
  51.                 'en-GB' => "Open",
  52.                 'de-DE' => "Offen",
  53.                 'it-IT' => "Aperto"
  54.             ),
  55.             'paid' => array(
  56.                 'en-GB' => "Paid",
  57.                 'de-DE' => "Bezahlt",
  58.                 'it-IT' => "Pagato"
  59.             ),
  60.             'paid_partially' => array(
  61.                 'en-GB' => "Paid (partially)",
  62.                 'de-DE' => "Teilweise bezahlt",
  63.                 'it-IT' => "Parzialmente pagato"
  64.             ),
  65.             'reminded' => array(
  66.                 'en-GB' => "Reminded",
  67.                 'de-DE' => "Erinnert",
  68.                 'it-IT' => "Ricordato"
  69.             ),
  70.             'returned' => array(
  71.                 'en-GB' => "Returned",
  72.                 'de-DE' => "Retour",
  73.                 'it-IT' => "Restituito"
  74.             ),
  75.             'returned_partially' => array(
  76.                 'en-GB' => "Returned (partially)",
  77.                 'de-DE' => "Teilretour",
  78.                 'it-IT' => "Parzialmente restituito"
  79.             ),
  80.             'shipped' => array(
  81.                 'en-GB' => "Shipped",
  82.                 'de-DE' => "Versandt",
  83.                 'it-IT' => "Spedito"
  84.             ),
  85.             'shipped_partially' => array(
  86.                 'en-GB' => "Shipped (partially)",
  87.                 'de-DE' => "Teilweise versandt",
  88.                 'it-IT' => "Parzialmente spedito"
  89.             ),
  90.             'unconfirmed' => array(
  91.                 'en-GB' => "Unconfirmed",
  92.                 'de-DE' => "Unbestätigt",
  93.                 'it-IT' => "Non comfermato"
  94.             ),
  95.             'refunded_partially' => array(
  96.                 'en-GB' => "Refunded (partially)",
  97.                 'de-DE' => "Teilweise erstattet",
  98.                 'it-IT' => "Parzialmente rimborsato"
  99.             ),
  100.             'refunded' => array(
  101.                 'en-GB' => "Refunded",
  102.                 'de-DE' => "Erstattet",
  103.                 'it-IT' => "Rimborso"
  104.             ),
  105.             'pending' => array(
  106.                 'en-GB' => "Pending",
  107.                 'de-DE' => "Wartet",
  108.                 'it-IT' => "In attesa"
  109.             )
  110.         );
  111.         $langIds $languageIds;
  112.         foreach($allStates as $state){
  113.             foreach($langIds as $code => $langId){
  114.                 if(empty($StateTranslatedNames[$state->getTechnicalName()])){
  115.                     continue;
  116.                 }
  117.                 
  118.                 $stateMachineStateTransRepo->upsert([
  119.                     ['languageId' => $langId'stateMachineStateId' => $state->getId(), 'name' => $StateTranslatedNames[$state->getTechnicalName()][$code]]
  120.                 ], $context);
  121.             }
  122.             
  123.         }
  124.     }
  125.     private function getAllStateMachineStates(Context $context)
  126.     {
  127.         $stateMachineStateRepo $this->container->get('state_machine_state.repository');
  128.         $states $stateMachineStateRepo->search(new Criteria(), $context)->getElements();
  129.         return $states;
  130.     }
  131.     private function getLanguagesIds(Context $context)
  132.     {
  133.         $langCodes = ['en-GB''de-DE''it-IT'];
  134.         $languageRepo $this->container->get('language.repository');
  135.         $langs = array();
  136.         //get foreach langcode the language id 
  137.         foreach ($langCodes as $lang) {
  138.             $criteria = new Criteria();
  139.             $criteria->addAssociation('locale');
  140.             $criteria->addFilter(new EqualsFilter('locale.code'$lang));
  141.             $result $languageRepo->search($criteria$context);
  142.             if (($language $result->first()) != null) {
  143.                 $langs[$lang] = $language->getId();
  144.             }
  145.         }
  146.         return $langs;
  147.     }
  148. }