/** * Create a CartProduct from array * * @param array $preCartProductSetValue * * @return \Extcode\Cart\Domain\Model\Cart\Product */ public function createProduct(array $preCartProductSetValue) { $newFeVariant = null; if ($preCartProductSetValue['feVariants']) { $newFeVariant = new \Extcode\Cart\Domain\Model\Cart\FeVariant($preCartProductSetValue['feVariants']); } $newCartProduct = new \Extcode\Cart\Domain\Model\Cart\Product($preCartProductSetValue['productType'], $preCartProductSetValue['productId'], $preCartProductSetValue['tableId'], $preCartProductSetValue['contentId'], $preCartProductSetValue['sku'], $preCartProductSetValue['title'], $preCartProductSetValue['price'], $this->taxClasses[$preCartProductSetValue['taxClassId']], $preCartProductSetValue['quantity'], $preCartProductSetValue['isNetPrice'], $newFeVariant); if ($preCartProductSetValue['maxNumber'] !== null) { $newCartProduct->setMaxNumberInCart($preCartProductSetValue['maxNumber']); } if ($preCartProductSetValue['minNumber'] !== null) { $newCartProduct->setMinNumberInCart($preCartProductSetValue['minNumber']); } if ($preCartProductSetValue['specialPrice'] !== null) { $newCartProduct->setSpecialPrice($preCartProductSetValue['specialPrice']); } if ($preCartProductSetValue['serviceAttribute1'] !== null) { $newCartProduct->setServiceAttribute1($preCartProductSetValue['serviceAttribute1']); } if ($preCartProductSetValue['serviceAttribute2'] !== null) { $newCartProduct->setServiceAttribute2($preCartProductSetValue['serviceAttribute2']); } if ($preCartProductSetValue['serviceAttribute3'] !== null) { $newCartProduct->setServiceAttribute3($preCartProductSetValue['serviceAttribute3']); } $newVariantArr = []; // ToDo: refactor Variant if ($preCartProductSetValue['beVariants']) { $variantConf = []; if (isset($this->pluginSettings['repository']) && is_array($this->pluginSettings['repository'])) { $variantConf = $this->pluginSettings; } elseif (isset($this->pluginSettings['db']) && is_array($this->pluginSettings['db'])) { $variantConf = $this->pluginSettings; } $priceCalcMethod = $preCartProductSetValue['priceCalcMethod']; $price = $newCartProduct->getBestPrice(); if ($this->pluginSettings['gpValues']['beVariants']) { foreach ($this->pluginSettings['gpValues']['beVariants'] as $variantsKey => $variantsValue) { if ($variantsKey == 1) { if ($preCartProductSetValue['hasFeVariants']) { $newVariant = $this->getFeVariant($newCartProduct, null, $preCartProductSetValue, $variantsValue, $priceCalcMethod, $price, $preCartProductSetValue['hasFeVariants'] - 1); } else { if (isset($this->pluginSettings['repository'])) { $variantConf = $variantConf['repository']['beVariants']; } elseif (isset($this->pluginSettings['db'])) { $variantConf = $variantConf['db']['beVariants']; } $newVariant = $this->getDatabaseVariant($newCartProduct, null, $variantConf, $preCartProductSetValue, $variantsValue, $priceCalcMethod, $price); } if ($newVariant) { $newVariantArr[$variantsKey] = $newVariant; $newCartProduct->addBeVariant($newVariant); $price = $newVariant->getPrice(); } else { break; } } elseif ($variantsKey > 1) { // check if variant key-1 has fe_variants defined then use input as fe variant if ($newVariantArr[$variantsKey - 1]->getHasFeVariants()) { $newVariant = $this->getFeVariant(null, $newVariantArr[$variantsKey - 1], $preCartProductSetValue, $variantsValue, $priceCalcMethod, $price, $newVariantArr[$variantsKey - 1]->getHasFeVariants() - 1); } else { if (isset($variantConf['repository'])) { $variantConf = $variantConf['repository']['beVariants']; } elseif (isset($variantConf['db'])) { $variantConf = $variantConf['db']['beVariants']; } $newVariant = $this->getDatabaseVariant(null, $newVariantArr[$variantsKey - 1], $variantConf, $preCartProductSetValue, $variantsValue, $priceCalcMethod, $price); } if ($newVariant) { $newVariantArr[$variantsKey] = $newVariant; $newVariantArr[$variantsKey - 1]->addBeVariant($newVariant); $price = $newVariant->getPrice(); } else { break; } } } } } return $newCartProduct; }
/** * @test */ public function getBestPriceReturnsSpecialPriceWhenSpecialPriceIsLessThanPrice() { $price = 10.0; $specialPrice = 5.0; $product = new \Extcode\Cart\Domain\Model\Cart\Product($this->productType, $this->productId, null, $this->contentId, $this->sku, $this->title, $price, $this->taxClass, $this->quantity); $product->setSpecialPrice($specialPrice); $this->assertSame(5.0, $product->getBestPrice()); }