custom/plugins/SystemIntegration/src/SystemIntegration.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Virgin\SystemIntegration;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\System\CustomField\CustomFieldTypes;
  10. use Shopware\Core\Framework\Context;
  11. class SystemIntegration extends Plugin
  12. {
  13. public function update(UpdateContext $installContext): void
  14. {
  15. /* array definizione custom field */
  16. /*$newFields =
  17. [
  18. [
  19. 'name' => 'virgin_user_type_code',
  20. 'type' => CustomFieldTypes::TEXT,
  21. 'config' => [
  22. 'componentName' => 'sw-field',
  23. 'type' => 'text',
  24. 'customFieldType' => 'text',
  25. 'customFieldPosition' => 1,
  26. 'label' => [
  27. 'en-GB' => 'virgin type',
  28. ],
  29. ],
  30. ],
  31. ];*/
  32. /* funzione che verifica esistenza dei custom fields,
  33. * li associa ad un set di customfield
  34. * e mette in relazione il set con l'entity customer group
  35. */
  36. /*$this->addCustomerCustomFields($newFields);
  37. $this->CreateFiled('LEAD','0');
  38. $this->CreateFiled('SOCIO','1');
  39. $this->CreateFiled('EXSOCIO','2');
  40. $this->CreateFiled('INSOLUTO','3');
  41. $this->CreateFiled('BLACKLIST','4');
  42. $this->CreateFiled('DROP ECCEZIONE','5');
  43. $this->CreateFiled('NON DISPONIBILE','6');*/
  44. /* array definition custom field */
  45. /*$newProductFields =
  46. [
  47. [
  48. 'name' => 'virgin_exerp_productid',
  49. 'type' => CustomFieldTypes::INT,
  50. 'config' => [
  51. 'componentName' => 'sw-field',
  52. 'type' => 'number',
  53. 'customFieldType' => 'number',
  54. 'numbertype'=> 'int',
  55. 'customFieldPosition' => 1,
  56. 'label' => [
  57. 'en-GB' => 'virgin exerp product id',
  58. ],
  59. ],
  60. ],
  61. [
  62. 'name' => 'virgin_exerp_centerid',
  63. 'type' => CustomFieldTypes::INT,
  64. 'config' => [
  65. 'componentName' => 'sw-field',
  66. 'type' => 'number',
  67. 'numbertype'=> 'int',
  68. 'customFieldType' => "number",
  69. 'customFieldPosition' => 2,
  70. 'label' => [
  71. 'en-GB' => 'virgin exerp center id',
  72. ],
  73. ],
  74. ],
  75. [
  76. 'name' => 'virgin_exerp_type',
  77. 'type' => CustomFieldTypes::TEXT,
  78. 'config' => [
  79. 'componentName' => 'sw-field',
  80. 'type' => 'text',
  81. 'customFieldType' => 'text',
  82. 'customFieldPosition' => 3,
  83. 'label' => [
  84. 'en-GB' => 'virgin exerp pagament type',
  85. ],
  86. ],
  87. ],
  88. [
  89. 'name' => 'virgin_exerp_name',
  90. 'type' => CustomFieldTypes::TEXT,
  91. 'config' => [
  92. 'componentName' => 'sw-field',
  93. 'type' => 'text',
  94. 'customFieldType' => 'text',
  95. 'customFieldPosition' => 4,
  96. 'label' => [
  97. 'en-GB' => 'virgin exerp product Name',
  98. ],
  99. ],
  100. ],
  101. [
  102. 'name' => 'eft_cash_association_code',
  103. 'type' => CustomFieldTypes::TEXT,
  104. 'config' => [
  105. 'componentName' => 'sw-field',
  106. 'type' => 'text',
  107. 'customFieldType' => 'text',
  108. 'customFieldPosition' => 5,
  109. 'label' => [
  110. 'en-GB' => 'product number eft/cash associated product',
  111. ],
  112. ],
  113. ],
  114. [
  115. 'name' => 'virgin_exerp_externalid',
  116. 'type' => CustomFieldTypes::TEXT,
  117. 'config' => [
  118. 'componentName' => 'sw-field',
  119. 'type' => 'text',
  120. 'customFieldType' => 'text',
  121. 'customFieldPosition' => 5,
  122. 'label' => [
  123. 'en-GB' => 'virgin exerp external id (reference)',
  124. ],
  125. ],
  126. ],
  127. ];*/
  128. $newProductFields = [
  129. [
  130. 'name' => 'virgin_exerp_externalid',
  131. 'type' => CustomFieldTypes::TEXT,
  132. 'config' => [
  133. 'componentName' => 'sw-field',
  134. 'type' => 'text',
  135. 'customFieldType' => 'text',
  136. 'customFieldPosition' => 5,
  137. 'label' => [
  138. 'en-GB' => 'virgin exerp external id (reference)',
  139. ],
  140. ],
  141. ],
  142. ];
  143. $this->updateProductCustomFields($newProductFields, 'virgin_product_attributes', 'virgin attributes', 'product');
  144. }
  145. private function CreateFiled($name,$value){
  146. /** esempio di creazione di un customer group da codice */
  147. /** TODO FARE IN MODO CHE NON AGGIUNGA NULLA SE GIÀ PRESENTE A DB */
  148. $context = Context::createDefaultContext();
  149. $customerGroupRepository = $this->container->get('customer_group.repository');
  150. $customerGroupId = Uuid::randomHex();
  151. $customerGroupRepository->create(
  152. [
  153. [
  154. 'id' => $customerGroupId,
  155. 'name' => $name,
  156. ],
  157. ],
  158. $context
  159. );
  160. /** update del valore del custom field del customer group appena creato */
  161. $customerGroupRepository->upsert([[
  162. 'id' => $customerGroupId,
  163. 'customFields' => ['virgin_user_type_code' => $value]
  164. ]], $context);
  165. }
  166. // creazione custom fields set
  167. private function addCustomerCustomFields($newCustomerCustomFields)
  168. {
  169. /* @var $customFieldSetRepository */
  170. $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  171. $customFieldRepository = $this->container->get('custom_field.repository');
  172. $context = Context::createDefaultContext();
  173. foreach ($newCustomerCustomFields as $newCustomerCustomField) {
  174. $criteria = new Criteria();
  175. $criteria->addFilter(
  176. new EqualsFilter('custom_field.name', $newCustomerCustomField['name'])
  177. );
  178. $fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
  179. if ($fieldId == null) {
  180. $createCustomFields[] = $newCustomerCustomField;
  181. }
  182. }
  183. if (!empty($createCustomFields)) {
  184. $customFieldSetRepository->create([
  185. [
  186. 'name' => 'virgin_customergroup_attributes',
  187. 'customFields' => $createCustomFields,
  188. 'config' => [
  189. 'label' => [
  190. 'en-GB' => 'virgin attributes',
  191. ],
  192. ],
  193. 'relations' => [
  194. ['entityName' => 'customer_group'],
  195. ],
  196. ],
  197. ], $context);
  198. }
  199. }
  200. private function updateProductCustomFields($newProductCustomFields, $setName, $label, $entity)
  201. {
  202. /** @var EntityRepositoryInterface $customFieldSetRepository */
  203. $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  204. $customFieldRepository = $this->container->get('custom_field.repository');
  205. $context = Context::createDefaultContext();
  206. foreach ($newProductCustomFields as $newProductCustomField) {
  207. $criteria = new Criteria();
  208. $criteria->addFilter(
  209. new EqualsFilter('custom_field.name', $newProductCustomField['name'])
  210. );
  211. $fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
  212. if ($fieldId == null) {
  213. $createCustomFields[] = $newProductCustomField;
  214. }
  215. }
  216. $criteria = new Criteria();
  217. $criteria->addFilter(
  218. new EqualsFilter('name', $setName)
  219. );
  220. $customFieldSetId = $customFieldSetRepository->searchIds($criteria, $context)->firstId();
  221. if (!empty($createCustomFields)) {
  222. if (null == $customFieldSetId) {
  223. $customFieldSetRepository->create([
  224. [
  225. 'name' => $setName,
  226. 'customFields' => $createCustomFields,
  227. 'config' => [
  228. 'label' => [
  229. 'en-GB' => $label,
  230. ],
  231. ],
  232. 'relations' => [
  233. ['entityName' => $entity],
  234. ],
  235. ],
  236. ], $context);
  237. } else {
  238. $customFieldSetRepository->update(
  239. [
  240. [
  241. 'id' => $customFieldSetId,
  242. 'customFields' => $createCustomFields
  243. ]
  244. ], $context);
  245. }
  246. }
  247. }
  248. // creazione Custom Fields per Prodotto
  249. private function addProductCustomFields($newCustomerCustomFields)
  250. {
  251. /* @var $customFieldSetRepository */
  252. $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  253. $customFieldRepository = $this->container->get('custom_field.repository');
  254. $context = Context::createDefaultContext();
  255. foreach ($newCustomerCustomFields as $newCustomerCustomField) {
  256. $criteria = new Criteria();
  257. $criteria->addFilter(
  258. new EqualsFilter('custom_field.name', $newCustomerCustomField['name'])
  259. );
  260. $fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
  261. if ($fieldId == null) {
  262. $createCustomFields[] = $newCustomerCustomField;
  263. }
  264. }
  265. if (!empty($createCustomFields)) {
  266. $customFieldSetRepository->create([
  267. [
  268. 'name' => 'virgin_product_attributes',
  269. 'customFields' => $createCustomFields,
  270. 'config' => [
  271. 'label' => [
  272. 'en-GB' => 'virgin attributes',
  273. ],
  274. ],
  275. 'relations' => [
  276. ['entityName' => 'product'],
  277. ],
  278. ],
  279. ], $context);
  280. }
  281. }
  282. }