- Implemented a PHP script to delete old product combinations and recreate them based on existing attributes. - The script retrieves product details, existing combinations, and associated attributes from the database. - It deletes old combinations and their associations before inserting new combinations and linking them to shops. - The script ensures that the default combination is set correctly and clears the cache after updates.
134 lines
4.4 KiB
PHP
134 lines
4.4 KiB
PHP
<?php
|
|
/*
|
|
require(dirname(__FILE__).'/config/config.inc.php');
|
|
require(dirname(__FILE__).'/init.php');
|
|
|
|
$id_product = 35778;
|
|
$tablePrefix = 'materac_';
|
|
$db = Db::getInstance();
|
|
$product = new Product($id_product);
|
|
|
|
if (!Validate::isLoadedObject($product)) {
|
|
die("Nie znaleziono produktu ID $id_product\n");
|
|
}
|
|
|
|
// Pobierz wszystkie sklepy
|
|
$shops = $db->executeS("SELECT id_shop FROM {$tablePrefix}shop");
|
|
|
|
// Pobierz stare kombinacje wraz z ich parametrami
|
|
$oldCombinations = $db->executeS("
|
|
SELECT pa.id_product_attribute, pa.price, pa.weight, pa.quantity, pa.wholesale_price,
|
|
pa.ecotax, pa.unit_price_impact, pa.default_on, pa.minimal_quantity, pa.available_date
|
|
FROM {$tablePrefix}product_attribute pa
|
|
WHERE pa.id_product = $id_product
|
|
");
|
|
|
|
if (!$oldCombinations) {
|
|
die("Brak kombinacji do zregenerowania.\n");
|
|
}
|
|
|
|
// Usuń istniejące kombinacje i powiązania
|
|
// Przygotuj zestawy atrybutów do odtworzenia — zanim cokolwiek usuniesz!
|
|
$attributeSets = $db->executeS("
|
|
SELECT pac.id_product_attribute, GROUP_CONCAT(pac.id_attribute ORDER BY pac.id_attribute) as attributes
|
|
FROM {$tablePrefix}product_attribute_combination pac
|
|
WHERE pac.id_product_attribute IN (
|
|
SELECT id_product_attribute FROM {$tablePrefix}product_attribute WHERE id_product = $id_product
|
|
)
|
|
GROUP BY pac.id_product_attribute
|
|
");
|
|
|
|
if (!$attributeSets) {
|
|
die("Brak danych atrybutów do odtworzenia kombinacji. Nic nie zostało usunięte.");
|
|
}
|
|
|
|
// Usuń istniejące kombinacje i powiązania — dopiero teraz!
|
|
foreach ($oldCombinations as $comb) {
|
|
$id_pa = (int)$comb['id_product_attribute'];
|
|
|
|
$db->delete("product_attribute_combination", "id_product_attribute = $id_pa");
|
|
$db->delete("product_attribute_shop", "id_product_attribute = $id_pa");
|
|
$db->delete("product_attribute", "id_product_attribute = $id_pa");
|
|
}
|
|
echo "Usunięto stare kombinacje.\n";
|
|
|
|
|
|
// Mapa: stary id_pa → zestaw atrybutów
|
|
$attributeMap = [];
|
|
foreach ($attributeSets as $set) {
|
|
$attributeMap[$set['id_product_attribute']] = explode(',', $set['attributes']);
|
|
}
|
|
|
|
$newCombinationIds = [];
|
|
$firstDefault = true;
|
|
|
|
foreach ($oldCombinations as $oldComb) {
|
|
$old_pa = (int)$oldComb['id_product_attribute'];
|
|
$attrs = isset($attributeMap[$old_pa]) ? $attributeMap[$old_pa] : [];
|
|
|
|
if (empty($attrs)) {
|
|
echo "⚠️ Pominięto kombinację $old_pa — brak atrybutów.\n";
|
|
continue;
|
|
}
|
|
|
|
// Dodaj nową kombinację
|
|
$new_pa = $db->insert(
|
|
"product_attribute",
|
|
[
|
|
'id_product' => $id_product,
|
|
'price' => $oldComb['price'],
|
|
'wholesale_price' => $oldComb['wholesale_price'],
|
|
'weight' => $oldComb['weight'],
|
|
'quantity' => $oldComb['quantity'],
|
|
'ecotax' => $oldComb['ecotax'],
|
|
'unit_price_impact' => $oldComb['unit_price_impact'],
|
|
'default_on' => $firstDefault ? 1 : 0,
|
|
'minimal_quantity' => $oldComb['minimal_quantity'],
|
|
'available_date' => $oldComb['available_date'],
|
|
],
|
|
true
|
|
);
|
|
|
|
if (!$new_pa) {
|
|
echo "❌ Błąd dodawania nowej kombinacji.\n";
|
|
continue;
|
|
}
|
|
|
|
// Przypisz do sklepów
|
|
foreach ($shops as $shop) {
|
|
$db->insert("product_attribute_shop", [
|
|
'id_product_attribute' => $new_pa,
|
|
'id_product' => $id_product,
|
|
'id_shop' => (int)$shop['id_shop'],
|
|
'price' => $oldComb['price'],
|
|
'wholesale_price' => $oldComb['wholesale_price'],
|
|
'weight' => $oldComb['weight'],
|
|
'ecotax' => $oldComb['ecotax'],
|
|
'default_on' => $firstDefault ? 1 : 0,
|
|
'minimal_quantity' => $oldComb['minimal_quantity'],
|
|
'available_date' => $oldComb['available_date'],
|
|
]);
|
|
}
|
|
|
|
// Wstaw atrybuty
|
|
foreach ($attrs as $attr_id) {
|
|
$db->insert("product_attribute_combination", [
|
|
'id_product_attribute' => $new_pa,
|
|
'id_attribute' => (int)$attr_id,
|
|
]);
|
|
}
|
|
|
|
echo "✅ Dodano nową kombinację ID $new_pa z atrybutami: " . implode(',', $attrs) . "\n";
|
|
|
|
$newCombinationIds[] = $new_pa;
|
|
$firstDefault = false;
|
|
}
|
|
|
|
// Odśwież domyślną kombinację i cache
|
|
$product->checkDefaultAttributes();
|
|
$product->cache_default_attribute = (int)Product::getDefaultAttribute($product->id);
|
|
$product->save();
|
|
|
|
Tools::clearCache();
|
|
echo "🎉 Gotowe! Kombinacje zrekonstruowane i produkt zapisany.\n";
|