<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
declare(strict_types=1);
namespace scha1\scha1HidePriceInProducts;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Doctrine\DBAL\Connection;
/**
*
*/
class scha1HidePriceInProducts extends Plugin {
/**
*
* @param \scha1\scha1HidePriceInProducts\UninstallContext $uninstallContext
*/
public function uninstall(UninstallContext $uninstallContext): void {
/**
*
*/
if ($uninstallContext->keepUserData()) {
parent::uninstall($uninstallContext);
return;
}
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
//price
$scha1_showprice = new Criteria();
$scha1_showprice->addFilter(new EqualsFilter('name', 'scha1_showprice'));
$showprice = $customFieldSetRepository->searchIds($scha1_showprice, $uninstallContext->getContext());
if($showprice->getTotal() > 0 && !$uninstallContext->keepUserData()) {
$data = $showprice->getDataOfId($showprice->firstId());
$customFieldSetRepository->delete([$data], $uninstallContext->getContext());
}
$this->removeConfiguration();
$scha1_showpricetext = new Criteria();
$scha1_showpricetext->addFilter(new EqualsFilter('name', 'scha1_showpricetext'));
$showpricetext = $customFieldSetRepository->searchIds($scha1_showpricetext, $uninstallContext->getContext());
if($showpricetext->getTotal() > 0 && !$uninstallContext->keepUserData()) {
$data = $showpricetext->getDataOfId($showpricetext->firstId());
$customFieldSetRepository->delete([$data], $uninstallContext->getContext());
}
$this->removeConfiguration();
//price set
$scha1_hidePriceInProducts = new Criteria();
$scha1_hidePriceInProducts->addFilter(new EqualsFilter('name', 'scha1_hidePriceInProducts'));
$hidePriceInProducts = $customFieldSetRepository->searchIds($scha1_hidePriceInProducts, $uninstallContext->getContext());
if($hidePriceInProducts->getTotal() > 0 && !$uninstallContext->keepUserData()) {
$data = $hidePriceInProducts->getDataOfId($hidePriceInProducts->firstId());
$customFieldSetRepository->delete([$data], $uninstallContext->getContext());
}
$this->removeConfiguration();
//category
$scha1_showpriceincategory = new Criteria();
$scha1_showpriceincategory->addFilter(new EqualsFilter('name', 'scha1_showpriceincategory'));
$showpriceincategory = $customFieldSetRepository->searchIds($scha1_showpriceincategory, $uninstallContext->getContext());
if($showpriceincategory->getTotal() > 0 && !$uninstallContext->keepUserData()) {
$data = $showpriceincategory->getDataOfId($showpriceincategory->firstId());
$customFieldSetRepository->delete([$data], $uninstallContext->getContext());
}
$this->removeConfiguration();
//category set
$scha1_showPriceInCategory = new Criteria();
$scha1_showPriceInCategory->addFilter(new EqualsFilter('name', 'scha1_showPriceInCategory'));
$showPriceInCategory = $customFieldSetRepository->searchIds($scha1_showPriceInCategory, $uninstallContext->getContext());
if($showPriceInCategory->getTotal() > 0 && !$uninstallContext->keepUserData()) {
$data = $showPriceInCategory->getDataOfId($showPriceInCategory->firstId());
$customFieldSetRepository->delete([$data], $uninstallContext->getContext());
}
$this->removeConfiguration();
$this->removeSnippets();
parent::uninstall($uninstallContext);
}
/**
*
*/
private function removeConfiguration() {
$connection = $this->container->get(Connection::class);
$qb = $connection->createQueryBuilder();
$qb->delete('system_config', 's');
$qb->where('s.configuration_key LIKE :configuration_key');
$qb->setParameter('configuration_key', '%scha1HidePriceInProducts%');
}
/**
*
*/
private function removeSnippets() {
$connection = $this->container->get(Connection::class);
$qb = $connection->createQueryBuilder();
$qb->delete('snippet', 's');
$qb->where('s.translation_key LIKE :translation_key');
$qb->setParameter('translation_key', '%scha1_showprice%');
}
/**
*
* @param \scha1\scha1HidePriceInProducts\InstallContext $installContext
*/
public function install(InstallContext $installContext): void {
$customFieldSetProduct = $this->container->get('custom_field_set.repository');
$customFieldSetProduct->create([[
'name' => 'scha1_hidePriceInProducts',
'config' => ['label' => ['de-DE' => 'Ausblenden der Preise', 'en-GB' => 'Hide Price in Products'], 'customFieldPosition' => 1],
'customFields' => [
['name' => 'scha1_showprice', 'type' => CustomFieldTypes::BOOL, 'config' => ['label' => ['de-DE' => 'Preis ausblenden', 'en-GB' => 'Hide Price'], 'customFieldPosition' => 1]],
['name' => 'scha1_showpricetext', 'type' => CustomFieldTypes::HTML, 'config' => ['label' => ['de-DE' => 'Text anstelle des Preises', 'en-GB' => 'Text instead of the price'], 'componentName' => 'sw-text-editor', 'customFieldType' => 'textEditor', 'customFieldPosition' => 2]],
// ['name' => 'scha1_requestprice', 'type' => CustomFieldTypes::BOOL, 'config' => ['label' => ['de-DE' => 'Preis anfragen', 'en-GB' => 'Request Price']]],
],
'relations' => [['entityName' => 'product']],
]] ,$installContext->getContext());
$customFieldSetProduct->create([[
'name' => 'scha1_showPriceInCategory',
'config' => ['label' => ['de-DE' => 'Preise für diese Kategorie einblenden', 'en-GB' => 'show prices in categories'], 'customFieldPosition' => 1],
'customFields' => [
['name' => 'scha1_showpriceincategory', 'type' => CustomFieldTypes::BOOL, 'config' => ['label' => ['de-DE' => 'Preise einblenden', 'en-GB' => 'show prices for this category'], 'customFieldPosition' => 1]],
],
'relations' => [['entityName' => 'category']]
]],
$installContext->getContext());
parent::install($installContext);
}
}