/** * Build product values from template values raw data * * @param ProductTemplateInterface $template * @param AttributeInterface[] $attributes * * @return ProductValueInterface[] */ protected function buildProductValuesFromTemplateValuesData(ProductTemplateInterface $template, array $attributes) { $values = $this->denormalizer->denormalize($template->getValuesData(), 'ProductValue[]', 'json'); $product = new $this->productClass(); foreach ($values as $value) { $product->addValue($value); } foreach ($attributes as $attribute) { $this->productBuilder->addAttributeToProduct($product, $attribute); } return $product->getValues(); }
/** * Creates required value(s) to add the attribute to the product * * @param ProductInterface $product * @param AvailableAttributes $availableAttributes * @param array $savingOptions */ public function addAttributesToProduct(ProductInterface $product, AvailableAttributes $availableAttributes, array $savingOptions = []) { foreach ($availableAttributes->getAttributes() as $attribute) { $this->builder->addAttributeToProduct($product, $attribute); } $options = array_merge(['recalculate' => false, 'schedule' => false], $savingOptions); $this->productSaver->save($product, $options); }
/** * Build product values from template values raw data * * @param ProductTemplateInterface $template * @param AttributeInterface[] $attributes * * @return ProductValueInterface[] */ protected function buildProductValuesFromTemplateValuesData(ProductTemplateInterface $template, array $attributes) { $options = ['locale' => $this->localeResolver->getCurrentLocale(), 'disable_grouping_separator' => true]; $values = $this->denormalizer->denormalize($template->getValuesData(), 'ProductValue[]', 'json', $options); $product = new $this->productClass(); foreach ($values as $value) { $product->addValue($value); } foreach ($attributes as $attribute) { $this->productBuilder->addAttributeToProduct($product, $attribute); } $this->productBuilder->addMissingProductValues($product); return $product->getValues(); }
/** * Denormalize product values * * @param string $data * @param string $format * @param array $context * @param ProductInterface $product */ protected function denormalizeValues($data, $format, array $context, ProductInterface $product) { foreach ($product->getValues() as $value) { $product->removeValue($value); } foreach ($data as $attFieldName => $dataValue) { $attributeInfos = $this->fieldNameBuilder->extractAttributeFieldNameInfos($attFieldName); $attribute = $attributeInfos['attribute']; unset($attributeInfos['attribute']); if (!$product->hasAttribute($attribute)) { $this->productBuilder->addAttributeToProduct($product, $attribute); } // Denormalize data value. // The value is already added to the product so automatically updated $productValue = $product->getValue($attribute->getCode(), $attributeInfos['locale_code'], $attributeInfos['scope_code']); $this->serializer->denormalize($dataValue, $this->productValueClass, $format, ['product' => $product, 'entity' => $productValue] + $attributeInfos + $context); } }
/** * Creates required value(s) to add the attribute to the product * * @param ProductInterface $product * @param AvailableAttributes $availableAttributes * * @return null */ public function addAttributesToProduct(ProductInterface $product, AvailableAttributes $availableAttributes) { foreach ($availableAttributes->getAttributes() as $attribute) { $this->builder->addAttributeToProduct($product, $attribute); } }