Files
YNWA Fawzy 8a3bbd7cb0 - fix plugin not working
- minimize data sent by plugin
- fix the lowest price always zero.
- add method to update the price when you visit the product page with the extension.
- fix new store not added on existing product
- format prices on chart
- fix multiple prices not showing in tooltip in price history widget when store is added later on.
2024-08-04 18:42:52 +04:00

60 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers\Actions;
use App\Helpers\StoresAvailable\StoreTemplate;
use App\Helpers\URLHelper;
use App\Http\Controllers\Controller;
use App\Models\ProductStore;
use App\Models\Store;
use Illuminate\Http\Request;
class UpdateProductController extends Controller
{
/**
* Handle the incoming request.
*/
public function __invoke(Request $request)
{
$request->validate([
'url'=>['required','url'],
'current_price'=>['required','numeric'],
]);
$url=new URLHelper($request->url);
if (!$url->product_unique_key)
return;
//check the store
$store= Store::where('domain', $url->domain)->first();
if (!$store)
return;
//get the record and update it
//todo add used_price
$product_store_record=ProductStore::where([
'store_id'=>$store->id,
'key'=>$url->product_unique_key,
])->first();
if (!$product_store_record)
return;
$to_update=[];
if ($request->current_price != $product_store_record->price)
$to_update['price']=$request->current_price * 100;
if ($request->current_price < $product_store_record->lowest_price || !$product_store_record->lowest_price )
$to_update['lowest_price']=$request->current_price * 100;
if ($request->current_price > $product_store_record->highest_price || !$product_store_record->highest_price )
$to_update['highest_price']=$request->current_price * 100;
ProductStore::where("id", $product_store_record->id)
->update($to_update);
StoreTemplate::record_price_history(product_id: $product_store_record->product_id, store_id: $store->id, price: $request->current_price);
}
}