diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index 111bb54..f28f911 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -136,7 +136,7 @@ class Product extends \yii\db\ActiveRecord */ public function getImages() { - return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id']); + return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id'])->where(['product_variant_id' => null]); } /** @@ -144,12 +144,14 @@ class Product extends \yii\db\ActiveRecord */ public function getVariant() { +// return ProductVariant::find()->filterWhere([ProductVariant::tableName() .'.product_id' => Product::tableName() .'.product_id'])->where('>', ProductVariant::tableName() .'.stock', 0); return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id']); } public function getVariantPrice() { return $this->variant->price; } + /** * @return \yii\db\ActiveQuery */ @@ -158,6 +160,18 @@ class Product extends \yii\db\ActiveRecord return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id']); } + public function getEnabledVariants() + { + /*$result = []; + foreach ($this->variants as $variant) { + if (is_null($variant->stock) || $variant->stock > 0) { + $result[] = $variant; + } + } + return $result;*/ + return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->where(['!=', ProductVariant::tableName() .'.stock', 0]); + } + public function setVariants($variants) { $this->_variants = $variants; } @@ -201,6 +215,12 @@ class Product extends \yii\db\ActiveRecord return new ProductQuery(get_called_class()); } + public function getQuantity() { + return ProductStock::find() + ->where(['product_id' => $this->product_id]) + ->sum('quantity'); + } + public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); diff --git a/common/modules/product/models/ProductVariant.php b/common/modules/product/models/ProductVariant.php index 7bbbfc8..90f3b64 100755 --- a/common/modules/product/models/ProductVariant.php +++ b/common/modules/product/models/ProductVariant.php @@ -108,12 +108,14 @@ class ProductVariant extends \yii\db\ActiveRecord return $this->hasOne(Product::className(), ['product_id' => 'product_id']); } + public function getProductStock() { + return $this->hasMany(ProductStock::className(), ['product_variant_id' => 'product_variant_id']); + } + public function getQuantity() { return ProductStock::find() ->where(['product_variant_id' => $this->product_variant_id]) ->sum('quantity'); -// return $this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_id' => 'product_id'])->sum(ProductStock::tableName() .'.quantity') > 0; -// return $this->stock !== 0; } public function getStock_caption() { diff --git a/frontend/views/catalog/catalog.php b/frontend/views/catalog/catalog.php index cffd4e4..0484e39 100644 --- a/frontend/views/catalog/catalog.php +++ b/frontend/views/catalog/catalog.php @@ -17,7 +17,7 @@ $this->params['breadcrumbs'][] = ['label' => 'Каталог'];