/** * @return array */ protected function getCurrencyCodes() { if (empty($this->currencyCodes)) { $this->currencyCodes = $this->currencyManager->getActiveCodes(); } return $this->currencyCodes; }
/** * {@inheritdoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { parent::setDefaultOptions($resolver); $currencyChoices = $this->currencyManager->getActiveCodes(); $resolver->replaceDefaults(array('data_type' => NumberFilterType::DATA_DECIMAL)); $resolver->setDefaults(array('currency_choices' => array_combine($currencyChoices, $currencyChoices), 'currency_options' => array())); }
/** * Prepare attributes list for CSV headers * * @param array $attributesList * * @return array */ protected function prepareAttributesList(array $attributesList) { $scopeCode = $this->catalogContext->getScopeCode(); $localeCodes = $this->localeRepository->getActivatedLocaleCodes(); $fieldsList = []; foreach ($attributesList as $attribute) { $attCode = $attribute->getCode(); if ($attribute->isLocalizable() && $attribute->isScopable()) { foreach ($localeCodes as $localeCode) { $fieldsList[] = sprintf('%s-%s-%s', $attCode, $localeCode, $scopeCode); } } elseif ($attribute->isLocalizable()) { foreach ($localeCodes as $localeCode) { $fieldsList[] = sprintf('%s-%s', $attCode, $localeCode); } } elseif ($attribute->isScopable()) { $fieldsList[] = sprintf('%s-%s', $attCode, $scopeCode); } elseif (AttributeTypes::IDENTIFIER === $attribute->getAttributeType()) { array_unshift($fieldsList, $attCode); } elseif (AttributeTypes::PRICE_COLLECTION === $attribute->getAttributeType()) { foreach ($this->currencyManager->getActiveCodes() as $currencyCode) { $fieldsList[] = sprintf('%s-%s', $attCode, $currencyCode); } } else { $fieldsList[] = $attCode; } } return $fieldsList; }
/** * Prepare attributes list for CSV headers * * @param array $attributesList * * @return array */ protected function prepareAttributesList(array $attributesList) { $scopeCode = $this->catalogContext->getScopeCode(); $localeCodes = $this->localeManager->getActiveCodes(); $fieldsList = array(); foreach ($attributesList as $attribute) { $attCode = $attribute->getCode(); if ($attribute->isLocalizable() && $attribute->isScopable()) { foreach ($localeCodes as $localeCode) { $fieldsList[] = sprintf('%s-%s-%s', $attCode, $localeCode, $scopeCode); } } elseif ($attribute->isLocalizable()) { foreach ($localeCodes as $localeCode) { $fieldsList[] = sprintf('%s-%s', $attCode, $localeCode); } } elseif ($attribute->isScopable()) { $fieldsList[] = sprintf('%s-%s', $attCode, $scopeCode); } elseif ($attribute->getAttributeType() === 'pim_catalog_identifier') { array_unshift($fieldsList, $attCode); } elseif ($attribute->getAttributeType() === 'pim_catalog_price_collection') { foreach ($this->currencyManager->getActiveCodes() as $currencyCode) { $fieldsList[] = sprintf('%s-%s', $attCode, $currencyCode); } } else { $fieldsList[] = $attCode; } } return $fieldsList; }
/** * Check if data are valid * * @param AttributeInterface $attribute * @param mixed $data * * @return mixed */ protected function checkData(AttributeInterface $attribute, $data) { if (!is_array($data)) { throw InvalidArgumentException::arrayExpected($attribute->getCode(), 'setter', 'prices collection', gettype($data)); } foreach ($data as $price) { if (!is_array($price)) { throw InvalidArgumentException::arrayOfArraysExpected($attribute->getCode(), 'setter', 'prices collection', gettype($data)); } if (!array_key_exists('data', $price)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'data', 'setter', 'prices collection', print_r($data, true)); } if (!array_key_exists('currency', $price)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'currency', 'setter', 'prices collection', print_r($data, true)); } if (!in_array($price['currency'], $this->currencyManager->getActiveCodes())) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'currency', 'The currency does not exist', 'setter', 'prices collection', $price['currency']); } } }
/** * Create a value * * @param AbstractAttribute $attribute * @param string $localeCode * @param string $channelCode * * @return ProductValueInterface */ protected function createValue(AbstractAttribute $attribute, $localeCode = null, $channelCode = null) { $value = $this->productManager->createProductValue(); $value->setAttribute($attribute); if ($attribute->isLocalizable()) { $value->setLocale($localeCode); } if ($attribute->isScopable()) { $value->setScope($channelCode); } if ('pim_catalog_price_collection' === $attribute->getAttributeType()) { foreach ($this->currencyManager->getActiveCodes() as $code) { $value->addPrice($this->createProductPrice($code)); } } return $value; }
/** * @param AttributeInterface $attribute * @param mixed $data */ protected function checkValue(AttributeInterface $attribute, $data) { if (!is_array($data)) { throw InvalidArgumentException::arrayExpected($attribute->getCode(), 'filter', 'price', gettype($data)); } if (!array_key_exists('data', $data)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'data', 'filter', 'price', print_r($data, true)); } if (!array_key_exists('currency', $data)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'currency', 'filter', 'price', print_r($data, true)); } if (!is_numeric($data['data']) && null !== $data['data']) { throw InvalidArgumentException::arrayNumericKeyExpected($attribute->getCode(), 'data', 'filter', 'price', gettype($data['data'])); } if (!is_string($data['currency'])) { throw InvalidArgumentException::arrayStringKeyExpected($attribute->getCode(), 'currency', 'filter', 'price', gettype($data['currency'])); } if (!in_array($data['currency'], $this->currencyManager->getActiveCodes())) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'currency', 'The currency does not exist', 'filter', 'price', $data['currency']); } }
/** * Add missing prices (a price per currency) * * @param ProductInterface $product the product * * @return null */ protected function addMissingPrices(ProductInterface $product) { $activeCurrencies = $this->currencyManager->getActiveCodes(); foreach ($product->getValues() as $value) { if ($value->getAttribute()->getAttributeType() === 'pim_catalog_price_collection') { $prices = $value->getPrices(); foreach ($activeCurrencies as $activeCurrency) { $hasPrice = $prices->filter(function ($price) use($activeCurrency) { return $activeCurrency === $price->getCurrency(); })->count() > 0; if (!$hasPrice) { $this->addPriceForCurrency($value, $activeCurrency); } } foreach ($prices as $price) { if (!in_array($price->getCurrency(), $activeCurrencies)) { $value->removePrice($price); } } } } }
/** * Get active codes * * @return string[] */ public function getActiveCodes() { return $this->baseCurrencyManager->getActiveCodes(); }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver); $currencyChoices = $this->currencyManager->getActiveCodes(); $resolver->setDefaults(['data_type' => NumberFilterType::DATA_DECIMAL, 'currency_choices' => array_combine($currencyChoices, $currencyChoices), 'currency_options' => []]); }