Dodano funkcję do pobierania wszystkich adresów URL obrazów z danych produktu XML oraz zaktualizowano logikę dodawania obrazów do produktów. Zmieniono domyślną grupę VAT dla produktów oraz poprawiono aktualizację cen wariantów.
This commit is contained in:
@@ -111,18 +111,56 @@ function getCategoryId($categoryName) {
|
||||
}
|
||||
}
|
||||
|
||||
function getAllImageUrlsFromXmlProduct($productData, $max = 10)
|
||||
{
|
||||
$urls = [];
|
||||
|
||||
// główne <image>
|
||||
if (!empty($productData->image)) {
|
||||
$urls[] = trim((string)$productData->image);
|
||||
}
|
||||
|
||||
// images_1..10 oraz Images_1..10 (różna wielkość liter w XML!)
|
||||
for ($i = 1; $i <= $max; $i++) {
|
||||
$k1 = 'images_' . $i;
|
||||
$k2 = 'Images_' . $i;
|
||||
|
||||
$u1 = isset($productData->{$k1}) ? trim((string)$productData->{$k1}) : '';
|
||||
$u2 = isset($productData->{$k2}) ? trim((string)$productData->{$k2}) : '';
|
||||
|
||||
if ($u1 !== '') $urls[] = $u1;
|
||||
if ($u2 !== '') $urls[] = $u2;
|
||||
}
|
||||
|
||||
// usuń duplikaty + puste
|
||||
$urls = array_values(array_unique(array_filter($urls)));
|
||||
|
||||
return $urls;
|
||||
}
|
||||
|
||||
|
||||
// Function to download image from URL and associate it with a product
|
||||
function addProductImage($productId, $imageUrl)
|
||||
function addProductImage($productId, $imageUrl, $isCover = false)
|
||||
{
|
||||
$image = new Image();
|
||||
$image->id_product = (int)$productId;
|
||||
$image->position = Image::getHighestPosition($productId) + 1;
|
||||
$image->cover = true; // Set the first image as cover
|
||||
$image->cover = (bool)$isCover; // tylko pierwsze jako cover
|
||||
$image->add();
|
||||
|
||||
$imagePath = $image->getPathForCreation();
|
||||
$url = str_replace(' ', '%20', $imageUrl);
|
||||
if (!copy($url, $imagePath . '.jpg')) {
|
||||
|
||||
// pobranie pliku
|
||||
$url = str_replace(' ', '%20', trim($imageUrl));
|
||||
|
||||
// UWAGA: copy() czasem failuje na HTTPS; file_get_contents + file_put_contents bywa pewniejsze
|
||||
$data = @file_get_contents($url);
|
||||
if ($data === false) {
|
||||
$image->delete();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (@file_put_contents($imagePath . '.jpg', $data) === false) {
|
||||
$image->delete();
|
||||
return false;
|
||||
}
|
||||
@@ -296,7 +334,7 @@ function getTaxRulesGroupIdForRate($rate, $id_country = null) {
|
||||
ORDER BY trg.`id_tax_rules_group` ASC';
|
||||
|
||||
$id = Db::getInstance()->getValue($sql);
|
||||
return $id ? (int)$id : 0;
|
||||
return $id ? (int)$id : 8;
|
||||
}
|
||||
|
||||
// Zwraca główny produkt z grupy – pierwszy, którego SKU istnieje w PrestaShop
|
||||
@@ -322,7 +360,7 @@ foreach ($xml->product as $productData) {
|
||||
}
|
||||
|
||||
// ID grupy VAT 23%
|
||||
$idTaxRulesGroup23 = getTaxRulesGroupIdForRate(23);
|
||||
$idTaxRulesGroup23 = 8;
|
||||
|
||||
// =======================================
|
||||
// =========== TRYB AKTUALIZACJI =========
|
||||
@@ -527,12 +565,14 @@ if ($modeAdd) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// Główny produkt – dane z pierwszego w grupie
|
||||
// $mainProductData = $products[0];
|
||||
// $mainProduct = findProductByReference((string)$mainProductData->sku);
|
||||
// Główny produkt – wybieramy deterministycznie
|
||||
$mainProductData = findMainProductDataFromGroup($products);
|
||||
if (!$mainProductData) {
|
||||
$mainProductData = $products[0]; // fallback na etapie dodawania
|
||||
// fallback: jeśli żaden nie istnieje, wybierz alfabetycznie najniższy SKU
|
||||
usort($products, function($a, $b) {
|
||||
return strcmp((string)$a->sku, (string)$b->sku);
|
||||
});
|
||||
$mainProductData = $products[0];
|
||||
}
|
||||
$mainProduct = findProductByReference((string)$mainProductData->sku);
|
||||
|
||||
@@ -552,9 +592,7 @@ if ($modeAdd) {
|
||||
$mainProduct->price = $netPrice > 0 ? $netPrice : 0;
|
||||
|
||||
// VAT 23% jeśli dostępny
|
||||
if (!empty($idTaxRulesGroup23)) {
|
||||
$mainProduct->id_tax_rules_group = (int)$idTaxRulesGroup23;
|
||||
}
|
||||
$mainProduct->id_tax_rules_group = 8;
|
||||
|
||||
// Produkt aktywny + delivery times
|
||||
$mainProduct->active = 1;
|
||||
@@ -564,8 +602,14 @@ if ($modeAdd) {
|
||||
$mainProduct->reference = (string)$mainProductData->sku;
|
||||
$mainProduct->id_category_default = 107; // np. Meble
|
||||
$mainProduct->link_rewrite = createLinkRewrite((string)$mainProductData->title);
|
||||
|
||||
// Najpierw dodaj produkt
|
||||
$mainProduct->add();
|
||||
|
||||
$mainProduct = new Product($mainProduct->id);
|
||||
$mainProduct->id_tax_rules_group = 8;
|
||||
$mainProduct->update();
|
||||
|
||||
// ===== Poprawne przypisanie kategorii =====
|
||||
$id_lang = (int)$context->language->id;
|
||||
$defaultCategory = new Category($mainProduct->id_category_default, $id_lang);
|
||||
@@ -584,15 +628,12 @@ if ($modeAdd) {
|
||||
// ===== Koniec ustawienia kategorii =====
|
||||
|
||||
// Add images to the product
|
||||
if (!empty($mainProductData->image)) {
|
||||
addProductImage($mainProduct->id, (string)$mainProductData->image);
|
||||
}
|
||||
$imageUrls = getAllImageUrlsFromXmlProduct($mainProductData, 10);
|
||||
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$imageUrl = (string)$mainProductData->{'images_' . $i};
|
||||
if (!empty($imageUrl)) {
|
||||
addProductImage($mainProduct->id, $imageUrl);
|
||||
}
|
||||
$first = true;
|
||||
foreach ($imageUrls as $url) {
|
||||
addProductImage($mainProduct->id, $url, $first);
|
||||
$first = false;
|
||||
}
|
||||
|
||||
$productAdded = true;
|
||||
@@ -635,19 +676,40 @@ if ($modeAdd) {
|
||||
// Add or update combination if it is unique
|
||||
if (!empty($attributeIds) && !isset($addedCombinations[$key])) {
|
||||
$combination = findCombinationByAttributes($mainProduct->id, $attributeIds);
|
||||
|
||||
// Oblicz cenę wariantu
|
||||
$variantGross = parsePrice((string)$productData->price);
|
||||
$variantNet = $variantGross > 0 ? Tools::ps_round($variantGross / 1.23, 6) : 0;
|
||||
|
||||
// Impact względem ceny bazowej produktu
|
||||
$impact = $variantNet - $netPrice;
|
||||
if ($impact < 0) {
|
||||
$impact = 0; // zabezpieczenie
|
||||
}
|
||||
$impact = Tools::ps_round($impact, 6);
|
||||
|
||||
if (!$combination) {
|
||||
// Create new combination
|
||||
$combination = new Combination();
|
||||
$combination->id_product = (int)$mainProduct->id;
|
||||
$combination->quantity = 100; // startowo, i tak zaraz nadpiszemy StockAvailable
|
||||
$combination->reference = (string)$productData->sku;
|
||||
$combination->price = $impact; // wpływ na cenę (tax excluded)
|
||||
$combination->ecotax = 0;
|
||||
$combination->wholesale_price = 0;
|
||||
$combination->default_on = 0;
|
||||
$combination->add();
|
||||
$combination->setAttributes($attributeIds);
|
||||
$combination->save();
|
||||
|
||||
// Dodatkowy update aby upewnić się że cena jest prawidłowo zapisana
|
||||
$combination->price = $impact;
|
||||
$combination->update();
|
||||
|
||||
$combinationAdded = true;
|
||||
} else {
|
||||
// Update existing combination quantity if necessary
|
||||
// Update existing combination
|
||||
$combination->quantity = 100; // startowo
|
||||
$combination->price = $impact; // aktualizacja ceny
|
||||
$combination->update();
|
||||
}
|
||||
|
||||
@@ -681,6 +743,15 @@ if ($modeAdd) {
|
||||
$mainProduct->checkDefaultAttributes();
|
||||
Product::updateDefaultAttribute($mainProduct->id);
|
||||
|
||||
// Upewnij się że produkt główny ma poprawne ustawienie VAT po dodaniu kombinacji
|
||||
if ($productAdded || $combinationAdded) {
|
||||
$mainProduct = new Product($mainProduct->id); // Przeładuj produkt
|
||||
if (!empty($idTaxRulesGroup23) && (int)$mainProduct->id_tax_rules_group !== (int)$idTaxRulesGroup23) {
|
||||
$mainProduct->id_tax_rules_group = (int)$idTaxRulesGroup23;
|
||||
$mainProduct->update();
|
||||
}
|
||||
}
|
||||
|
||||
if ($productAdded || $combinationAdded) {
|
||||
if ($productAdded) {
|
||||
echo "<p>Dodałem produkt: " . htmlspecialchars((string)$mainProductData->title) . "</p>";
|
||||
|
||||
Reference in New Issue
Block a user