From a6e4e32e2b2afbd52a30cc14c390ac9917985431 Mon Sep 17 00:00:00 2001 From: Lorenzo Ruozzi Date: Thu, 4 Jan 2024 14:51:41 +0100 Subject: [PATCH] Add basic command to remove disabled products from wishlists --- ...oveDisabledProductsFromWishlistCommand.php | 46 +++++++++++++++++++ src/Resources/config/services/command.yml | 7 +++ 2 files changed, 53 insertions(+) create mode 100644 src/CliCommand/RemoveDisabledProductsFromWishlistCommand.php create mode 100644 src/Resources/config/services/command.yml diff --git a/src/CliCommand/RemoveDisabledProductsFromWishlistCommand.php b/src/CliCommand/RemoveDisabledProductsFromWishlistCommand.php new file mode 100644 index 00000000..ab80d29b --- /dev/null +++ b/src/CliCommand/RemoveDisabledProductsFromWishlistCommand.php @@ -0,0 +1,46 @@ + $wishlistProductRepository + */ + public function __construct( + private readonly RepositoryInterface $wishlistProductRepository, + ) { + parent::__construct(); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $wishlistProducts = $this->wishlistProductRepository->findAll(); + + foreach ($wishlistProducts as $wishlistProduct) { + if ($wishlistProduct->getProduct()->isEnabled()) { + continue; + } + + $output->writeln(sprintf( + 'Removing disabled product with code "%s" from wishlist "%s".', + (string) $wishlistProduct->getProduct()->getCode(), + (string) $wishlistProduct->getWishlist()->getId(), + )); + $wishlistProduct->getWishlist()->removeProduct($wishlistProduct); + $this->wishlistProductRepository->remove($wishlistProduct); + } + + return self::SUCCESS; + } +} diff --git a/src/Resources/config/services/command.yml b/src/Resources/config/services/command.yml new file mode 100644 index 00000000..bcfbef8e --- /dev/null +++ b/src/Resources/config/services/command.yml @@ -0,0 +1,7 @@ +services: + bitbag_sylius_wishlist_plugin.command.remove_disabled_products_from_wishlist: + class: BitBag\SyliusWishlistPlugin\CliCommand\RemoveDisabledProductsFromWishlistCommand + arguments: + - '@bitbag_sylius_wishlist_plugin.repository.wishlist_product' + tags: + - { name: console.command }