<?php declare(strict_types=1);
namespace Huebert\CrossSellingVariants;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class HuebertCrossSellingVariants extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext); // TODO: Change the autogenerated stub
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSet = [
'name' => 'huebert_cross_selling_variants',
'config' => [
'label' => [
'en-GB' => 'Csutom field set fo Huebert Cross Selling Variants plugin',
'de-DE' => 'Custom Field set für Hübert Cross Selling Variants Plugin'
]
],
'customFields' => [
[
'name' => 'huebert_cross_selling_variants_display_element',
'type' => CustomFieldTypes::SWITCH,
'config' => [
'label' => [
'en-GB' => 'Hide Cross-Selling Element on detail page.',
'de-DE' => 'Cross-Selling Element auf Detail Page ausblenden.'
],
'customFieldPosition' => 1
]
]
],
'relations' => [
[
'entityName' => 'product'
]
]
];
$customFieldSetRepository->create([$customFieldSet], $installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = (new Criteria())
->addFilter(new EqualsFilter('name', 'huebert_cross_selling_variants'));
$customFieldSetId = $customFieldSetRepository->searchIds($criteria, $uninstallContext->getContext())->firstId();
$customFieldSetRepository->delete([
[
'id' => $customFieldSetId
]
], $uninstallContext->getContext());
/** @var EntityRepositoryInterface $productRepository */
$productRepository = $this->container->get('product.repository');
/** @var EntityRepositoryInterface $crossSellingRepository */
$crossSellingRepository = $this->container->get('product_cross_selling.repository');
$criteria = (new Criteria())
->addAssociation('crossSellings')
->addFilter(new MultiFilter(MultiFilter::CONNECTION_AND, [
new NotFilter(MultiFilter::CONNECTION_AND, [
new EqualsFilter('parent.id', null)
])
]));
$products = $productRepository->search($criteria, $uninstallContext->getContext());
/** @var ProductEntity $product */
foreach ($products->getElements() as $product) {
if ($product->getCrossSellings()->count() > 0) {
$crossSellings = $product->getCrossSellings()->getIds();
$deleteData = [];
foreach ($crossSellings as $crossSellingId) {
$deleteData[] = [
'id' => $crossSellingId
];
}
$crossSellingRepository->delete($deleteData, $uninstallContext->getContext());
}
}
}
}