<?php declare(strict_types=1);
namespace Virgin\CheckoutPlugin;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class CheckoutPlugin extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$newFields =
[
[
'name' => 'dynamic_product_type',
'type' => 'select',
'config' => [
'componentName' => 'sw-single-select',
'validation' => 'required',
'options' => [[
'label' => [
'en-GB' => 'Subscription carousel',
],
'value' => 'subscription', ],
[
'label' => [
'en-GB' => 'Life carousel',
],
'value' => '1',
], [
'label' => [
'en-GB' => 'Premium carousel',
],
'value' => '2',
], [
'label' => [
'en-GB' => 'Premium Plus carousel',
],
'value' => '3',
], [
'label' => [
'en-GB' => 'Collection carousel',
],
'value' => '4',
],
],
'customFieldType' => 'select',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Product type carousel',
],
],
],
];
$this->addProductCustomFields($newFields);
$orderFields =
[
[
'name' => 'payment_option',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'payment option',
],
],
],
[
'name' => 'rid_date',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 2,
'label' => [
'en-GB' => 'rid date',
],
],
],
[
'name' => 'mc_code',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 3,
'label' => [
'en-GB' => 'mc code',
],
],
],
[
'name' => 'prorata_date',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 4,
'label' => [
'en-GB' => 'prorata date',
],
],
],
[
'name' => 'account_holder',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 5,
'label' => [
'en-GB' => 'account holder',
],
],
],
[
'name' => 'account_number',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 6,
'label' => [
'en-GB' => 'account number',
],
],
],
[
'name' => 'iban',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 7,
'label' => [
'en-GB' => 'iban',
],
],
],
[
'name' => 'bic',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 8,
'label' => [
'en-GB' => 'bic',
],
],
],
];
$this->addOrderCustomFields($orderFields);
$orderInvoiceFields =
[
[
'name' => 'vat_number',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'vat',
],
],
],
[
'name' => 'company_name',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 2,
'label' => [
'en-GB' => 'company name',
],
],
],
[
'name' => 'pec',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 3,
'label' => [
'en-GB' => 'pec',
],
],
],
[
'name' => 'recipient_code',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 4,
'label' => [
'en-GB' => 'recipient code',
],
],
],
];
$this->addInvoiceOrderCustomFields($orderInvoiceFields);
}
public function update(UpdateContext $updateContext): void
{
/*$contractPrivacyFields =
[
[
'name' => 'contract_privacy',
'type' => CustomFieldTypes::JSON,
'config' => [
'type' => 'json',
'customFieldPosition' => 1,
'componentName'=> 'sw-text-editor',
'customFieldType' => 'textEditor',
'label' => [
'en-GB' => 'contract and privacy',
],
],
],
];
$this->addContractPrivacyFields($contractPrivacyFields);*/
// $nexiResponse =
// [
// [
// 'name' => 'nexi_response',
// 'type' => CustomFieldTypes::JSON,
// 'config' => [
// 'type' => 'json',
// 'customFieldPosition' => 10,
// 'componentName'=> 'sw-text-editor',
// 'customFieldType' => 'textEditor',
// 'label' => [
// 'en-GB' => 'nexi response params',
// ],
// ],
// ],
// [
// 'name' => 'is_exported',
// 'type' => CustomFieldTypes::BOOL,
// 'config' => [
// 'componentName' => 'sw-field',
// 'type' => 'checkbox',
// 'customFieldType' => 'checkbox',
// 'customFieldPosition' => 9,
// 'label' => [
// 'en-GB' => 'order exported',
// ],
// ],
// ]
// ];
$refundOption =
[
[
'name' => 'to_be_refund',
'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-field',
'type' => 'checkbox',
'customFieldType' => 'checkbox',
'customFieldPosition' => 9,
'label' => [
'en-GB' => 'to be refund',
],
],
],
[
'name' => 'lead_crm_guid',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 10,
'label' => [
'en-GB' => 'lead crm guid',
],
],
],
[
'name' => 'data_richiesta_mc_order',
'type' => CustomFieldTypes::DATETIME,
'config' => [
'componentName' => 'sw-field',
'time_24hr' => true,
'dateType' => 'datetime',
'customFieldType' => 'date',
'customFieldPosition' => 11,
'label' => [
'en-GB' => 'data richiesta mc order',
],
],
],
];
$this->addOrderCustomFields($refundOption);
$orderInvoiceFields =
[
[
'name' => 'account_holder_vat',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 5,
'label' => [
'en-GB' => 'account holder vat',
],
],
],
[
'name' => 'address_vat',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 6,
'label' => [
'en-GB' => 'address',
],
],
],
[
'name' => 'city_vat',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 7,
'label' => [
'en-GB' => 'city',
],
],
],
[
'name' => 'zipcode_vat',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 8,
'label' => [
'en-GB' => 'zipcode',
],
],
],
[
'name' => 'province_vat',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 9,
'label' => [
'en-GB' => 'province',
],
],
],
[
'name' => 'fiscal_code',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 10,
'label' => [
'en-GB' => 'fiscal code',
],
],
],
];
$this->addInvoiceOrderCustomFields($orderInvoiceFields);
}
private function addProductCustomFields($newProductCustomFields)
{
/** @var $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldRepository = $this->container->get('custom_field.repository');
$context = Context::createDefaultContext();
foreach ($newProductCustomFields as $newProductCustomField) {
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('custom_field.name', $newProductCustomField['name'])
);
$fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
if ($fieldId == null) {
$createCustomFields[] = $newProductCustomField;
}
}
if (!empty($createCustomFields)) {
$customFieldSetRepository->create([
[
'name' => 'product_type_carousel',
'customFields' => $createCustomFields,
'config' => [
'label' => [
'en-GB' => 'Product type carousel',
],
],
'relations' => [
['entityName' => 'product_stream'],
],
],
], $context);
}
}
private function addOrderCustomFields($newOrderCustomFields)
{
/** @var $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldRepository = $this->container->get('custom_field.repository');
$context = Context::createDefaultContext();
foreach ($newOrderCustomFields as $newOrderCustomField) {
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('custom_field.name', $newOrderCustomField['name'])
);
$fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
if ($fieldId == null) {
$createCustomFields[] = $newOrderCustomField;
}
}
if (!empty($createCustomFields)) {
$customFieldSetRepository->create([
[
'name' => 'payment_option',
'customFields' => $createCustomFields,
'config' => [
'label' => [
'en-GB' => 'Payment option',
],
],
'relations' => [
['entityName' => 'order'],
],
],
], $context);
}
}
private function addInvoiceOrderCustomFields($newOrderCustomFields)
{
/** @var $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldRepository = $this->container->get('custom_field.repository');
$context = Context::createDefaultContext();
foreach ($newOrderCustomFields as $newOrderCustomField) {
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('custom_field.name', $newOrderCustomField['name'])
);
$fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
if ($fieldId == null) {
$createCustomFields[] = $newOrderCustomField;
}
}
if (!empty($createCustomFields)) {
$customFieldSetRepository->create([
[
'name' => 'invoice_data',
'customFields' => $createCustomFields,
'config' => [
'label' => [
'en-GB' => 'Invoice data',
],
],
'relations' => [
['entityName' => 'order'],
],
],
], $context);
}
}
private function addContractPrivacyFields($newOrderCustomFields)
{
/** @var $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldRepository = $this->container->get('custom_field.repository');
$context = Context::createDefaultContext();
foreach ($newOrderCustomFields as $newOrderCustomField) {
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('custom_field.name', $newOrderCustomField['name'])
);
$fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
if ($fieldId == null) {
$createCustomFields[] = $newOrderCustomField;
}
}
if (!empty($createCustomFields)) {
$customFieldSetRepository->create([
[
'name' => 'contract_privacy',
'customFields' => $createCustomFields,
'config' => [
'label' => [
'en-GB' => 'Contract and privacy',
],
],
'relations' => [
['entityName' => 'order'],
],
],
], $context);
}
}
}