Skip to content

Disable Product accordingly to product Variants Status #155

@lamasfoker

Description

@lamasfoker

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions