vendor/store.shopware.com/zeobvabandonedcart/src/ZeobvAbandonedCart.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Zeobv\AbandonedCart;
  3. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  10. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  11. use Zeobv\AbandonedCart\Checkout\AbandonedCart\AbandonedCartDefinition;
  12. class ZeobvAbandonedCart extends Plugin
  13. {
  14.     const MAIL_TEMPLATE_REMINDER_NAME 'abandoned_cart.reminder';
  15.     public function install(InstallContext $installContext): void
  16.     {
  17.         $mailTemplateRepository $this->container->get('mail_template.repository');
  18.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  19.         $criteria = new Criteria();
  20.         $criteria->addFilter(new EqualsFilter('technicalName'self::MAIL_TEMPLATE_REMINDER_NAME));
  21.         $templateType $mailTemplateTypeRepository->search($criteria$installContext->getContext())->first();
  22.         if ( $templateType instanceof MailTemplateTypeEntity) {
  23.             return;
  24.         }
  25.         $mailTemplateRepository->create([
  26.             [
  27.                 'systemDefault' => false,
  28.                 'translations' => [
  29.                     [
  30.                         'languageId' => Defaults::LANGUAGE_SYSTEM,
  31.                         'subject' => 'Your cart is waiting for you',
  32.                         'description' => 'Mail template send to customer who abandoned a shopping cart for longer than the configured time.',
  33.                         'senderName' => '{{ salesChannel.name }}',
  34.                         'contentPlain' => 'Your cart is waiting for you. Use the following link to go to checkout {{ url(\'frontend.zeo.abandonedcart.recover\', {id: zeoAbandonedCart.id}) }}',
  35.                         'contentHtml' => '<h2>Your cart is waiting for you.</h2>
  36. <p>Click the following link to proceed to checkout.<br/>
  37.     <a href="{{ url(\'frontend.zeo.abandonedcart.recover\', {id: zeoAbandonedCart.id}) }}">
  38.         <button>Go to checkout</button>
  39.     </a>
  40. </p>'
  41.                     ]
  42.                 ],
  43.                 'mailTemplateType' => [
  44.                     'technicalName' => self::MAIL_TEMPLATE_REMINDER_NAME,
  45.                     'availableEntities' => [
  46.                         'zeoAbandonedCart' => 'zeo_abandoned_cart',
  47.                         'salesChannel' => 'sales_channel',
  48.                         'customer' => 'customer',
  49.                         'cart' => 'cart'
  50.                     ],
  51.                     'templateData' => $this->getTemplateData(),
  52.                     'translations' => [
  53.                         [
  54.                             'languageId' => Defaults::LANGUAGE_SYSTEM,
  55.                             'name' => 'Enter abandoned cart state: Reminded'
  56.                         ],
  57.                     ]
  58.                 ]
  59.             ]
  60.         ], $installContext->getContext());
  61.     }
  62.     public function uninstall(UninstallContext $uninstallContext): void
  63.     {
  64.         if ($uninstallContext->keepUserData()) {
  65.             return;
  66.         }
  67.         $connection $this->container->get('Doctrine\DBAL\Connection');
  68.         $abandonedCartsTable AbandonedCartDefinition::ENTITY_NAME;
  69.         $connection->exec("DROP TABLE IF EXISTS $abandonedCartsTable");
  70.         $mailTemplateRepository $this->container->get('mail_template.repository');
  71.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  72.         $criteria = new Criteria();
  73.         $criteria->addAssociation('mailTemplateType');
  74.         $criteria->addFilter(new EqualsFilter('mailTemplateType.technicalName'self::MAIL_TEMPLATE_REMINDER_NAME));
  75.         $templates $mailTemplateRepository->search($criteria$uninstallContext->getContext());
  76.         if ($templates->count() <= 0) {
  77.             return;
  78.         }
  79.         $mailTemplateIds = [];
  80.         $mailTemplateTypeIds = [];
  81.         /** @var MailTemplateEntity $mailTemplate */
  82.         foreach ($templates->getElements() as $mailTemplate) {
  83.             $mailTemplateIds[] = ['id' => $mailTemplate->getId()];
  84.             if (!in_array($mailTemplate->getMailTemplateTypeId(), $mailTemplateTypeIds)) {
  85.                 $mailTemplateTypeIds[] = ['id' => $mailTemplate->getMailTemplateTypeId()];
  86.             }
  87.         }
  88.         if (!empty($mailTemplateIds)) {
  89.             $mailTemplateRepository->delete($mailTemplateIds$uninstallContext->getContext());
  90.         }
  91.         if (!empty($mailTemplateTypeIds)) {
  92.             $mailTemplateTypeRepository->delete($mailTemplateTypeIds$uninstallContext->getContext());
  93.         }
  94.         $customFieldName "ZeobvAbandonedCartMail";
  95.         $query "UPDATE `order` SET `custom_fields` = JSON_REMOVE(`custom_fields`, '$.$customFieldName');";
  96.         $connection->executeStatement($query);
  97.         parent::uninstall($uninstallContext);
  98.     }
  99.     private function getTemplateData(): array
  100.     {
  101.         return [
  102.             'zeoAbandonedCart' => [
  103.                 "id" => "0x4805CAC305EE4E9D994F7E986AE361B3",
  104.                 "cartToken" => "yc3vn9MHseLx7dFxDo8iHMJ7duoBsG51",
  105.                 "email" => "example@gmail.nl",
  106.                 "lastMailSendAt" => null,
  107.                 "scheduleIndex" => 0,
  108.                 "lineItems" => [
  109.                     "0f134719b07c4bb8873c7ccbc1500ee8" => [
  110.                         "id" => "0f134719b07c4bb8873c7ccbc1500ee8",
  111.                         "good" => true,
  112.                         "type" => "product",
  113.                         "cover" => null,
  114.                         "label" => "Example product",
  115.                         "price" => [
  116.                             "quantity" => 1,
  117.                             "taxRules" => [
  118.                                 ["taxRate" => 21"extensions" => [], "percentage" => 100]
  119.                             ],
  120.                             "listPrice" => null,
  121.                             "unitPrice" => 223.99,
  122.                             "extensions" => [],
  123.                             "totalPrice" => 223.99,
  124.                             "referencePrice" => null,
  125.                             "calculatedTaxes" => [
  126.                                 [
  127.                                     "tax" => 38.87,
  128.                                     "price" => 223.99,
  129.                                     "taxRate" => 21,
  130.                                     "extensions" => []
  131.                                 ]
  132.                             ]
  133.                         ],
  134.                         "payload" => [
  135.                             "isNew" => false,
  136.                             "taxId" => "f5bc0df0172c4c88b98b20e54733f3e2",
  137.                             "tagIds" => null,
  138.                             "options" => [],
  139.                             "features" => [],
  140.                             "createdAt" => "2021-09-02 07:07:23.515",
  141.                             "optionIds" => null,
  142.                             "isCloseout" => false,
  143.                             "categoryIds" => [
  144.                                 "17aa7e210b2f44dd917cb69147536e89"
  145.                             ],
  146.                             "propertyIds" => null,
  147.                             "releaseDate" => null,
  148.                             "customFields" => [],
  149.                             "productNumber" => "SW10000",
  150.                             "manufacturerId" => "d857fa1a0e9549ce8de86b2536f278fc",
  151.                             "purchasePrices" => [
  152.                                 "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca",
  153.                                 "net" =>0,
  154.                                 "gross" =>0,
  155.                                 "linked" =>true,
  156.                                 "listPrice" =>null,
  157.                                 "extensions" =>[]
  158.                             ],
  159.                             "markAsTopseller" => null
  160.                         ],
  161.                         "children" => [],
  162.                         "modified" => false,
  163.                         "quantity" => 1,
  164.                         "removable" => true,
  165.                         "stackable" => true,
  166.                         "extensions" => [],
  167.                         "description" => null,
  168.                         "requirement" => null,
  169.                         "referencedId" => "0f134719b07c4bb8873c7ccbc1500ee8",
  170.                         "dataTimestamp" => "2021-09-12T07:13:04.631+00:00",
  171.                         "dataContextHash" => "7116e36cd0356dd78186992022f6366e",
  172.                         "priceDefinition" => [
  173.                             "type" => "quantity",
  174.                             "price" => 223.99,
  175.                             "quantity" => 1,
  176.                             "taxRules" => [
  177.                                 [
  178.                                     "taxRate" => 21,
  179.                                     "extensions" => [],
  180.                                     "percentage" => 100
  181.                                 ]
  182.                             ],
  183.                             "listPrice" => null,
  184.                             "extensions" => [],
  185.                             "isCalculated" => true,
  186.                             "referencePriceDefinition" => null
  187.                         ],
  188.                         "deliveryInformation" => [
  189.                             "stock" => 111,
  190.                             "width" => null,
  191.                             "height" => null,
  192.                             "length" => null,
  193.                             "weight" => 0,
  194.                             "extensions" => [],
  195.                             "restockTime" => null,
  196.                             "deliveryTime" => null,
  197.                             "freeDelivery" => false
  198.                         ],
  199.                         "quantityInformation" => [
  200.                             "extensions" => [],
  201.                             "maxPurchase" => 100,
  202.                             "minPurchase" => 1,
  203.                             "purchaseSteps" => 1
  204.                         ]
  205.                     ]
  206.                 ],
  207.                 "currencyId" => "0xB7D2554B0CE847CD82F3AC9BD1C0DFCA",
  208.                 "shippingMethodId" => "0x758ABD1C272A4979A4D4FAFA20A57D0C",
  209.                 "paymentMethodId" => "0x589863A7ED6C449ABD24D793E31D5EF6",
  210.                 "countryId" => "0x555534E7E0C646858866D5DC3D93813B",
  211.                 "customerId" => "0xA7E16507214040A48963F6E91463D758",
  212.                 "salesChannelId" => "0x197D1D8975F74A6EAC7270852DFAB90C",
  213.                 "salesChannelDomainId" => "0x36495AB4470241E59D075D663F1AB6E3",
  214.                 "createdAt" => "2021-09-12 07:03:36.377",
  215.                 "updatedAt" => null
  216.             ],
  217.             'salesChannel' => [
  218.                 "typeId" => "8a243080f92e4c719546314b577cf82b",
  219.                 "languageId" => "2fbb5fe2e29a4d70aa5854ce7ce3e20b",
  220.                 "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca",
  221.                 "paymentMethodId" => "11491d8f829143c9a1f15c9c55e3df0c",
  222.                 "shippingMethodId" => "03168af91f804087b7bc24eea072c2d6",
  223.                 "countryId" => "36159d966ed844e6add50a0a370c99b2",
  224.                 "navigationCategoryId" => "aee8846cc9214295bd832fc436ff0891",
  225.                 "navigationCategoryDepth" => 2,
  226.                 "homeSlotConfig" => null,
  227.                 "homeCmsPageId" => null,
  228.                 "homeCmsPage" => null,
  229.                 "homeEnabled" => null,
  230.                 "homeName" => null,
  231.                 "homeMetaTitle" => null,
  232.                 "homeMetaDescription" => null,
  233.                 "homeKeywords" => null,
  234.                 "footerCategoryId" => null,
  235.                 "serviceCategoryId" => null,
  236.                 "name" => "Storefront",
  237.                 "shortName" => null,
  238.                 "accessKey" => "SWSCATLQZHBWBVZ1SGHNQNBLAQ",
  239.                 "currencies" => null,
  240.                 "languages" => null,
  241.                 "configuration" => null,
  242.                 "active" => true,
  243.                 "maintenance" => false,
  244.                 "maintenanceIpWhitelist" => null,
  245.                 "taxCalculationType" => "horizontal",
  246.                 "type" => null,
  247.                 "currency" => null,
  248.                 "language" => null,
  249.                 "paymentMethod" => null,
  250.                 "shippingMethod" => null,
  251.                 "country" => null,
  252.                 "orders" => null,
  253.                 "customers" => null,
  254.                 "countries" => null,
  255.                 "paymentMethods" => null,
  256.                 "shippingMethods" => null,
  257.                 "translations" => null,
  258.                 "domains" => [
  259.                     [
  260.                         "url" => "http => \/\/localhost\/development\/public",
  261.                         "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca",
  262.                         "currency" => null,
  263.                         "snippetSetId" => "765a7f1059ee4f75a4592194291d1e1e",
  264.                         "snippetSet" => null,
  265.                         "salesChannelId" => "6d5b12a8049e411cb463296092b0e887",
  266.                         "salesChannel" => null,
  267.                         "languageId" => "2fbb5fe2e29a4d70aa5854ce7ce3e20b",
  268.                         "language" => null,
  269.                         "customFields" => null,
  270.                         "productExports" => null,
  271.                         "salesChannelDefaultHreflang" => null,
  272.                         "hreflangUseOnlyLocale" => false,
  273.                         "_uniqueIdentifier" => "7ed2b68f70284aea83d1c2747b622848",
  274.                         "versionId" => null,
  275.                         "translated" => [
  276.                         ],
  277.                         "createdAt" => "2021-04-08T07 => 30 => 38.224+00 => 00",
  278.                         "updatedAt" => null,
  279.                         "extensions" => [
  280.                             "foreignKeys" => [
  281.                                 "apiAlias" => null,
  282.                                 "extensions" => [
  283.                                 ]
  284.                             ]
  285.                         ],
  286.                         "id" => "7ed2b68f70284aea83d1c2747b622848"
  287.                     ]
  288.                 ],
  289.                 "systemConfigs" => null,
  290.                 "customFields" => null,
  291.                 "navigationCategory" => null,
  292.                 "footerCategory" => null,
  293.                 "serviceCategory" => null,
  294.                 "productVisibilities" => null,
  295.                 "mailHeaderFooterId" => null,
  296.                 "numberRangeSalesChannels" => null,
  297.                 "mailHeaderFooter" => null,
  298.                 "customerGroupId" => "cfbd5018d38d41d8adca10d94fc8bdd6",
  299.                 "customerGroup" => null,
  300.                 "newsletterRecipients" => null,
  301.                 "promotionSalesChannels" => null,
  302.                 "documentBaseConfigSalesChannels" => null,
  303.                 "productReviews" => null,
  304.                 "seoUrls" => null,
  305.                 "seoUrlTemplates" => null,
  306.                 "mainCategories" => null,
  307.                 "paymentMethodIds" => [
  308.                     "02f698f2ca24408bbbd40370ac95275d",
  309.                     "11491d8f829143c9a1f15c9c55e3df0c",
  310.                     "68cd624bbe2946f4afbdc1ae2f389e0e",
  311.                     "e226a70069af441f965e79459acccb41"
  312.                 ],
  313.                 "productExports" => null,
  314.                 "hreflangActive" => false,
  315.                 "hreflangDefaultDomainId" => null,
  316.                 "hreflangDefaultDomain" => null,
  317.                 "analyticsId" => null,
  318.                 "analytics" => null,
  319.                 "customerGroupsRegistrations" => null,
  320.                 "eventActions" => null,
  321.                 "boundCustomers" => null,
  322.                 "wishlists" => null,
  323.                 "landingPages" => null,
  324.                 "_uniqueIdentifier" => "6d5b12a8049e411cb463296092b0e887",
  325.                 "versionId" => null,
  326.                 "translated" => [
  327.                     "name" => "Storefront",
  328.                     "customFields" => []
  329.                 ],
  330.                 "createdAt" => "2021-04-08T07:30:38.224+00:00",
  331.                 "updatedAt" => null,
  332.                 "extensions" => [
  333.                     "foreignKeys" => [
  334.                         "apiAlias" => null,
  335.                         "extensions" => [
  336.                         ]
  337.                     ]
  338.                 ],
  339.                 "id" => "6d5b12a8049e411cb463296092b0e887",
  340.                 "navigationCategoryVersionId" => "0fa91ce3e96a4bc2be4bd9ce752c3425",
  341.                 "footerCategoryVersionId" => null,
  342.                 "serviceCategoryVersionId" => null
  343.             ],
  344.             'customer' => [
  345.                 "id" => "ca87efbfbd2f23efbfbd4742efbfbd11",
  346.                 "autoIncrement" => 3,
  347.                 "customerGroupId" => "1323efbfbd32774576efbfbd1636efbf",
  348.                 "requestedCustomerGroupId" => null,
  349.                 "defaultPaymentMethodId" => null,
  350.                 "salesChannelId" => "6d5b12a8049e411cb463296092b0e887",
  351.                 "languageId" => "2fbb5fe2e29a4d70aa5854ce7ce3e20b",
  352.                 "lastPaymentMethodId" => null,
  353.                 "defaultBillingAddressId" => "68efbfbd58c2be034cefbfbdefbfbd56",
  354.                 "defaultShippingAddressId" => "68efbfbd58c2be034cefbfbdefbfbd56",
  355.                 "customerNumber" => "10000",
  356.                 "salutationId" => "efbfbd76efbfbdd1a73b4b1cefbfbdef",
  357.                 "firstName" => "John",
  358.                 "lastName" => "Doe",
  359.                 "company" => 'Shopware AG.',
  360.                 "password" => '$2y$10$Of2prbDQtxqFFjdb.8pXde6K9BqYyUGblqlJrwM67/E9WVjFPmZH2',
  361.                 "email" => "example@mail.com",
  362.                 "title" => null,
  363.                 "vatIds" => ['NL123456789B01'],
  364.                 "active" => 1,
  365.                 "guest" => 0,
  366.                 "firstLogin" => "2021-10-19 12:39:23.593",
  367.                 "lastLogin" => null,
  368.                 "newsletterSalesChannelIds" => null,
  369.                 "newsletter" => 0,
  370.                 "birthday" => null,
  371.                 "lastOrderDate" => null,
  372.                 "orderCount" => 1,
  373.                 "orderTotalAmount" => 1234.95,
  374.                 "customFields" => null,
  375.                 "affiliateCode" => 'affiliate-code-example',
  376.                 "campaignCode" => 'campaign-code-example',
  377.                 "createdAt" => "2021-10-19 12:39:23.641",
  378.                 "updatedAt" => "2021-12-15 11:07:53.759",
  379.                 "remoteAddress" => "0.0.0.0",
  380.                 "tagIds" => null,
  381.             ],
  382.         ];
  383.     }
  384. }