/** * Runs the purchase. * Purchases the positions in the [[cart]]. */ public function run() { if (!$this->cart->isEmpty) { foreach ($this->cart->positions as $position) { $purchase = $position->getPurchaseModel(); try { if ($purchase->execute()) { $this->_success[] = $purchase; $this->cart->remove($position); } else { $this->_error[] = new ErrorPurchaseException(reset(reset($purchase->getErrors())), $purchase); } } catch (PendingPurchaseException $e) { $this->_pending[] = $e; $this->cart->remove($position); } catch (ErrorResponseException $e) { $this->_error[] = new ErrorPurchaseException($e->getMessage(), $purchase, $e); } catch (HiArtException $e) { $this->_error[] = new ErrorPurchaseException($e->getMessage(), $purchase, $e); } } } }
/** * Updates positions using the calculations provided with [[getCalculation]] */ private function applyCalculations() { $currency = Yii::$app->params['currency']; foreach ($this->models as $position) { $id = $position->id; $calculation = $this->getCalculation($id); if (!$calculation instanceof Calculation) { Yii::error('Cart position "' . $position->getName() . '" was removed from the cart because of failed value calculation. Normally this should never happen.', 'hipanel.cart'); $this->cart->removeById($position->id); break; } $value = $calculation->forCurrency($currency); if (!$value instanceof Value) { Yii::error('Cart position "' . $position->getName() . '" was removed from the cart because calculation for currency "' . $currency . '" is not available', 'hipanel.cart'); $this->cart->removeById($position->id); break; } $position->setPrice($value->price); $position->setValue($value->value); } }
public function testFormatCurrency() { $this->assertSame('$9.99', $this->object->formatCurrency($this->price)); }
public function testGetCart() { $this->assertInstanceOf(ShoppingCart::className(), $this->object->getCart()); $this->assertSame(Yii::$app->getModule('cart')->get('cart'), $this->object->cart); }