public function run() { $this->cart = \Yii::$app->cart; $products = Product::find()->orderBy(['id' => SORT_DESC])->limit($this->num)->all(); $showOwners = $this->cart->saveToDataBase; return $this->render('new-products', ['products' => $products, 'showOwners' => $showOwners]); }
public function up() { $this->addColumn('shop_product', 'status', $this->integer()->defaultValue(1)); $products = Product::find()->all(); foreach ($products as $product) { $product->status = 10; $product->save(); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = \Yii::$app->user->can('viewCompleteProductList') ? Product::find()->joinWith('translations')->orderBy(['category_id' => SORT_ASC, 'position' => SORT_ASC]) : Product::find()->joinWith('translations')->where(['owner' => \Yii::$app->user->id])->orderBy(['category_id' => SORT_ASC, 'position' => SORT_ASC]); $this->load($params); $query->andFilterWhere(['shop_product.category_id' => $this->category])->andFilterWhere(['like', 'shop_product_translation.title', $this->title])->andFilterWhere(['shop_product.status' => $this->status]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pagesize' => 10]]); if (!$this->validate()) { return $dataProvider; } return $dataProvider; }
private function findRecommendedProducts($id) { if (!empty($id)) { $product = Product::findOne($id); $categoryId = $product->category_id; $previous = Product::find()->where(['<', 'id', $id])->andWhere(['category_id' => $categoryId])->orderBy(['id' => SORT_DESC])->limit('2')->all(); $next = Product::find()->where(['>', 'id', $id])->andWhere(['category_id' => $categoryId])->orderBy(['id' => SORT_ASC])->limit('2')->all(); $products = ArrayHelper::merge($previous, $next); return $products; } return false; }
/** * Creates data provider instance with search query applied * @param array $params * @return ActiveDataProvider * @throws Exception if search is not validated */ public function search($params, $descendantCategories) { $this->load($params, ''); $query = Product::find(); if (\Yii::$app->controller->module->showChildCategoriesProducts) { if (!empty($params['id'])) { $query->where(['in', 'category_id', ArrayHelper::map($descendantCategories, 'id', 'id')]); } } else { if (!empty($params['id'])) { $query->where(['category_id' => $params['id']]); } } $filterTypes = FilterType::find()->all(); foreach ($filterTypes as $filterType) { $className = $filterType->class_name; /*Getting get-method name*/ $getMethodName = explode("\\", $className); $getMethodName = lcfirst(end($getMethodName)); $query->joinWith($getMethodName); $tableName = $className::tableName(); $column = $filterType->column; $query->andFilterWhere([$tableName . '.' . 'id' => $this->{$column}]); } if (isset($params['sort'])) { switch ($params['sort']) { case self::SORT_CHEAP: $query->orderBy(['price' => SORT_ASC]); break; case self::SORT_EXPENSIVE: $query->orderBy(['price' => SORT_DESC]); break; case self::SORT_OLD: $query->orderBy(['creation_time' => SORT_ASC]); break; case self::SORT_NEW: $query->orderBy(['creation_time' => SORT_DESC]); break; } } else { $query->orderBy(['category_id' => SORT_ASC, 'position' => SORT_ASC]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]); if ($this->validate()) { return $dataProvider; } else { throw new Exception(); } }
public function upload() { if ($this->validate()) { $dir = Yii::getAlias('@frontend/web/video'); if (!file_exists($dir)) { mkdir($dir); } if (!empty($this->file_name)) { $baseName = Product::generateImageName($this->file_name->name) . '.' . end(explode("/", $this->file_name->type)); $this->file_name->saveAs($dir . '/' . $baseName); return $baseName; } } return false; }
public function safeUp() { $this->addColumn('shop_product', 'position', 'INT(5) AFTER id'); $categories = Category::find()->all(); foreach ($categories as $category) { $products = Product::find()->where(['category_id' => $category->id])->all(); for ($i = 0; $i < count($products); $i++) { $this->execute('UPDATE shop_product SET position=' . ($i + 1) . ' WHERE id=' . $products[$i]->id . ' AND category_id=' . $category->id); } } $this->addColumn('shop_category', 'position', $this->integer()); for ($i = 0; $i < count($categories); $i++) { $this->execute('UPDATE shop_category SET position=' . ($i + 1) . ' WHERE id=' . $categories[$i]->id); } }
public function run() { parent::run(); if (!empty($this->className)) { $class = \Yii::createObject($this->className); $categories = $class::find()->where(['parent_id' => null, 'show' => 1])->orderBy('position')->all(); $currentCategoryId = ''; if (Yii::$app->controller->module->id == 'shop') { if (Yii::$app->controller->id == 'category') { $currentCategoryId = \Yii::$app->request->get('id'); } elseif (Yii::$app->controller->id == 'product') { $product = Product::findOne(\Yii::$app->request->get('id')); $currentCategoryId = $product->category_id; } } return $this->render('tree/tree', ['categories' => $categories, 'currentCategoryId' => $currentCategoryId, 'level' => 0, 'context' => $this]); } else { return false; } }
public function copy($link) { $imagable = \Yii::$app->shop_imagable; $dir = $imagable->imagesPath . '/shop-product/'; if (exif_imagetype($link) == IMAGETYPE_JPEG || exif_imagetype($link) == IMAGETYPE_PNG) { if (!empty($link)) { $baseName = Product::generateImageName($link); if (!file_exists($dir)) { mkdir($dir); } $newFile = $dir . $baseName . $this->extension; if (copy($link, $newFile)) { $image_name = $imagable->create('shop-product', $newFile); unlink($newFile); return $image_name; } } } return false; }
/** * Creates a URL according to the given route and parameters. * @param UrlManager $manager the URL manager * @param string $route the route. It should not have slashes at the beginning or the end. * @param array $params the parameters * @return string|boolean the created URL, or false if this rule cannot be used for creating this URL. */ public function createUrl($manager, $route, $params) { $pathInfo = ''; if ($route == $this->categoryRoute && empty($params['id'])) { $pathInfo = $this->prefix; } else { if (($route == $this->productRoute || $route == $this->categoryRoute) && !empty($params['id'])) { $id = $params['id']; $parentId = null; $language = Language::findOne(['lang_id' => $manager->language]); if ($route == $this->productRoute) { $product = Product::findOne($id); if (empty($product)) { return false; } if ($product->getTranslation($language->id) && $product->getTranslation($language->id)->seoUrl) { $pathInfo = $product->getTranslation($language->id)->seoUrl; $parentId = $product->category_id; } else { return false; } } else { if ($route == $this->categoryRoute) { $category = Category::findOne($id); if ($category->getTranslation($language->id) && $category->getTranslation($language->id)->seoUrl) { $pathInfo = $category->getTranslation($language->id)->seoUrl; $parentId = $category->parent_id; } else { return false; } } } while ($parentId != null) { $category = Category::findOne($parentId); if ($category->getTranslation($language->id) && $category->getTranslation($language->id)->seoUrl) { $pathInfo = $category->getTranslation($language->id)->seoUrl . '/' . $pathInfo; $parentId = $category->parent_id; } else { return false; } } if (!empty($this->prefix)) { $pathInfo = $this->prefix . '/' . $pathInfo; } unset($params['id']); } else { return false; } } return $pathInfo . '?' . http_build_query($params); }
public function getProducts() { return $this->hasMany(Product::className(), ['country_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getProduct() { return $this->hasOne(Product::className(), ['id' => 'product_id']); }
/** * Changes product status property by ModerationManager * * Users which have 'moderateProductCreation' permission can change product status. * * @param integer $id * @param integer $status * @return mixed */ public function actionChangeProductStatus($id, $status) { if (!empty($id) && !empty($status)) { $product = Product::findOne($id); if ($product->status == Product::STATUS_ON_MODERATION) { switch ($status) { case Product::STATUS_SUCCESS: $product->status = Product::STATUS_SUCCESS; $product->save(); break; case Product::STATUS_DECLINED: $product->status = Product::STATUS_DECLINED; $product->save(); break; } } } return $this->redirect(Yii::$app->request->referrer); }
/** * @return \yii\db\ActiveQuery */ public function getProducts() { return $this->hasMany(Product::className(), ['status' => 'id']); }