sarga/packages/Webkul/Product/src/Console/Commands/PriceUpdate.php

79 lines
2.7 KiB
PHP
Raw Normal View History

<?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)';
/**
* Create a new command instance.
*
2020-03-05 13:37:08 +00:00
* @param ]Webkul\Product\Repositories\ProductFlatRepository $productFlatRepository
* @return void
*/
public function __construct(protected ProductFlatRepository $productFlatRepository)
{
parent::__construct();
}
/**
* Execute the console command.
*
2020-03-05 13:37:08 +00:00
* @return void
*/
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()
2020-02-28 10:25:53 +00:00
->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_productsproduct_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();
}
}
}
}
}