Skip to content

Commit

Permalink
Add basic command to remove disabled products from wishlists
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Jan 4, 2024
1 parent 41414ed commit a6e4e32
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/CliCommand/RemoveDisabledProductsFromWishlistCommand.php
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;
}
}
7 changes: 7 additions & 0 deletions src/Resources/config/services/command.yml
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 }

0 comments on commit a6e4e32

Please sign in to comment.