/**
  * Validate variant group
  *
  * @param GroupInterface $variantGroup
  * @param Constraint     $constraint
  */
 protected function validateVariantGroup(GroupInterface $variantGroup, Constraint $constraint)
 {
     $existingCombinations = array();
     $products = $variantGroup->getProducts();
     if (null === $products) {
         $products = $this->getMatchingProducts($variantGroup);
     }
     foreach ($products as $product) {
         $values = array();
         foreach ($variantGroup->getAxisAttributes() as $attribute) {
             $code = $attribute->getCode();
             $option = $product->getValue($code) ? (string) $product->getValue($code)->getOption() : null;
             if (null === $option) {
                 $this->addEmptyAxisViolation($constraint, $variantGroup->getLabel(), $product->getIdentifier()->getVarchar(), $attribute->getCode());
             }
             $values[] = sprintf('%s: %s', $code, $option);
         }
         $combination = implode(', ', $values);
         if (in_array($combination, $existingCombinations)) {
             $this->addExistingCombinationViolation($constraint, $variantGroup->getLabel(), $combination);
         } else {
             $existingCombinations[] = $combination;
         }
     }
 }
 function it_does_not_copy_values_to_products_when_template_is_empty(GroupInterface $variantGroup, ProductTemplateInterface $productTemplate, Collection $productCollection, ProductInterface $productOne, ProductInterface $productTwo, $productTplApplier, $stepExecution)
 {
     $variantGroup->getId()->willReturn(42);
     $stepExecution->incrementSummaryInfo('update')->shouldBeCalled();
     $variantGroup->getProductTemplate()->willReturn($productTemplate);
     $productTemplate->getValuesData()->willReturn([]);
     $variantGroup->getProducts()->willReturn($productCollection);
     $productCollection->isEmpty()->willReturn(false);
     $productCollection->toArray()->willReturn([$productOne, $productTwo]);
     $productCollection->count()->willReturn(2);
     $productTplApplier->apply($productTemplate, [$productOne, $productTwo])->shouldNotBeCalled();
     $this->write([$variantGroup]);
 }
 function it_adds_violation_when_validating_group_containing_products_with_non_unique_combination_of_axis_attributes($context, GroupInterface $tShirtVariantGroup, GroupTypeInterface $tShirtGroupType, ProductInterface $redTShirtProduct, ProductInterface $redTShirtProduct2, AttributeInterface $sizeAttribute, AttributeInterface $colorAttribute, ProductValueInterface $sizeProductValue, ProductValueInterface $colorProductValue, UniqueVariantAxis $uniqueVariantAxisConstraint, ConstraintViolationBuilderInterface $violation)
 {
     $tShirtGroupType->isVariant()->willReturn(true);
     $tShirtVariantGroup->getType()->willReturn($tShirtGroupType);
     $tShirtVariantGroup->getProducts()->willReturn([$redTShirtProduct, $redTShirtProduct2]);
     $tShirtVariantGroup->getAxisAttributes()->willReturn([$sizeAttribute, $colorAttribute]);
     $sizeAttribute->getCode()->willReturn('size');
     $colorAttribute->getCode()->willReturn('color');
     $sizeProductValue->getOption()->willReturn('XL');
     $colorProductValue->getOption()->willReturn('Red');
     $redTShirtProduct->getValue('size')->willReturn($sizeProductValue);
     $redTShirtProduct->getValue('color')->willReturn($colorProductValue);
     $redTShirtProduct2->getValue('size')->willReturn($sizeProductValue);
     $redTShirtProduct2->getValue('color')->willReturn($colorProductValue);
     $tShirtVariantGroup->getLabel()->willReturn('Groupe TShirt');
     $context->buildViolation('Group "%variant group%" already contains another product with values "%values%"', ['%variant group%' => 'Groupe TShirt', '%values%' => 'size: XL, color: Red'])->shouldBeCalled()->willReturn($violation);
     $this->validate($tShirtVariantGroup, $uniqueVariantAxisConstraint);
 }
 /**
  * Copy variant group values to products
  *
  * @param GroupInterface $variantGroup
  */
 protected function copyValuesToProducts(GroupInterface $variantGroup)
 {
     $template = $variantGroup->getProductTemplate();
     $products = $variantGroup->getProducts();
     if ($template && count($template->getValuesData()) > 0 && count($products) > 0) {
         $skippedMessages = $this->productTplApplier->apply($template, $products->toArray());
         $nbSkipped = count($skippedMessages);
         $nbUpdated = count($products) - $nbSkipped;
         $this->incrementUpdatedProductsCount($nbUpdated);
         if ($nbSkipped > 0) {
             $this->incrementSkippedProductsCount($nbSkipped, $skippedMessages);
         }
     }
 }
예제 #5
0
 /**
  * Copy the variant group values on any products belonging in the variant group
  *
  * @param GroupInterface $group
  */
 protected function copyVariantGroupValues(GroupInterface $group)
 {
     $template = $group->getProductTemplate();
     $products = $group->getProducts()->toArray();
     $this->productTplApplier->apply($template, $products);
 }
 function it_marks_products_as_updated_when_a_group_is_removed_or_updated(EntityManager $em, ProductInterface $foo, ProductInterface $bar, GroupInterface $group)
 {
     $group->getProducts()->willReturn([$foo, $bar]);
     $this->guessUpdates($em, $group, UpdateGuesserInterface::ACTION_UPDATE_ENTITY)->shouldReturn([$foo, $bar]);
     $this->guessUpdates($em, $group, UpdateGuesserInterface::ACTION_DELETE)->shouldReturn([$foo, $bar]);
 }