Created command for updated min_price and max_price based on special_price

This commit is contained in:
jitendra 2019-09-10 12:18:01 +05:30
parent 10b706c1d7
commit 98bd644e3e
4 changed files with 112 additions and 2 deletions

View File

@ -0,0 +1,91 @@
<?php
namespace Webkul\Product\Console\Commands;
use Illuminate\Console\Command;
use Webkul\Product\Repositories\ProductFlatRepository;
class PriceUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'product:price:update';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Automatically updates product information (eg. min_price and max_price)';
/**
* ProductFlatRepository object
*
* @var Object
*/
protected $productFlatRepository;
/**
* Create a new command instance.
*
* @param Webkul\Product\Repositories\ProductFlatRepository $productFlatRepository
* @return void
*/
public function __construct(
ProductFlatRepository $productFlatRepository
)
{
$this->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();
}
}
}
}
}

View File

@ -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
*/

View File

@ -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,]);
}
}

View File

@ -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);