aggiorna quantita in base se è in prevendita

This commit is contained in:
paoloar77
2024-05-20 01:23:47 +02:00
parent a983293991
commit 024144a942
4 changed files with 167 additions and 114 deletions

View File

@@ -95,6 +95,9 @@ class ProductUpdateGm extends Command
$log1 = 'EVENTUALI PRODOTTI NON INSERITI' . "\n";
$log2 = 'PRODOTTI AGGIORNATI' . "\n";
$log3 = 'PRODOTTI NON INSERITI PER PROBLEMI SERVER' . "\n";
$log4 = 'PRODOTTI IN PREVENDITA' . "\n";
$preorder = true;
foreach ($articles as $article) {
try {
@@ -1028,16 +1031,26 @@ class ProductUpdateGm extends Command
'date_on_sale_from' => $article->DataInizioCampagna,
'date_on_sale_to' => $article->DataFineCampagna,
'stock_quantity' => $article->stock,
];
$idprodotto = $productsku['parent_id'];
if ($idprodotto > 0) {
$variation = Variation::update($idprodotto, $productsku['id'], $data1);
$log2 .= $article->Titolo . ' - ' . $article->DescrizioneTipologia . ' - ' . $article->DescrizioneFormato . ' - Articolo aggiornato - ' . $variation['permalink'] . "\n";
$log2 .= $article->Titolo . ' - [Quantità: ' . $data1['stock_quantity'] . '] - ' . $article->DescrizioneTipologia . ' - ' . $article->DescrizioneFormato . ' - Articolo aggiornato - ' . $variation['permalink'] . "\n";
$aggiornato = true;
}
}
if ($preorder) {
// Controlla se è in preordine
$inprevendita = isArticleInPrevendita($article->IdArticolo);
if ($inprevendita) {
setPreOrder($article->IdArticolo, true, false);
$log4 .= ' SEtTATO IN PREVENDITA ! <br>';
}
}
} catch (\Exception $e) {
$log3 .= $article->IdArticolo . ' - ' . $article->Titolo . "\n";
}
@@ -1045,9 +1058,9 @@ class ProductUpdateGm extends Command
$settingora->value = $ora_update;
$settingora->save();
if ($aggiornato) {
Log::channel('updateproducts')->notice($log . $log2 . $log1 . $log3);
Log::channel('updateproducts')->notice($log . $log2 . $log1 . $log3 . $log4);
Log::channel('updateproducts')->notice('Fino a ' . $ora_update . "\n");
Mail::raw($loginizio . $log . $log2 . $log1 . $log3 . $logfine, function ($message) {
Mail::raw($loginizio . $log . $log2 . $log1 . $log3 . $log4 . $logfine, function ($message) {
$message->to(Mylog::getEmail());
$message->subject(Mylog::getSubjectEmail("Inserim. nuovi prodotti"));
});

View File

@@ -100,6 +100,13 @@ class ProductUpdateQta extends Command
}
$log .= ' *** ' . $stock->Codice . ' QTA=' . $stock->QtaDisponibile . "\n";
$inprevendita = isArticleInPrevendita($stock->Codice);
if ($inprevendita) {
setPreOrder($stock->Codice, true, false);
$log .= ' SEtTATO IN PREVENDITA ! <br>';
}
$aggiornato = true;
}
} catch (\Exception $e) {
@@ -114,7 +121,7 @@ class ProductUpdateQta extends Command
$settingora->save();
if ($aggiornato) {
Log::channel('updateproductsqta')->notice($loginizio . $lognrprodotti . $logfine);
Log::channel('updateproductsqta')->notice($loginizio . $lognrprodotti . $log . $logfine);
Mail::raw($loginizio . $lognrprodotti . $logfine, function ($message) {
$message->to(Mylog::getEmail());
$message->subject(Mylog::getSubjectEmail("Aggiornam. Qtà Prodotti:"));

View File

@@ -1,5 +1,9 @@
<?php
use App\Article;
use Codexshaper\WooCommerce\Facades\Product;
use Codexshaper\WooCommerce\Facades\Variation;
function showarray($array)
{
echo '<pre>' . json_encode($array, JSON_PRETTY_PRINT) . '</pre>';
@@ -26,7 +30,8 @@ function getValueByKey($array, $key)
}
// Funzione per aggiornare il valore di un certo key in un array di oggetti
function updateValueByKey(&$array, $key, $newValue) {
function updateValueByKey(&$array, $key, $newValue)
{
foreach ($array as &$item) {
if (is_object($item) && property_exists($item, 'key') && $item->key === $key) {
$item->value = $newValue;
@@ -42,7 +47,8 @@ function updateValueByKey(&$array, $key, $newValue) {
$array[] = $newItem; // Aggiungi il nuovo elemento all'array
}
function updateValueByKeyArr(&$array, $key, $newValue) {
function updateValueByKeyArr(&$array, $key, $newValue)
{
foreach ($array as &$item) {
if ($item['key'] === $key) {
$item['value'] = $newValue;
@@ -52,3 +58,135 @@ function updateValueByKeyArr(&$array, $key, $newValue) {
$array[$key] = $newValue;
}
function setPreOrderByIdArticolo($idarticolo, $aggiornapreordine, $debug)
{
return setPreOrder($idarticolo, $aggiornapreordine, $debug);
}
function setPreOrder($sku, $aggiornapreordine, $debug)
{
try {
// Aggiorna Preorder
$product = Product::where('sku', $sku)->first();
// get the article by product
// $article = Article::where('IdArticolo', $idarticolo)->first();
// showarray($article);
if ($debug)
showarray($product);
$campoPreOrder = '_wpro_variable_is_preorder';
if ($product) {
$titolo = $product['name'];
if ($debug)
echo "Prodotto trovato: " . $titolo . "StockQty = " . $product['stock_quantity'] . "<br>";
$preorder = false;
if (isKeyPresent($product['meta_data'], $campoPreOrder)) {
$preorder = getValueByKey($product['meta_data'], $campoPreOrder);
}
if ($debug) {
if ($preorder) {
if ($preorder === 'true') {
echo "<span class='badge badge-success' style='color: green;'>PREORDER SI !</span>: " . $preorder;
} else {
echo "<span class='badge badge-success' style='color: green;'>PREORDER:</span>: " . $preorder;
}
} else {
echo "<span style='color: red;'>preorder non presente !</span>: ";
}
echo "<br>";
}
if ($aggiornapreordine === '1') {
$data = [];
$datenow = date('Y-m-d');
$idprodotto = $product['parent_id'];
if ($idprodotto > 0) {
if ($debug)
echo "Variazioni: " . $product['parent_id'] . "<br>";
$variations = Variation::all($product['parent_id']);
if ($debug)
showarray($variations);
for ($i = 0; $i < count($variations); $i++) {
$variation = $variations[$i];
if ($variation->id == $product['id']) {
// convert object into array
$data = json_decode(json_encode($variation), true);
break;
}
}
$data['stock_quantity'] = '1000';
if ($debug) {
echo "Data:";
showarray($data);
}
$agg = true;
$data['meta_data'] = $product['meta_data'];
if ($agg) {
updateValueByKey($data['meta_data'], $campoPreOrder, 'yes');
updateValueByKey($data['meta_data'], '_is_pre_order', 'yes');
updateValueByKey($data['meta_data'], '_pre_order_date', $datenow);
updateValueByKey($data['meta_data'], '_wpro_date_label_variable', '');
updateValueByKey($data['meta_data'], '_wpro_no_date_label_variable', '');
updateValueByKey($data['meta_data'], '_wpro_manage_price_variable', '');
updateValueByKey($data['meta_data'], '_wpro_price_variable', '');
updateValueByKey($data['meta_data'], '_wpro_label_variable', 'Pre Ordinalo!');
updateValueByKey($data['meta_data'], '_wpro_price_type_variable', 'manual');
updateValueByKey($data['meta_data'], '_wpro_amount_price_variable', 'fixed');
updateValueByKey($data['meta_data'], '_wpro_date_variable', '');
updateValueByKey($data['meta_data'], '_wpro_time_variable', '');
updateValueByKey($data['meta_data'], '_rank_math_gtin_code', '');
}
if ($debug) {
echo "<br>Dati da Salvare:";
showarray($data);
}
$variation = Variation::update($idprodotto, $product['id'], $data);
// Product::update($idprodotto, $data);
if ($variation) {
if ($debug)
echo "Aggiornato Preorder: [ParentId=" . $idprodotto . '] ProdId= ' . $product['id'] . ' ' . $variation['name'] . "<br>";
}
}
}
} else {
if ($debug)
echo "Il prodotto non esiste";
}
} catch (Exception $e) {
echo "Errore: " . $e->getMessage();
}
}
function isArticleInPrevendita($idarticolo) {
$product = Product::where('sku', $idarticolo)->first();
$article = Article::where('IdArticolo', $idarticolo)->first();
$qtaneg = $article->QtaDisponibile < 0;
if ($article) {
return $qtaneg && ($article->DescrizioneStatoProdotto === 'In prevendita'
|| $article->DescrizioneStatoProdotto === 'In Commercio'
|| $article->DescrizioneStatoProdotto === 'Prossima uscita');
}
return false;
}

View File

@@ -6066,112 +6066,7 @@ Route::get('/provapao', [TestPaoController::class, 'provapao']);
Route::get('/aggiornapreorder/{idarticolo}/{aggiornapreordine}', function ($idarticolo, $aggiornapreordine) {
try {
// Aggiorna Preorder
$product = Product::where('sku', $idarticolo)->first();
// get the article by product
$article = Article::where('IdArticolo', $idarticolo)->first();
// showarray($article);
showarray($product);
$product_id = $product['id'];
$sku = $product['sku'];
$campoPreOrder = '_wpro_variable_is_preorder';
if ($product) {
$titolo = $product['name'];
echo "Prodotto trovato: " . $product['name'] . "StockQty = " . $product['stock_quantity'] . "<br>";
$preorder = false;
if (isKeyPresent($product['meta_data'], $campoPreOrder)) {
$preorder = getValueByKey($product['meta_data'], $campoPreOrder);
}
if ($preorder) {
if ($preorder === 'true') {
echo "<span class='badge badge-success' style='color: green;'>PREORDER SI !</span>: " . $preorder;
} else {
echo "<span class='badge badge-success' style='color: green;'>PREORDER:</span>: " . $preorder;
}
} else {
echo "<span style='color: red;'>preorder non presente !</span>: ";
}
echo "<br>";
/*foreach ($variations as $variation) {
$found_key_version = array_search('Versione', array_column($variation->attributes, 'name'));
if ($variation->attributes[$found_key_version]->option == 'Nuovo') {
$id = 0;
}
}*/
if ($aggiornapreordine === '1') {
$data = [];
$datenow = date('Y-m-d');
$idprodotto = $product['parent_id'];
if ($idprodotto > 0) {
echo "Variazioni: " . $product['parent_id'] . "<br>";
$variations = Variation::all($product['parent_id']);
showarray($variations);
for ($i = 0; $i < count($variations); $i++) {
$variation = $variations[$i];
if ($variation->id == $product['id']) {
// convert object into array
$data = json_decode(json_encode($variation), true);
break;
}
}
$data['stock_quantity'] = '1000';
echo "Data:";
showarray($data);
$agg = true;
$data['meta_data'] = $product['meta_data'];
if ($agg) {
updateValueByKey($data['meta_data'], $campoPreOrder, 'yes');
updateValueByKey($data['meta_data'], '_is_pre_order', 'yes');
updateValueByKey($data['meta_data'], '_pre_order_date', $datenow);
updateValueByKey($data['meta_data'], '_wpro_date_label_variable', '');
updateValueByKey($data['meta_data'], '_wpro_no_date_label_variable', '');
updateValueByKey($data['meta_data'], '_wpro_manage_price_variable', '');
updateValueByKey($data['meta_data'], '_wpro_price_variable', '');
updateValueByKey($data['meta_data'], '_wpro_label_variable', 'Pre Ordinalo!');
updateValueByKey($data['meta_data'], '_wpro_price_type_variable', 'manual');
updateValueByKey($data['meta_data'], '_wpro_amount_price_variable', 'fixed');
updateValueByKey($data['meta_data'], '_wpro_date_variable', '');
updateValueByKey($data['meta_data'], '_wpro_time_variable', '');
updateValueByKey($data['meta_data'], '_rank_math_gtin_code', '');
}
echo "<br>Dati da Salvare:";
showarray($data);
$variation = Variation::update($idprodotto, $product['id'], $data);
// Product::update($idprodotto, $data);
if ($variation) {
echo "Aggiornato Preorder: [ParentId=" . $idprodotto . '] ProdId= ' . $product['id'] . ' ' . $variation['name'] . "<br>";
}
// $log2 .= $article->Titolo . ' - ' . $article->DescrizioneTipologia . ' - ' . $article->DescrizioneFormato . ' - Articolo aggiornato - ' . $variation['permalink'] . "\n";
}
}
} else {
echo "Il prodotto non esiste";
}
} catch (Exception $e) {
echo "Errore: " . $e->getMessage();
}
setPreOrder($idarticolo, $aggiornapreordine, true);
});
Route::get('/paolibri', function () {