From 98bd644e3e4bcc940446da4311fc5697d73d8bbc Mon Sep 17 00:00:00 2001 From: jitendra Date: Tue, 10 Sep 2019 12:18:01 +0530 Subject: [PATCH] Created command for updated min_price and max_price based on special_price --- .../src/Console/Commands/PriceUpdate.php | 91 +++++++++++++++++++ .../Webkul/Product/src/Models/ProductFlat.php | 8 ++ .../src/Providers/ProductServiceProvider.php | 12 +++ .../src/Repositories/ProductRepository.php | 3 +- 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 packages/Webkul/Product/src/Console/Commands/PriceUpdate.php diff --git a/packages/Webkul/Product/src/Console/Commands/PriceUpdate.php b/packages/Webkul/Product/src/Console/Commands/PriceUpdate.php new file mode 100644 index 000000000..c15f09c33 --- /dev/null +++ b/packages/Webkul/Product/src/Console/Commands/PriceUpdate.php @@ -0,0 +1,91 @@ +productFlatRepository = $productFlatRepository; + + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $products = $this->productFlatRepository->findWhere([['special_price', '>', 0]]); + + foreach ($products as $product) { + request()->request->set('channel', $product->channel); + + $product->min_price = $product->getTypeInstance()->getMinimalPrice(); + + $product->max_price = $product->getTypeInstance()->getMaximamPrice(); + + $product->save(); + + if ($product->parent) { + $product->parent->min_price = $product->parent->getTypeInstance()->getMinimalPrice(); + + $product->parent->max_price = $product->parent->getTypeInstance()->getMaximamPrice(); + + $product->parent->save(); + } else { + $bundleProducts = $this->productFlatRepository->getModel() + ->addSelect('product_flat.*') + ->distinct() + ->leftJoin('products', 'product_flat.product_id', 'products.id') + ->leftJoin('product_bundle_options', 'products.id', 'product_bundle_options.product_id') + ->leftJoin('product_bundle_option_products', 'product_bundle_options.id', 'product_bundle_option_products.product_bundle_option_id') + ->where('product_bundle_option_products.product_id', $product->product_id) + ->get(); + + foreach ($bundleProducts as $bundleProduct) { + $bundleProduct->min_price = $bundleProduct->getTypeInstance()->getMinimalPrice(); + + $bundleProduct->max_price = $bundleProduct->getTypeInstance()->getMaximamPrice(); + + $bundleProduct->save(); + } + } + } + } +} \ No newline at end of file diff --git a/packages/Webkul/Product/src/Models/ProductFlat.php b/packages/Webkul/Product/src/Models/ProductFlat.php index 5e84da1ce..17b94ec06 100644 --- a/packages/Webkul/Product/src/Models/ProductFlat.php +++ b/packages/Webkul/Product/src/Models/ProductFlat.php @@ -47,6 +47,14 @@ class ProductFlat extends Model implements ProductFlatContract return $this->hasMany(self::class, 'parent_id'); } + /** + * Get the product that owns the product. + */ + public function parent() + { + return $this->belongsTo(self::class, 'parent_id'); + } + /** * Get product type value from base product */ diff --git a/packages/Webkul/Product/src/Providers/ProductServiceProvider.php b/packages/Webkul/Product/src/Providers/ProductServiceProvider.php index 54f9f43e1..902f5261b 100755 --- a/packages/Webkul/Product/src/Providers/ProductServiceProvider.php +++ b/packages/Webkul/Product/src/Providers/ProductServiceProvider.php @@ -5,6 +5,7 @@ namespace Webkul\Product\Providers; use Illuminate\Support\ServiceProvider; use Webkul\Product\Models\ProductProxy; use Webkul\Product\Observers\ProductObserver; +use Webkul\Product\Console\Commands\PriceUpdate; class ProductServiceProvider extends ServiceProvider { @@ -34,6 +35,8 @@ class ProductServiceProvider extends ServiceProvider public function register() { $this->registerConfig(); + + $this->registerCommands(); } public function registerConfig() { @@ -41,4 +44,13 @@ class ProductServiceProvider extends ServiceProvider dirname(__DIR__) . '/Config/product_types.php', 'product_types' ); } + + /** + * Register the console commands of this package + */ + protected function registerCommands() + { + if ($this->app->runningInConsole()) + $this->commands([PriceUpdate::class,]); + } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index dd6e647c0..37479c7d7 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -83,9 +83,8 @@ class ProductRepository extends Repository $product = $product->getTypeInstance()->update($data, $id, $attribute); - if (isset($data['channels'])) { + if (isset($data['channels'])) $product['channels'] = $data['channels']; - } Event::fire('catalog.product.update.after', $product);