custom/plugins/CheckoutPlugin/src/CheckoutPlugin.php line 13

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Virgin\CheckoutPlugin;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  8. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  9. use Shopware\Core\System\CustomField\CustomFieldTypes;
  10. class CheckoutPlugin extends Plugin
  11. {
  12. public function install(InstallContext $installContext): void
  13. {
  14. parent::install($installContext);
  15. $newFields =
  16. [
  17. [
  18. 'name' => 'dynamic_product_type',
  19. 'type' => 'select',
  20. 'config' => [
  21. 'componentName' => 'sw-single-select',
  22. 'validation' => 'required',
  23. 'options' => [[
  24. 'label' => [
  25. 'en-GB' => 'Subscription carousel',
  26. ],
  27. 'value' => 'subscription', ],
  28. [
  29. 'label' => [
  30. 'en-GB' => 'Life carousel',
  31. ],
  32. 'value' => '1',
  33. ], [
  34. 'label' => [
  35. 'en-GB' => 'Premium carousel',
  36. ],
  37. 'value' => '2',
  38. ], [
  39. 'label' => [
  40. 'en-GB' => 'Premium Plus carousel',
  41. ],
  42. 'value' => '3',
  43. ], [
  44. 'label' => [
  45. 'en-GB' => 'Collection carousel',
  46. ],
  47. 'value' => '4',
  48. ],
  49. ],
  50. 'customFieldType' => 'select',
  51. 'customFieldPosition' => 1,
  52. 'label' => [
  53. 'en-GB' => 'Product type carousel',
  54. ],
  55. ],
  56. ],
  57. ];
  58. $this->addProductCustomFields($newFields);
  59. $orderFields =
  60. [
  61. [
  62. 'name' => 'payment_option',
  63. 'type' => CustomFieldTypes::TEXT,
  64. 'config' => [
  65. 'componentName' => 'sw-field',
  66. 'type' => 'text',
  67. 'customFieldType' => 'text',
  68. 'customFieldPosition' => 1,
  69. 'label' => [
  70. 'en-GB' => 'payment option',
  71. ],
  72. ],
  73. ],
  74. [
  75. 'name' => 'rid_date',
  76. 'type' => CustomFieldTypes::TEXT,
  77. 'config' => [
  78. 'componentName' => 'sw-field',
  79. 'type' => 'text',
  80. 'customFieldType' => 'text',
  81. 'customFieldPosition' => 2,
  82. 'label' => [
  83. 'en-GB' => 'rid date',
  84. ],
  85. ],
  86. ],
  87. [
  88. 'name' => 'mc_code',
  89. 'type' => CustomFieldTypes::TEXT,
  90. 'config' => [
  91. 'componentName' => 'sw-field',
  92. 'type' => 'text',
  93. 'customFieldType' => 'text',
  94. 'customFieldPosition' => 3,
  95. 'label' => [
  96. 'en-GB' => 'mc code',
  97. ],
  98. ],
  99. ],
  100. [
  101. 'name' => 'prorata_date',
  102. 'type' => CustomFieldTypes::TEXT,
  103. 'config' => [
  104. 'componentName' => 'sw-field',
  105. 'type' => 'text',
  106. 'customFieldType' => 'text',
  107. 'customFieldPosition' => 4,
  108. 'label' => [
  109. 'en-GB' => 'prorata date',
  110. ],
  111. ],
  112. ],
  113. [
  114. 'name' => 'account_holder',
  115. 'type' => CustomFieldTypes::TEXT,
  116. 'config' => [
  117. 'componentName' => 'sw-field',
  118. 'type' => 'text',
  119. 'customFieldType' => 'text',
  120. 'customFieldPosition' => 5,
  121. 'label' => [
  122. 'en-GB' => 'account holder',
  123. ],
  124. ],
  125. ],
  126. [
  127. 'name' => 'account_number',
  128. 'type' => CustomFieldTypes::TEXT,
  129. 'config' => [
  130. 'componentName' => 'sw-field',
  131. 'type' => 'text',
  132. 'customFieldType' => 'text',
  133. 'customFieldPosition' => 6,
  134. 'label' => [
  135. 'en-GB' => 'account number',
  136. ],
  137. ],
  138. ],
  139. [
  140. 'name' => 'iban',
  141. 'type' => CustomFieldTypes::TEXT,
  142. 'config' => [
  143. 'componentName' => 'sw-field',
  144. 'type' => 'text',
  145. 'customFieldType' => 'text',
  146. 'customFieldPosition' => 7,
  147. 'label' => [
  148. 'en-GB' => 'iban',
  149. ],
  150. ],
  151. ],
  152. [
  153. 'name' => 'bic',
  154. 'type' => CustomFieldTypes::TEXT,
  155. 'config' => [
  156. 'componentName' => 'sw-field',
  157. 'type' => 'text',
  158. 'customFieldType' => 'text',
  159. 'customFieldPosition' => 8,
  160. 'label' => [
  161. 'en-GB' => 'bic',
  162. ],
  163. ],
  164. ],
  165. ];
  166. $this->addOrderCustomFields($orderFields);
  167. $orderInvoiceFields =
  168. [
  169. [
  170. 'name' => 'vat_number',
  171. 'type' => CustomFieldTypes::TEXT,
  172. 'config' => [
  173. 'componentName' => 'sw-field',
  174. 'type' => 'text',
  175. 'customFieldType' => 'text',
  176. 'customFieldPosition' => 1,
  177. 'label' => [
  178. 'en-GB' => 'vat',
  179. ],
  180. ],
  181. ],
  182. [
  183. 'name' => 'company_name',
  184. 'type' => CustomFieldTypes::TEXT,
  185. 'config' => [
  186. 'componentName' => 'sw-field',
  187. 'type' => 'text',
  188. 'customFieldType' => 'text',
  189. 'customFieldPosition' => 2,
  190. 'label' => [
  191. 'en-GB' => 'company name',
  192. ],
  193. ],
  194. ],
  195. [
  196. 'name' => 'pec',
  197. 'type' => CustomFieldTypes::TEXT,
  198. 'config' => [
  199. 'componentName' => 'sw-field',
  200. 'type' => 'text',
  201. 'customFieldType' => 'text',
  202. 'customFieldPosition' => 3,
  203. 'label' => [
  204. 'en-GB' => 'pec',
  205. ],
  206. ],
  207. ],
  208. [
  209. 'name' => 'recipient_code',
  210. 'type' => CustomFieldTypes::TEXT,
  211. 'config' => [
  212. 'componentName' => 'sw-field',
  213. 'type' => 'text',
  214. 'customFieldType' => 'text',
  215. 'customFieldPosition' => 4,
  216. 'label' => [
  217. 'en-GB' => 'recipient code',
  218. ],
  219. ],
  220. ],
  221. ];
  222. $this->addInvoiceOrderCustomFields($orderInvoiceFields);
  223. }
  224. public function update(UpdateContext $updateContext): void
  225. {
  226. /*$contractPrivacyFields =
  227. [
  228. [
  229. 'name' => 'contract_privacy',
  230. 'type' => CustomFieldTypes::JSON,
  231. 'config' => [
  232. 'type' => 'json',
  233. 'customFieldPosition' => 1,
  234. 'componentName'=> 'sw-text-editor',
  235. 'customFieldType' => 'textEditor',
  236. 'label' => [
  237. 'en-GB' => 'contract and privacy',
  238. ],
  239. ],
  240. ],
  241. ];
  242. $this->addContractPrivacyFields($contractPrivacyFields);*/
  243. // $nexiResponse =
  244. // [
  245. // [
  246. // 'name' => 'nexi_response',
  247. // 'type' => CustomFieldTypes::JSON,
  248. // 'config' => [
  249. // 'type' => 'json',
  250. // 'customFieldPosition' => 10,
  251. // 'componentName'=> 'sw-text-editor',
  252. // 'customFieldType' => 'textEditor',
  253. // 'label' => [
  254. // 'en-GB' => 'nexi response params',
  255. // ],
  256. // ],
  257. // ],
  258. // [
  259. // 'name' => 'is_exported',
  260. // 'type' => CustomFieldTypes::BOOL,
  261. // 'config' => [
  262. // 'componentName' => 'sw-field',
  263. // 'type' => 'checkbox',
  264. // 'customFieldType' => 'checkbox',
  265. // 'customFieldPosition' => 9,
  266. // 'label' => [
  267. // 'en-GB' => 'order exported',
  268. // ],
  269. // ],
  270. // ]
  271. // ];
  272. $refundOption =
  273. [
  274. [
  275. 'name' => 'to_be_refund',
  276. 'type' => CustomFieldTypes::BOOL,
  277. 'config' => [
  278. 'componentName' => 'sw-field',
  279. 'type' => 'checkbox',
  280. 'customFieldType' => 'checkbox',
  281. 'customFieldPosition' => 9,
  282. 'label' => [
  283. 'en-GB' => 'to be refund',
  284. ],
  285. ],
  286. ],
  287. [
  288. 'name' => 'lead_crm_guid',
  289. 'type' => CustomFieldTypes::TEXT,
  290. 'config' => [
  291. 'componentName' => 'sw-field',
  292. 'type' => 'text',
  293. 'customFieldType' => 'text',
  294. 'customFieldPosition' => 10,
  295. 'label' => [
  296. 'en-GB' => 'lead crm guid',
  297. ],
  298. ],
  299. ],
  300. [
  301. 'name' => 'data_richiesta_mc_order',
  302. 'type' => CustomFieldTypes::DATETIME,
  303. 'config' => [
  304. 'componentName' => 'sw-field',
  305. 'time_24hr' => true,
  306. 'dateType' => 'datetime',
  307. 'customFieldType' => 'date',
  308. 'customFieldPosition' => 11,
  309. 'label' => [
  310. 'en-GB' => 'data richiesta mc order',
  311. ],
  312. ],
  313. ],
  314. ];
  315. $this->addOrderCustomFields($refundOption);
  316. $orderInvoiceFields =
  317. [
  318. [
  319. 'name' => 'account_holder_vat',
  320. 'type' => CustomFieldTypes::TEXT,
  321. 'config' => [
  322. 'componentName' => 'sw-field',
  323. 'type' => 'text',
  324. 'customFieldType' => 'text',
  325. 'customFieldPosition' => 5,
  326. 'label' => [
  327. 'en-GB' => 'account holder vat',
  328. ],
  329. ],
  330. ],
  331. [
  332. 'name' => 'address_vat',
  333. 'type' => CustomFieldTypes::TEXT,
  334. 'config' => [
  335. 'componentName' => 'sw-field',
  336. 'type' => 'text',
  337. 'customFieldType' => 'text',
  338. 'customFieldPosition' => 6,
  339. 'label' => [
  340. 'en-GB' => 'address',
  341. ],
  342. ],
  343. ],
  344. [
  345. 'name' => 'city_vat',
  346. 'type' => CustomFieldTypes::TEXT,
  347. 'config' => [
  348. 'componentName' => 'sw-field',
  349. 'type' => 'text',
  350. 'customFieldType' => 'text',
  351. 'customFieldPosition' => 7,
  352. 'label' => [
  353. 'en-GB' => 'city',
  354. ],
  355. ],
  356. ],
  357. [
  358. 'name' => 'zipcode_vat',
  359. 'type' => CustomFieldTypes::TEXT,
  360. 'config' => [
  361. 'componentName' => 'sw-field',
  362. 'type' => 'text',
  363. 'customFieldType' => 'text',
  364. 'customFieldPosition' => 8,
  365. 'label' => [
  366. 'en-GB' => 'zipcode',
  367. ],
  368. ],
  369. ],
  370. [
  371. 'name' => 'province_vat',
  372. 'type' => CustomFieldTypes::TEXT,
  373. 'config' => [
  374. 'componentName' => 'sw-field',
  375. 'type' => 'text',
  376. 'customFieldType' => 'text',
  377. 'customFieldPosition' => 9,
  378. 'label' => [
  379. 'en-GB' => 'province',
  380. ],
  381. ],
  382. ],
  383. [
  384. 'name' => 'fiscal_code',
  385. 'type' => CustomFieldTypes::TEXT,
  386. 'config' => [
  387. 'componentName' => 'sw-field',
  388. 'type' => 'text',
  389. 'customFieldType' => 'text',
  390. 'customFieldPosition' => 10,
  391. 'label' => [
  392. 'en-GB' => 'fiscal code',
  393. ],
  394. ],
  395. ],
  396. ];
  397. $this->addInvoiceOrderCustomFields($orderInvoiceFields);
  398. }
  399. private function addProductCustomFields($newProductCustomFields)
  400. {
  401. /** @var $customFieldSetRepository */
  402. $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  403. $customFieldRepository = $this->container->get('custom_field.repository');
  404. $context = Context::createDefaultContext();
  405. foreach ($newProductCustomFields as $newProductCustomField) {
  406. $criteria = new Criteria();
  407. $criteria->addFilter(
  408. new EqualsFilter('custom_field.name', $newProductCustomField['name'])
  409. );
  410. $fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
  411. if ($fieldId == null) {
  412. $createCustomFields[] = $newProductCustomField;
  413. }
  414. }
  415. if (!empty($createCustomFields)) {
  416. $customFieldSetRepository->create([
  417. [
  418. 'name' => 'product_type_carousel',
  419. 'customFields' => $createCustomFields,
  420. 'config' => [
  421. 'label' => [
  422. 'en-GB' => 'Product type carousel',
  423. ],
  424. ],
  425. 'relations' => [
  426. ['entityName' => 'product_stream'],
  427. ],
  428. ],
  429. ], $context);
  430. }
  431. }
  432. private function addOrderCustomFields($newOrderCustomFields)
  433. {
  434. /** @var $customFieldSetRepository */
  435. $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  436. $customFieldRepository = $this->container->get('custom_field.repository');
  437. $context = Context::createDefaultContext();
  438. foreach ($newOrderCustomFields as $newOrderCustomField) {
  439. $criteria = new Criteria();
  440. $criteria->addFilter(
  441. new EqualsFilter('custom_field.name', $newOrderCustomField['name'])
  442. );
  443. $fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
  444. if ($fieldId == null) {
  445. $createCustomFields[] = $newOrderCustomField;
  446. }
  447. }
  448. if (!empty($createCustomFields)) {
  449. $customFieldSetRepository->create([
  450. [
  451. 'name' => 'payment_option',
  452. 'customFields' => $createCustomFields,
  453. 'config' => [
  454. 'label' => [
  455. 'en-GB' => 'Payment option',
  456. ],
  457. ],
  458. 'relations' => [
  459. ['entityName' => 'order'],
  460. ],
  461. ],
  462. ], $context);
  463. }
  464. }
  465. private function addInvoiceOrderCustomFields($newOrderCustomFields)
  466. {
  467. /** @var $customFieldSetRepository */
  468. $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  469. $customFieldRepository = $this->container->get('custom_field.repository');
  470. $context = Context::createDefaultContext();
  471. foreach ($newOrderCustomFields as $newOrderCustomField) {
  472. $criteria = new Criteria();
  473. $criteria->addFilter(
  474. new EqualsFilter('custom_field.name', $newOrderCustomField['name'])
  475. );
  476. $fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
  477. if ($fieldId == null) {
  478. $createCustomFields[] = $newOrderCustomField;
  479. }
  480. }
  481. if (!empty($createCustomFields)) {
  482. $customFieldSetRepository->create([
  483. [
  484. 'name' => 'invoice_data',
  485. 'customFields' => $createCustomFields,
  486. 'config' => [
  487. 'label' => [
  488. 'en-GB' => 'Invoice data',
  489. ],
  490. ],
  491. 'relations' => [
  492. ['entityName' => 'order'],
  493. ],
  494. ],
  495. ], $context);
  496. }
  497. }
  498. private function addContractPrivacyFields($newOrderCustomFields)
  499. {
  500. /** @var $customFieldSetRepository */
  501. $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  502. $customFieldRepository = $this->container->get('custom_field.repository');
  503. $context = Context::createDefaultContext();
  504. foreach ($newOrderCustomFields as $newOrderCustomField) {
  505. $criteria = new Criteria();
  506. $criteria->addFilter(
  507. new EqualsFilter('custom_field.name', $newOrderCustomField['name'])
  508. );
  509. $fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
  510. if ($fieldId == null) {
  511. $createCustomFields[] = $newOrderCustomField;
  512. }
  513. }
  514. if (!empty($createCustomFields)) {
  515. $customFieldSetRepository->create([
  516. [
  517. 'name' => 'contract_privacy',
  518. 'customFields' => $createCustomFields,
  519. 'config' => [
  520. 'label' => [
  521. 'en-GB' => 'Contract and privacy',
  522. ],
  523. ],
  524. 'relations' => [
  525. ['entityName' => 'order'],
  526. ],
  527. ],
  528. ], $context);
  529. }
  530. }
  531. }