diff --git a/models/Product.php b/models/Product.php index be3de9a..d9586ba 100755 --- a/models/Product.php +++ b/models/Product.php @@ -571,7 +571,40 @@ } return $groups; } - + + + /** + * Get characteristic from TaxGroups for current Product filled with $customOptions that satisfy current Product + * + * @return TaxGroup[] + */ + public function getCharacteristic(): array + { + $groups = $options = []; + foreach ($this->getOptions() + ->with('lang') + ->all() as $option) { + /** + * @var TaxOption $option + */ + $options[ $option[ 'tax_group_id' ] ][] = $option; + } + foreach (TaxGroup::find() + ->where(['is_menu'=>true]) + ->andWhere([ 'id' => array_keys($options) ]) + ->with('lang') + ->all() as $group) { + /** + * @var TaxGroup $group + */ + if (!empty($options[ $group->id ])) { + $group->customOptions = $options[ $group->id ]; + $groups[] = $group; + } + } + return $groups; + } + public function getVideos() { return $this->hasMany(ProductVideo::className(), [ 'product_id' => 'id' ]); diff --git a/models/ProductVariant.php b/models/ProductVariant.php index d6181c0..24995de 100755 --- a/models/ProductVariant.php +++ b/models/ProductVariant.php @@ -398,6 +398,38 @@ } return $groups; } + + /** + * Get characteristic from TaxGroups for current ProductVariant filled with $customOptions that satisfy current Product + * + * @return TaxGroup[] + */ + public function getCharacteristic(): array + { + $groups = $options = []; + foreach ($this->getOptions() + ->with('lang') + ->all() as $option) { + /** + * @var TaxOption $option + */ + $options[ $option[ 'tax_group_id' ] ][] = $option; + } + foreach (TaxGroup::find() + ->where(['is_menu'=>true]) + ->andWhere([ 'id' => array_keys($options) ]) + ->with('lang') + ->all() as $group) { + /** + * @var TaxGroup $group + */ + if (!empty($options[ $group->id ])) { + $group->customOptions = $options[ $group->id ]; + $groups[] = $group; + } + } + return $groups; + } /** * Set stocks to override existing in product_stock table -- libgit2 0.21.4