Skip to content

Commit

Permalink
Merge pull request #2068 from dpfaffenbauer/variants-recursive
Browse files Browse the repository at this point in the history
[Variant] allow recursive attributes and variants
  • Loading branch information
dpfaffenbauer authored Sep 8, 2022
2 parents 4248343 + de50139 commit 5b0cdc3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ public function preUpdate(DataObjectEvent $dataObjectEvent): void
return;
}

$parent = $object->getParent();
$parent = $object;

if ($parent instanceof AttributeGroupInterface) {
$object->setAttributeGroup($parent);
} else {
$object->setAttributeGroup(null);
}
do {
$parent = $parent->getParent();

if ($parent instanceof AttributeGroupInterface) {
$object->setAttributeGroup($parent);
break;
}

} while($parent != null);

$this->validate($object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function serializeGroups(array $attributeGroups, array $groups = ['coresh
return $this->serializer->normalize($attributeGroups, 'json', [
'groups' => $groups,
AbstractNormalizer::CALLBACKS => [
'valueColor' => static function (?string $innerObject) {
'valueColor' => static function (string $innerObject) {
return $innerObject;
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use CoreShop\Component\Variant\Model\AttributeGroupInterface;
use CoreShop\Component\Variant\Model\AttributeInterface;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -48,7 +48,14 @@ public function validate($value, Constraint $constraint): void
return;
}

foreach ($parent->getChildren() as $child) {
$concreteListing = new DataObject\Listing();
$concreteListing->setCondition('o_path LIKE \''.$parent->getFullPath().'/%\'');

foreach ($concreteListing as $child) {
if (!$child instanceof AttributeInterface) {
continue;
}

if (!$value instanceof $child) {
$this->context->buildViolation($constraint->message)
->addViolation();
Expand Down
7 changes: 3 additions & 4 deletions src/CoreShop/Component/Variant/AttributeCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getAttributesFromVariants(ProductVariantAwareInterface $product,
if (AbstractObject::OBJECT_TYPE_VARIANT === $product->getType()) {
$variants = [$product];
} else {
$variants = $product->getChildren([AbstractObject::OBJECT_TYPE_VARIANT]);
$variants = $product->getVariants();
}

return $this->getAttributes($variants, $showInList);
Expand Down Expand Up @@ -99,9 +99,8 @@ public function getAttributesFromObject(ProductVariantAwareInterface $product, b
if (AbstractObject::OBJECT_TYPE_VARIANT === $product->getType()) {
$product = $product->getVariantParent();
}
$variants = $product->getChildren([AbstractObject::OBJECT_TYPE_VARIANT]);

return $this->getAttributes($variants, $showInList);
return $this->getAttributes($product->getVariants(), $showInList);
}

public function getIndex(ProductVariantAwareInterface $product)
Expand All @@ -111,7 +110,7 @@ public function getIndex(ProductVariantAwareInterface $product)
$product = $product->getVariantParent();
}

$variants = $product->getChildren([AbstractObject::OBJECT_TYPE_VARIANT]);
$variants = $product->getVariants();

foreach ($variants as $variant) {
if (!$variant instanceof ProductVariantAwareInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ public function getMainVariant(): ?Concrete;
public function setMainVariant(?Concrete $purchasable);

public function getVariantParent();

public function getVariants(): array;
}
21 changes: 18 additions & 3 deletions src/CoreShop/Component/Variant/Model/ProductVariantTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,26 @@ trait ProductVariantTrait
*/
abstract public function getAttributes(): ?array;

public function getVariants(): array
{
if ($this instanceof Concrete) {
$list = $this::getList();
$list->setCondition('o_path LIKE \'' . $this->getFullPath() . '/%\'');
$list->setObjectTypes([AbstractObject::OBJECT_TYPE_VARIANT]);

$variants = $list->getObjects();
}
else {
$variants = $this->getChildren([AbstractObject::OBJECT_TYPE_VARIANT]);
}

return $variants;
}

public function findAttributeForGroup(AttributeGroupInterface $attributeGroup): ?AttributeInterface
{
foreach ($this->getAttributes() as $attribute) {
if ($attribute->getAttributeGroup() && $attribute->getAttributeGroup()->getId() === $attributeGroup->getId(
)) {
if ($attribute->getAttributeGroup() && $attribute->getAttributeGroup()->getId() === $attributeGroup->getId()) {
return $attribute;
}
}
Expand All @@ -54,7 +69,7 @@ public function findMainVariant(): ?ProductVariantAwareInterface
/**
* @var ProductVariantAwareInterface[] $variants
*/
$variants = $this->getChildren([AbstractObject::OBJECT_TYPE_VARIANT]);
$variants = $this->getVariants();

if (!$variants) {
return null;
Expand Down

0 comments on commit 5b0cdc3

Please sign in to comment.