/** * Get price * * @param Currency $currency * @return ProductPrice */ public function getPrice(Currency $currency) { if (!$this->isPriceSet($currency)) { return ProductPrice::getNewInstance($this->product, $currency); } return $this->prices[$currency->getID()]; }
private function updatePrice($replaceQuantityPrices) { $request = $this->getApplication()->getRequest(); $sku = $request->get('sku'); $product = Product::getInstanceBySku($sku, array('ProductPrice')); if ($product == null) { throw new Exception('Product not found'); } $currency = $request->get('currency'); if ($currency == '') { $currency = $this->getApplication()->getDefaultCurrency()->getID(); } $price = $request->get('definedPrice'); if (is_numeric($price)) { $product->setPrice($currency, $price, false); } $price = $request->get('definedListPrice'); if (is_numeric($price)) { $product->setPrice($currency, $price, true); } $quantityPrices = $request->get('quantityPrices'); $groupedQuantityPrices = array(); foreach ($quantityPrices as $item) { if ($item['currency'] == '') { $item['currency'] = $currency; } if ($item['group'] == '') { $item['group'] = 0; } $groupedQuantityPrices[$item['currency']][$item['quantity']][$item['group']] = $item['price']; } foreach ($product->getRelatedRecordSet('ProductPrice', new ARSelectFilter()) as $productPrice) { if ($replaceQuantityPrices == true) { $productPrice->serializedRules->set(serialize(array())); } $currencyID = $productPrice->currency->get()->getID(); if (array_key_exists($currencyID, $groupedQuantityPrices)) { foreach ($groupedQuantityPrices[$currencyID] as $quanty => $qItem) { foreach ($qItem as $group => $price) { $group = !$group ? null : UserGroup::getInstanceByID($group); $productPrice->setPriceRule($quanty, $group, $price); } } $productPrice->save(); unset($groupedQuantityPrices[$currencyID]); // for this currency saved } } if (count($groupedQuantityPrices) > 0) { // there is missing ProductPrice for some currencies, // will try to save as new ProductPrice items foreach ($groupedQuantityPrices as $currency => $rules) { $productPrice = ProductPrice::getNewInstance($product, Currency::getInstanceById($currency)); $productPrice->serializedRules->set(serialize($rules)); $productPrice->save(); } } $product->save(); return $this->statusResponse($sku, 'updated'); }