forked from BitBagCommerce/SyliusWishlistPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic command to remove disabled products from wishlists
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/CliCommand/RemoveDisabledProductsFromWishlistCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusWishlistPlugin\CliCommand; | ||
|
||
use BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class RemoveDisabledProductsFromWishlistCommand extends Command | ||
{ | ||
protected static $defaultName = 'bitbag:wishlist:remove-disabled-products'; | ||
|
||
/** | ||
* @param RepositoryInterface<WishlistProductInterface> $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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |