/**
  * @param ProductValueInterface $value
  * @param mixed                 $data
  * @param string                $currency
  */
 protected function addPriceForCurrency(ProductValueInterface $value, $data, $currency)
 {
     $priceValue = $this->productBuilder->addPriceForCurrency($value, $currency);
     $priceValue->setCurrency($currency);
     $priceValue->setData($data);
     $value->addPrice($priceValue);
 }
 /**
  * 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();
 }
 /**
  * {@inheritdoc}
  */
 public function process($product)
 {
     $contextChannel = $this->channelManager->getChannelByCode($this->channel);
     $this->productBuilder->addMissingProductValues($product, [$contextChannel], $contextChannel->getLocales()->toArray());
     $data['media'] = [];
     $mediaValues = $this->getMediaProductValues($product);
     foreach ($mediaValues as $mediaValue) {
         $data['media'][] = $this->serializer->normalize($mediaValue->getMedia(), 'flat', ['field_name' => 'media', 'prepare_copy' => true, 'value' => $mediaValue]);
     }
     $data['product'] = $this->serializer->normalize($product, 'flat', $this->getNormalizerContext());
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function process($product)
 {
     if (null !== $this->productBuilder) {
         $this->productBuilder->addMissingProductValues($product);
     }
     $data['media'] = [];
     $productMedias = $this->getProductMedias($product);
     if (count($productMedias) > 0) {
         try {
             $data['media'] = $this->serializer->normalize($productMedias, 'flat', ['field_name' => 'media', 'prepare_copy' => true]);
         } catch (FileNotFoundException $e) {
             throw new InvalidItemException($e->getMessage(), ['item' => $product->getIdentifier()->getData(), 'uploadDirectory' => $this->uploadDirectory]);
         }
     }
     $data['product'] = $this->serializer->normalize($product, 'flat', $this->getNormalizerContext());
     return $data;
 }
 /**
  * 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);
 }
 /**
  * @param ProductValueInterface $productValue
  * @param ProductValueInterface $value
  */
 protected function setProductPrice(ProductValueInterface $productValue, ProductValueInterface $value)
 {
     foreach ($value->getPrices() as $price) {
         if (null === ($productPrice = $productValue->getPrice($price->getCurrency()))) {
             $productPrice = $this->productBuilder->addPriceForCurrency($productValue, $price->getCurrency());
         }
         $productPrice->setData($price->getData());
     }
 }
 /**
  * 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();
 }
 /**
  * Add all the values required by the given attribute
  *
  * @param AttributeInterface $attribute
  * @param LocaleInterface    $locale
  */
 protected function addValues(AttributeInterface $attribute, $locale)
 {
     if ($attribute->isScopable()) {
         foreach ($locale->getChannels() as $channel) {
             $key = $attribute->getCode() . '_' . $channel->getCode();
             $value = $this->productBuilder->createProductValue($attribute, $locale->getCode(), $channel->getCode());
             $this->productBuilder->addMissingPrices($value);
             $this->values[$key] = $value;
         }
     } else {
         $value = $this->productBuilder->createProductValue($attribute, $locale->getCode());
         $this->productBuilder->addMissingPrices($value);
         $this->values[$attribute->getCode()] = $value;
     }
 }
 /**
  * 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);
     }
 }
 /**
  * @param ProductInterface $product
  *
  * @deprecated will be removed in 1.5, please use ProductBuilderInterface::addMissingAssociations
  */
 public function ensureAllAssociationTypes(ProductInterface $product)
 {
     $this->builder->addMissingAssociations($product);
 }
 /**
  * Deletes values that link an attribute to a product
  *
  * @param ProductInterface  $product
  * @param AbstractAttribute $attribute
  *
  * @return boolean
  */
 public function removeAttributeFromProduct(ProductInterface $product, AbstractAttribute $attribute)
 {
     $this->builder->removeAttributeFromProduct($product, $attribute);
 }