diff --git a/core/lib/Thelia/Config/Resources/import.xml b/core/lib/Thelia/Config/Resources/import.xml index e8787dede..2e775cef7 100644 --- a/core/lib/Thelia/Config/Resources/import.xml +++ b/core/lib/Thelia/Config/Resources/import.xml @@ -19,5 +19,14 @@ Import your stock + + + + Importez vos prix + + + Import your prices + + diff --git a/core/lib/Thelia/ImportExport/AbstractHandler.php b/core/lib/Thelia/ImportExport/AbstractHandler.php index 6b7e7a622..6857fff88 100644 --- a/core/lib/Thelia/ImportExport/AbstractHandler.php +++ b/core/lib/Thelia/ImportExport/AbstractHandler.php @@ -20,6 +20,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ abstract class AbstractHandler { + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ protected $container; /** @@ -32,6 +33,19 @@ abstract class AbstractHandler } + public function getContainer() + { + return $this->container; + } + + /** + * @return \Thelia\Core\HttpFoundation\Request + */ + public function getRequest() + { + return $this->container->get("request"); + } + /** * @return string|array * diff --git a/core/lib/Thelia/ImportExport/Import/Type/ProductPriceImport.php b/core/lib/Thelia/ImportExport/Import/Type/ProductPriceImport.php new file mode 100644 index 000000000..96250a0e4 --- /dev/null +++ b/core/lib/Thelia/ImportExport/Import/Type/ProductPriceImport.php @@ -0,0 +1,112 @@ + + */ +class ProductPriceImport extends ImportHandler +{ + /** + * @return string|array + * + * Define all the type of formatters that this can handle + * return a string if it handle a single type ( specific exports ), + * or an array if multiple. + * + * Thelia types are defined in \Thelia\Core\FileFormat\FormatType + * + * example: + * return array( + * FormatType::TABLE, + * FormatType::UNBOUNDED, + * ); + */ + public function getHandledTypes() + { + return array( + FormatType::TABLE, + FormatType::UNBOUNDED, + ); + } + + /** + * @param \Thelia\Core\FileFormat\Formatting\FormatterData + * @return string|array error messages + * + * The method does the import routine from a FormatterData + */ + public function retrieveFromFormatterData(FormatterData $data) + { + $errors = []; + $translator = Translator::getInstance(); + + while (null !== $row = $data->popRow()) { + $obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]); + + if ($obj === null) { + $errorMessage = $translator->trans( + "The product sale element reference %ref doesn't exist", + [ + "%ref" => $row["ref"] + ] + ); + + $errors[] = $errorMessage ; + } else { + + $currency = null; + + if (isset($row["currency"])) { + $currency = CurrencyQuery::create()->findOneByCode($row["currency"]); + } + + if ($currency === null) { + $currency = $this->getRequest()->getSession()->getCurrency(); + } + + $price = ProductPriceQuery::create() + ->filterByProductSaleElements($obj) + ->findOneByCurrencyId($currency->getId()); + + if ($price === null) { + $price = new ProductPrice(); + $price->setProductSaleElements($obj) + ->setCurrency($currency) + ; + } + + $price->setPrice($row["price"]); + + if (isset($row["promo_price"])) { + $price->setPromoPrice($row["promo_price"]); + } + + $price->save(); + } + } + + return $errors; + } + +} \ No newline at end of file