public function importData(array $data) { $pse = ProductSaleElementsQuery::create()->findPk($data['id']); if ($pse === null) { return Translator::getInstance()->trans('The product sale element id %id doesn\'t exist', ['%id' => $data['id']]); } else { $currency = null; if (isset($data['currency'])) { $currency = CurrencyQuery::create()->findOneByCode($data['currency']); } if ($currency === null) { $currency = Currency::getDefaultCurrency(); } $price = ProductPriceQuery::create()->filterByProductSaleElementsId($pse->getId())->findOneByCurrencyId($currency->getId()); if ($price === null) { $price = new ProductPrice(); $price->setProductSaleElements($pse)->setCurrency($currency); } $price->setPrice($data['price']); if (isset($data['promo_price'])) { $price->setPromoPrice($data['promo_price']); } if (isset($data['promo'])) { $price->getProductSaleElements()->setPromo((int) $data['promo'])->save(); } $price->save(); $this->importedRows++; } return null; }
/** * @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())) { $this->checkMandatoryColumns($row); $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 = Currency::getDefaultCurrency(); } $price = ProductPriceQuery::create()->filterByProductSaleElementsId($obj->getId())->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"]); } if (isset($row["promo"])) { $price->getProductSaleElements()->setPromo((int) $row["promo"])->save(); } $price->save(); $this->importedRows++; } } return $errors; }
/** * @param ProductPrice $productPrice * @param ProductSaleElements $saleElement * @param Currency $defaultCurrency * @param Currency $currentCurrency */ protected function updatePriceFromDefaultCurrency($productPrice, $saleElement, $defaultCurrency, $currentCurrency) { // Get price for default currency $priceForDefaultCurrency = ProductPriceQuery::create()->filterByCurrency($defaultCurrency)->filterByProductSaleElements($saleElement)->findOne(); if ($priceForDefaultCurrency !== null) { $productPrice->setPrice($priceForDefaultCurrency->getPrice() * $currentCurrency->getRate())->setPromoPrice($priceForDefaultCurrency->getPromoPrice() * $currentCurrency->getRate()); } }