webgriffe / SyliusAkeneoPlugin

Plugin allowing to import data from Akeneo PIM to your Sylius store.
https://webgriffe.github.io/SyliusAkeneoPlugin/
MIT License
23 stars 2 forks source link

Disable Product accordingly to product Variants Status #155

Open lamasfoker opened 1 year ago

lamasfoker commented 1 year ago

If all the product variants of a particular product are disabled also the product should be disabled. In other words, if at least one product variant of a particular product is enabled then the product itself should be enabled.

Proposal

<?php

declare(strict_types=1);

namespace Webgriffe\SyliusAkeneoPlugin\Product;

use App\Doctrine\ORM\ProductRepositoryInterface;

final class StatusResolver implements StatusResolverInterface
{
    public function __construct(private ProductRepositoryInterface $productRepository) {}

    public function resolve(array $akeneoProduct): bool
    {
        $akeneoProductIsEnabled = (bool) $akeneoProduct['enabled'];
        if ($akeneoProduct['parent'] === null || $akeneoProductIsEnabled) {
            return $akeneoProductIsEnabled;
        }

        return $this->isAtLeastOneOfTheOtherVariantsEnabled($akeneoProduct['parent'], $akeneoProduct['identifier']);
    }

    private function isAtLeastOneOfTheOtherVariantsEnabled(string $productCode, string $productVariantCodeTotSkip): bool {
        $product = $this->productRepository->findOneByCode($productCode);
        foreach ($product->getVariants() as $variant) {
            if ($variant->isEnabled() && $variant->getCode() !== $productVariantCodeTotSkip) {
                return true;
            }
        }

        return false;
    }
}

I want also to rewrite the Status resolver paragraph in the README file.