/** * @param $product_id */ public function add($product_id) { $product = Catalog::product($product_id); if ($product) { $this->items[$product_id] = ['name' => $product->model->name]; } $this->save(); }
/** * @param $productId * @param int $quantity */ public function add($productId, $quantity = 1) { if (isset($this->lines[$productId])) { $this->lines[$productId]->quantity += $quantity; } else { $product = Catalog::product($productId); if (!$product) { throw new InvalidParamException("Product with id = {$productId} does not exist"); } $this->lines[$productId] = new CartLine(['product_id' => $productId, 'price' => $product->model->price, 'quantity' => $quantity]); } $this->save(); }
/** * @param $slug * @return string * @throws NotFoundHttpException */ public function actionView($slug) { $product = Catalog::product($slug); if (!$product) { throw new NotFoundHttpException(); } $comment = new Comment(); if (!\Yii::$app->user->isGuest && $comment->load(\Yii::$app->request->post())) { $comment->user_id = \Yii::$app->user->id; $comment->status = Comment::STATUS_ACTIVE; $product->model->link('comments', $comment); } $wishListModel = new AddToWishListForm(); $wishList = WishList::get(); if ($wishListModel->load(\Yii::$app->request->post())) { $wishList->add($product->model->id); } $category = Catalog::cat($product->model->category_id); return $this->render('view', ['product' => $product, 'category' => $category, 'wishListModel' => $wishListModel, 'inWishList' => $wishList->has($product->model->id)]); }
/** * @return Product */ public function getProduct() { return Catalog::product($this->product_id); }
<table class="table"> <thead> <tr> <th class="thumb-mini"></th> <th class="name"><?php echo Yii::t('app', 'Product name'); ?> </th> </tr> </thead> <tbody> <?php foreach ($wishList->items as $id => $item) { ?> <?php $product = Catalog::product($id); ?> <?php if ($product) { ?> <tr> <td class="thumb-mini"> <a href="<?php echo Url::to(['/product/view', 'slug' => $product->model->slug]); ?> "> <?php echo Html::img($product->thumb(64, 64)); ?> </a> </td>