Files
drmaterac.pl/import-product.php
2025-06-06 00:35:52 +02:00

806 lines
31 KiB
PHP

<?php
// Dołącz pliki konfiguracyjne PrestaShop
include(dirname(__FILE__) . '/config/config.inc.php');
include(dirname(__FILE__) . '/init.php');
libxml_use_internal_errors(true);
$xmlString = file_get_contents( 'https://amz.com.pl/bazy-produktow-export/produkty-amz.xml' );
if ( !$xmlString ) {
die("Error: Cannot load XML file.");
}
$xmlString = preg_replace( '/(<\/?)(\w+):([^>]*>)/', '$1$2_$3', $xmlString );
// replace '-' with '_' in XML tags
$xml = simplexml_load_string( $xmlString, 'SimpleXMLElement', LIBXML_NOCDATA );
if ( !$xml )
{
die("Error: Cannot parse XML");
}
$products_array = [];
// pętla po produktach data -> post
foreach ( $xml -> post as $productData )
{
$product_tmp = (array)$productData;
$title = $product_tmp['Nazwaproduktu'];
$title_md5 = md5( $title );
if ( strpos( $product_tmp['Dostępnerozmiaryjeślidotyczy:untitled_3'], '|' ) !== false )
{
$products_array[$title_md5]['ean'] = $product_tmp['KodEAN-13'];
$products_array[$title_md5]['title'] = $title;
$products_array[$title_md5]['price'] = floatval( str_replace( ',', '', $product_tmp['CenaBRUTTO'] ) ) / 1.23; // cena netto
}
else
{
$combination_tmp['ean'] = $product_tmp['KodEAN-13'];
$combination_tmp['price'] = floatval( str_replace( ',', '', $product_tmp['CenaBRUTTO'] ) ) / 1.23; // cena netto
if ( isset( $products_array[$title_md5] ) )
$products_array[$title_md5]['combinations'][] = $combination_tmp;
else
{
$products_array[$title_md5]['ean'] = $product_tmp['KodEAN-13'];
$products_array[$title_md5]['title'] = $title;
$products_array[$title_md5]['price'] = floatval( str_replace( ',', '', $product_tmp['CenaBRUTTO'] ) ) / 1.23; // cena netto
}
}
}
foreach ( $products_array as $product )
{
if ( !count( $product['combinations'] ) )
$price = $product['price'];
else
$price = 0; // jeśli są kombinacje, to cena jest 0
$reference = $product['ean'];
$active = 0; // Ustawienie produktu jako nieaktywny
// table `materac_product`
if ( $reference )
{
$sql = 'UPDATE `' . _DB_PREFIX_ . 'product` SET `price` = ' . (float)$price . ', `active` = ' . (int)$active . ' WHERE `reference` = \'' . pSQL($reference) . '\'';
$result = Db::getInstance()->execute($sql);
// table `materac_product_shop`
$products_ids = Db::getInstance()->executeS('SELECT id_product FROM `' . _DB_PREFIX_ . 'product` WHERE `reference` = \'' . pSQL($reference) . '\'');
foreach ( $products_ids as $id )
{
$sql_shop = 'UPDATE `' . _DB_PREFIX_ . 'product_shop` SET `price` = ' . (float)$price . ', `active` = ' . (int)$active . ' WHERE `id_product` = ' . (int)$id['id_product'];
$result_shop = Db::getInstance()->execute($sql_shop);
echo "Zaktualizowano produkt z referencją $reference, id_product: " . $id['id_product'] . ", cena: $price, active: $active<br>";
}
}
foreach ( $product['combinations'] as $combination )
{
$sql_combination = 'UPDATE `' . _DB_PREFIX_ . 'product_attribute` SET `price` = ' . (float)$combination['price'] . ' WHERE `reference` = \'' . pSQL($combination['ean']) . '\'';
$result_combination = Db::getInstance()->execute($sql_combination);
$combination_ids = Db::getInstance()->executeS('SELECT id_product_attribute FROM `' . _DB_PREFIX_ . 'product_attribute` WHERE `reference` = \'' . pSQL($combination['ean']) . '\'');
foreach ( $combination_ids as $id_combination )
{
$sql_combination_shop = 'UPDATE `' . _DB_PREFIX_ . 'product_attribute_shop` SET `price` = ' . (float)$combination['price'] . ' WHERE `id_product_attribute` = ' . (int)$id_combination['id_product_attribute'];
$result_combination_shop = Db::getInstance()->execute($sql_combination_shop);
echo "Zaktualizowano kombinację z referencją " . $combination['ean'] . ", id_product_attribute: " . $id_combination['id_product_attribute'] . ", cena: " . $combination['price'] . "<br>";
}
}
}
exit;
// if ( strpos( $product_tmp['Dostępnerozmiaryjeślidotyczy:untitled_3'], '|' ) !== false )
// continue;
// // update price, id_tax_rules_group, active in materac_product where reference = KodEAN-13
// $reference = $product_tmp['KodEAN-13'];
// $price = floatval( str_replace( ',', '', $product_tmp['CenaBRUTTO'] ) ) / 1.23; // cena netto
// $id_tax_rules_group = 62; // ID grupy reguł podatkowych
// $active = 0; // Ustawienie produktu jako aktywnego
// $sql = 'UPDATE `' . _DB_PREFIX_ . 'product`
// SET `price` = ' . 0 . ',
// `id_tax_rules_group` = ' . (int)$id_tax_rules_group . ',
// `active` = ' . (int)$active . '
// WHERE `reference` = \'' . pSQL($reference) . '\'';
// $result = Db::getInstance()->execute($sql);
// // get id_product from materac_product where reference = KodEAN-13
// $id_product = Db::getInstance()->getValue('SELECT id_product FROM `' . _DB_PREFIX_ . 'product` WHERE `reference` = \'' . pSQL($reference) . '\'');
// if ( !$result )
// {
// echo "Error: Cannot update product with reference $reference";
// continue;
// }
// // update materac_product_shop where id_product = id_product
// $sql_shop = 'UPDATE `' . _DB_PREFIX_ . 'product_shop`
// SET `price` = ' . 0 . ',
// `id_tax_rules_group` = ' . (int)$id_tax_rules_group . ',
// `active` = ' . (int)$active . '
// WHERE `id_product` = ' . (int)$id_product;
// $result_shop = Db::getInstance()->execute($sql_shop);
// if ( !$result_shop )
// {
// echo "Error: Cannot update product shop with id_product $id_product";
// continue;
// }
// echo "Zaktualizowano produkt z referencją $reference, id_product: $id_product, cena: $price, id_tax_rules_group: $id_tax_rules_group, active: $active<br>";
// /// update materac_product_attribute by reference
// $sql_combination = 'UPDATE `' . _DB_PREFIX_ . 'product_attribute`
// SET `price` = ' . (float)$price . '
// WHERE `reference` = \'' . pSQL($reference) . '\'';
// $result_combination = Db::getInstance()->execute($sql_combination);
// if ( !$result_combination )
// {
// echo "Error: Cannot update product attribute with reference $reference";
// continue;
// }
// }
// echo '<pre>';
// print_r( $products_array );
// // exit;
// exit;
// Pobierz aktualny język kontekstu (ustaw odpowiedni ID języka, np. 1 dla polskiego)
$id_lang = Context::getContext()->language->id;
// Znajdź wszystkie produkty, których nazwa zawiera " AMZ"
$sql = '
SELECT p.`id_product`
FROM `' . _DB_PREFIX_ . 'product_lang` pl
JOIN `' . _DB_PREFIX_ . 'product` p ON p.`id_product` = pl.`id_product`
WHERE pl.`id_lang` = ' . (int)$id_lang . '
AND pl.`name` LIKE \'% AMZ%\'
';
$products = Db::getInstance()->executeS($sql);
if (!empty($products)) {
foreach ($products as $row) {
$id_product = (int)$row['id_product'];
// Ustaw id_tax_rules_group = 62 w tabeli product
Db::getInstance()->update(
'product',
['id_tax_rules_group' => 62],
'id_product = ' . $id_product
);
// Ustaw id_tax_rules_group = 62 w tabeli product_shop (dla sklepu o ID = 1)
Db::getInstance()->update(
'product_shop',
['id_tax_rules_group' => 62],
'id_product = ' . $id_product
);
// disable product
Db::getInstance()->update(
'product',
['active' => 0],
'id_product = ' . $id_product
);
Db::getInstance()->update(
'product_shop',
['active' => 0],
'id_product = ' . $id_product
);
}
echo 'Zaktualizowano stawkę VAT (62) dla wszystkich produktów zawierających "AMZ" w nazwie.';
} else {
echo 'Nie znaleziono produktów z "AMZ" w nazwie.';
}
die( '-- Import zakończony --' );
// Load XML file
libxml_use_internal_errors(true);
$xmlString = file_get_contents('https://cdn.projectpro.pl/drmaterac.pl/produkty-amz.xml');
$xmlString = preg_replace('/(<\/?)(\w+):([^>]*>)/', '$1$2_$3', $xmlString); // Zamień dwukropki na podkreślniki
$xml = simplexml_load_string($xmlString, 'SimpleXMLElement', LIBXML_NOCDATA);
if (!$xml) {
foreach (libxml_get_errors() as $error) {
echo "XML error: {$error->message}\n";
}
die("Error: Cannot parse XML");
}
$config['update_price'] = false; // Ustaw na true, jeśli chcesz aktualizować ceny produktów
$config['update_tax_id'] = true; // Ustaw na true, jeśli chcesz aktualizować ID podatku
// Logowanie czasu wykonania
function logTime($message, $startTime) {
file_put_contents('import_log.txt', $message . ' - Czas: ' . round(microtime(true) - $startTime, 10) . " s\n", FILE_APPEND);
}
$startGlobal = microtime(true);
// Function to find attribute group by name
function findAttributeGroupByName($name) {
$id_lang = Context::getContext()->language->id;
$result = Db::getInstance()->getRow('SELECT `id_attribute_group` FROM `'._DB_PREFIX_.'attribute_group_lang` WHERE `name` = \''.pSQL($name).'\' AND `id_lang` = '.(int)$id_lang);
return $result ? new AttributeGroup($result['id_attribute_group']) : false;
}
// Function to find attribute by name
function findAttributeByName($id_attribute_group, $name) {
$id_lang = Context::getContext()->language->id;
$sql = 'SELECT a.`id_attribute`
FROM `'._DB_PREFIX_.'attribute` a
JOIN `'._DB_PREFIX_.'attribute_lang` al ON a.`id_attribute` = al.`id_attribute`
WHERE al.`name` = \''.pSQL($name).'\' AND al.`id_lang` = '.(int)$id_lang.' AND a.`id_attribute_group` = '.(int)$id_attribute_group;
$result = Db::getInstance()->getRow($sql);
return $result ? new Attribute($result['id_attribute']) : false;
}
// Function to check if a product already has images
function productHasImages($productId) {
$images = Image::getImages(Context::getContext()->language->id, $productId);
return !empty($images);
}
// Function to create attribute if it doesn't exist
function createAttribute($name, $values) {
$attributeGroup = findAttributeGroupByName($name);
if (!$attributeGroup) {
$attributeGroup = new AttributeGroup();
$attributeGroup->name = createMultiLangField($name);
$attributeGroup->public_name = createMultiLangField($name);
$attributeGroup->group_type = 'select';
$attributeGroup->add();
Db::getInstance()->insert('attribute_group_shop', [
'id_attribute_group' => (int)$attributeGroup->id,
'id_shop' => 1
], false, true, Db::REPLACE);
}
foreach ($values as $value) {
$attribute = findAttributeByName($attributeGroup->id, $value);
if (!$attribute) {
$attribute = new Attribute();
$attribute->id_attribute_group = $attributeGroup->id;
$attribute->name = createMultiLangField($value);
$attribute->add();
Db::getInstance()->insert('attribute_shop', [
'id_attribute' => (int)$attribute->id,
'id_shop' => 1
], false, true, Db::REPLACE);
}
}
return $attributeGroup->id;
}
// Helper function to create a multi-language field
function createMultiLangField($field) {
$languages = Language::getLanguages(false);
$res = [];
foreach ($languages as $lang) {
$res[$lang['id_lang']] = $field;
}
return $res;
}
// Helper function to create a valid link_rewrite
function createLinkRewrite($field) {
$languages = Language::getLanguages(false);
$res = [];
$linkRewrite = Tools::link_rewrite($field); // PrestaShop's function to create valid link_rewrite
foreach ($languages as $lang) {
$res[$lang['id_lang']] = $linkRewrite;
}
return $res;
}
// Function to get category ID from name
function getCategoryId($categoryName) {
$category = Category::searchByName(1, $categoryName); // 1 for default language id
if (!empty($category)) {
return $category['id_category'];
} else {
// Create category if not exists
$category = new Category();
$category->name = createMultiLangField($categoryName);
$category->link_rewrite = createLinkRewrite($categoryName);
$category->id_parent = 2; // Default parent category
$category->add();
return $category->id;
}
}
// Function to log added images
function logAddedImage($productId, $imageUrl) {
$logFile = dirname(__FILE__) . '/image_log.json';
$logData = file_exists($logFile) ? json_decode(file_get_contents($logFile), true) : [];
if (!isset($logData[$productId])) {
$logData[$productId] = [];
}
$logData[$productId][] = $imageUrl;
file_put_contents($logFile, json_encode($logData));
}
// Function to check if an image has been added
function isImageAdded($productId, $imageUrl) {
$logFile = dirname(__FILE__) . '/image_log.json';
if (!file_exists($logFile)) {
return false;
}
$logData = json_decode(file_get_contents($logFile), true);
return isset($logData[$productId]) && in_array($imageUrl, $logData[$productId]);
}
// Function to download image from URL and associate it with a product
function addProductImage($productId, $imageUrl) {
// if (isImageAdded($productId, $imageUrl)) {
// return false; // Skip duplicate images
// }
$image = new Image();
$image->id_product = $productId;
$image->position = Image::getHighestPosition($productId) + 1;
$image->add();
$imagePath = $image->getPathForCreation();
$url = str_replace(' ', '%20', $imageUrl);
if (!copy($url, $imagePath . '.jpg')) {
$image->delete();
return false;
}
$imageTypes = ImageType::getImagesTypes('products');
foreach ($imageTypes as $imageType) {
ImageManager::resize($imagePath . '.jpg', $imagePath . '-' . stripslashes($imageType['name']) . '.jpg', $imageType['width'], $imageType['height']);
}
logAddedImage($productId, $imageUrl);
return $image->id;
}
// Function to set the first image as cover
function setCoverImage($productId) {
$images = Image::getImages(Context::getContext()->language->id, $productId);
if (!empty($images)) {
$firstImage = reset($images);
$firstImageId = $firstImage['id_image'];
Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'image` SET cover = 0 WHERE id_product = '.(int)$productId);
Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'image` SET cover = 1 WHERE id_image = '.(int)$firstImageId);
Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'image_shop` SET cover = 0 WHERE id_product = '.(int)$productId);
Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'image_shop` SET cover = 1 WHERE id_image = '.(int)$firstImageId);
}
}
// Function to find product by reference
function findProductByReference($reference) {
$result = Db::getInstance()->getRow('SELECT `id_product` FROM `'._DB_PREFIX_.'product` WHERE `reference` = \''.pSQL($reference).'\'');
return $result ? new Product($result['id_product']) : false;
}
// Function to find combination by product ID and attribute IDs
function findCombinationByAttributes($id_product, $attributeIds) {
$attributeIds = array_unique($attributeIds);
sort($attributeIds);
$searchKey = implode('-', $attributeIds);
$sql = '
SELECT pa.id_product_attribute
FROM materac_product_attribute pa
JOIN materac_product_attribute_combination pac ON pac.id_product_attribute = pa.id_product_attribute
WHERE pa.id_product = '.(int)$id_product;
$combinations = Db::getInstance()->executeS($sql);
foreach ($combinations as $combo) {
$comboAttrIds = Db::getInstance()->executeS('
SELECT id_attribute FROM materac_product_attribute_combination
WHERE id_product_attribute = '.(int)$combo['id_product_attribute'].'
');
$comboAttrIds = array_column($comboAttrIds, 'id_attribute');
sort($comboAttrIds);
if (implode('-', $comboAttrIds) === $searchKey) {
return new Combination($combo['id_product_attribute']);
}
}
return false;
}
// Function to assign manufacturer to product
function assignManufacturerToProduct($productId, $manufacturerId) {
$product = new Product($productId);
$product->id_manufacturer = $manufacturerId;
$product->update();
}
// Function to assign feature to product (without searching for IDs)
function assignFeatureToProduct($productId, $featureId, $featureValueId) {
$existingFeature = Db::getInstance()->getRow(
'SELECT id_feature_product FROM `'._DB_PREFIX_.'feature_product` WHERE `id_product` = '.(int)$productId.' AND `id_feature` = '.(int)$featureId
);
if (!$existingFeature) {
Db::getInstance()->insert('feature_product', [
'id_product' => (int)$productId,
'id_feature' => (int)$featureId,
'id_feature_value' => (int)$featureValueId
]);
} else {
Db::getInstance()->update('feature_product', [
'id_feature_value' => (int)$featureValueId
], 'id_feature_product = '.(int)$existingFeature['id_feature_product']);
}
}
// Function to update additional delivery parameters
function updateDeliveryParameters($productId) {
$product = new Product($productId);
$product->additional_delivery_times = 2;
$product->delivery_in_stock = 'ok. 2-5 dni roboczych';
$product->delivery_out_stock = 'ok. 2-5 dni roboczych';
$product->update();
}
// Function to calculate combination price impact
function calculatePriceImpact($basePrice, $combinationPrice) {
return floatval(str_replace(',', '', $combinationPrice)) - floatval(str_replace(',', '', $basePrice));
}
function addProductToShops($product) {
$shops = [1];
foreach ($shops as $shopId) {
Db::getInstance()->execute('INSERT IGNORE INTO `'._DB_PREFIX_.'product_shop` (`id_product`, `id_shop`, `id_category_default`, `price`, `active`)
VALUES ('.(int)$product->id.', '.(int)$shopId.', 2, '.(float)$product->price.', 1)');
}
}
// Funkcja do przypisania kombinacji do obu sklepów
function addCombinationToShops($combination) {
$shops = [1];
foreach ($shops as $shopId) {
Db::getInstance()->execute('INSERT IGNORE INTO `'._DB_PREFIX_.'product_attribute_shop` (`id_product_attribute`, `id_product`, `id_shop`, `price`)
VALUES ('.(int)$combination->id.', '.(int)$combination->id_product.', '.(int)$shopId.', '.(float)$combination->price.')');
}
}
// Funkcja do dodawania obrazów do obu sklepów
function addImageToShops($imageId, $productId) {
$shops = [1];
foreach ($shops as $shopId) {
Db::getInstance()->execute('INSERT IGNORE INTO `'._DB_PREFIX_.'image_shop` (`id_image`, `id_product`, `id_shop`, `cover`)
VALUES ('.(int)$imageId.', '.(int)$productId.', '.(int)$shopId.', 0)');
}
}
// Function to parse and import product features
function importProductFeatures($productId, $featuresString) {
$features = explode('|', $featuresString);
foreach ($features as $feature) {
list($featureName, $featureValue) = explode('>', $feature, 2);
if ( trim( $featureValue ) == 'Rozmiar' or $featureValue == 'ROZMIAR' or strpos( $featureName, 'filtr rozwijany' ) !== false )
continue;
$featureName = trim($featureName);
$featureValue = trim($featureValue);
$featureId = Db::getInstance()->getValue(
'SELECT id_feature FROM `'._DB_PREFIX_.'feature_lang` WHERE name = "' . pSQL($featureName) . '"'
);
if (!$featureId) {
$feature = new Feature();
$feature->name = createMultiLangField($featureName);
$feature->add();
$featureId = $feature->id;
}
if ( $featureId == 47 or $featureId == 76 )
continue;
$featureValueId = Db::getInstance()->getValue(
'SELECT fv.id_feature_value FROM `'._DB_PREFIX_.'feature_value` fv
JOIN `'._DB_PREFIX_.'feature_value_lang` fvl ON fv.id_feature_value = fvl.id_feature_value
WHERE fvl.value = "' . pSQL($featureValue) . '" AND fv.id_feature = ' . (int)$featureId
);
if (!$featureValueId) {
$featureValueObj = new FeatureValue();
$featureValueObj->id_feature = $featureId;
$featureValueObj->value = createMultiLangField($featureValue);
$featureValueObj->add();
$featureValueId = $featureValueObj->id;
}
Db::getInstance()->insert('feature_product', [
'id_product' => (int)$productId,
'id_feature' => (int)$featureId,
'id_feature_value' => (int)$featureValueId
]);
}
}
function sortAttributesByGroup($attributeIds) {
$sorted = [];
foreach ($attributeIds as $id) {
$groupId = Db::getInstance()->getValue('SELECT id_attribute_group FROM '._DB_PREFIX_.'attribute WHERE id_attribute = '.(int)$id);
$sorted[$groupId] = $id;
}
ksort($sorted); // Sortuj po ID grupy rosnąco
return array_values($sorted);
}
echo '<pre>';
// Parse XML and group products by Symbol
$productsBySymbol = [];
foreach ( $xml -> post as $productData )
{
$i = 1;
$product_tmp = (array)$productData;
$Dostępnerozmiaryjeślidotyczy = $product_tmp['Dostępnerozmiaryjeślidotyczy:untitled_3'];
$sku = $product_tmp['KodEAN-13'];
$title = $product_tmp['Nazwaproduktu'] . ' AMZ';
$description = $product_tmp['Długiopis'];
$short_description = $product_tmp['Krótkiopis'];
$price = $product_tmp['CenaBRUTTO'];
$rozmiar = $product_tmp['Dostępnerozmiaryjeślidotyczy:untitled_3'];
$kolor = $product_tmp['Dostępnekoloryjeślidotyczy:untitled_2'];
$images = explode( '|', $product_tmp['ZdjęciaproduktuURL-e'] );
if ( !count( $images ) or ( count( $images ) == 1 and empty( $images[0] ) ) )
{
if ( isset( $product_images[ $symbol ]['images'] ) )
$images = $product_images[ $symbol ]['images'];
}
foreach ( $images as $image )
{
$productData->{'images_' . $i} = $image;
$i++;
}
$symbol = md5( $productData -> Nazwaproduktu );
$productData->sku = $sku;
$productData->title = $title;
$productData->description = $description;
$productData->short_description = $short_description;
$productData->price = $price;
$productData->rozmiar = $rozmiar;
$productData->kolor = $kolor;
if ( !isset( $productsBySymbol[$symbol] ) ) {
$productsBySymbol[$symbol] = [];
}
if ( strpos( $Dostępnerozmiaryjeślidotyczy, '|' ) === false )
$productsBySymbol[$symbol][] = $productData;
else
{
$product_images[ $symbol ]['images'] = $images;
}
}
foreach ($productsBySymbol as $symbol => $products)
{
if ( empty( $products ) ) {
continue;
}
$mainProductData = $products[0];
$mainProduct = findProductByReference((string)$mainProductData->sku);
$amz_category = (string)$mainProductData->Kategorieproduktu;
if ( strpos( $amz_category, 'Kołdry' ) !== false ) {
$id_category_default = 60;
} elseif ( strpos( $amx_category, 'Poduszka' ) !== false or strpos( $amz_category, 'poduszka' ) !== false or strpos( $amz_category, 'Poduszki' ) !== false ) {
$id_category_default = 61;
} else {
$id_category_default = 2;
}
// print_r($mainProductData);
$productAdded = false;
if ( !$mainProduct )
{
// Create a new product if it doesn't exist
$mainProduct = new Product();
$mainProduct->name = createMultiLangField((string)$mainProductData->title);
$description = (string)$mainProductData->description;
$description = str_replace("\n", "<br>", $description);
$mainProduct->description = createMultiLangField($description);
$mainProduct->price = floatval(str_replace(',','',$mainProductData->price));
$mainProduct->reference = (string)$mainProductData->sku;
// ean13
$mainProduct->ean13 = (string)$mainProductData->sku;
$mainProduct->id_category_default = $id_category_default;
$mainProduct->link_rewrite = createLinkRewrite((string)$mainProductData->title);
$mainProduct->id_shop_list = [1]; // Przypisanie do sklepów
$mainProduct->active = 1; // Ustawienie produktu jako aktywnego
$mainProduct->id_tax_rules_group = 62; // Ustawienie ID grupy reguł podatkowych
$mainProduct->add();
// ❗ Teraz przypisanie do kategorii
$mainProduct->updateCategories([$id_category_default, $id_category_default]);
foreach ($categories as $catId) {
foreach ([2] as $shopId) { // tu wpisz ID sklepu, np. 2
Db::getInstance()->insert('category_product', [
'id_category' => (int)$catId,
'id_product' => (int)$mainProduct->id,
'position' => 1 // lub kolejność jaką chcesz
], false, true, Db::REPLACE);
}
}
Db::getInstance()->insert('product_shop', [
'id_product' => (int)$mainProduct->id,
'id_shop' => 1,
'id_category_default' => (int)$id_category_default,
'price' => (float)$mainProduct->price,
'active' => 1,
], false, true, Db::REPLACE);
$mainProduct->update();
addProductToShops($mainProduct);
if (!empty($mainProductData->Cechyproduktu)) {
importProductFeatures($mainProduct->id, (string)$mainProductData->Cechyproduktu);
}
// Ensure the product has combinations enabled
$mainProduct->checkDefaultAttributes();
Product::updateDefaultAttribute($mainProduct->id);
assignFeatureToProduct($mainProduct->id, 41, 2499);
assignManufacturerToProduct($mainProduct->id, 53);
updateDeliveryParameters($mainProduct->id);
$productAdded = true;
}
else
{
if ( $config['update_price'] == true )
{
$mainProduct->price = floatval(str_replace(',','',$mainProductData->price));
$description = (string)$mainProductData->description;
$description = str_replace("\n", "<br>", $description);
$mainProduct->description = createMultiLangField($description);
$mainProduct->update();
}
if ( $config['update_tax_id'] == true )
{
// check actual tax ID
$currentTaxId = Db::getInstance()->getValue('SELECT id_tax_rules_group FROM '._DB_PREFIX_.'product WHERE id_product = '.(int)$mainProduct->id);
if ($currentTaxId == 62) {
// echo '<p>Produkt już ma ustawiony ID podatku 62: ' . (string)$mainProductData->title . '</p>';
} else {
// Update tax ID in product column id_tax_rules_group, value 62, table materac_product
Db::getInstance()->update('product', ['id_tax_rules_group' => 62], 'id_product = '.(int)$mainProduct->id);
// also table materac_product_shop
Db::getInstance()->update('product_shop', ['id_tax_rules_group' => 62], 'id_product = '.(int)$mainProduct->id . ' AND id_shop = 1');
echo '<p>Zaktualizowano ID podatku dla produktu: ' . (string)$mainProductData->title . '</p>';
// change product to active by db
Db::getInstance()->update('product', ['active' => 1], 'id_product = '.(int)$mainProduct->id);
Db::getInstance()->update('product_shop', ['active' => 1], 'id_product = '.(int)$mainProduct->id . ' AND id_shop = 1');
}
}
}
// Ensure the product is saved before adding combinations
if (!$mainProduct->id) {
echo "Failed to create or update main product: " . (string)$mainProductData->title . "\n";
continue;
}
// Ensure the combination set is unique for the product
$addedCombinations = [];
// Add or update combinations for each product in the group
$combinationAdded = false;
foreach ($products as $productData)
{
$attributes = [
'Kolor' => (string)$productData->kolor,
'Rozmiar' => (string)$productData->rozmiar
];
$attributeIds = [];
foreach ($attributes as $name => $value) {
if (!empty($value)) {
$attributeGroupId = createAttribute($name, [$value]);
$attribute = findAttributeByName($attributeGroupId, $value);
if ($attribute) {
$attributeIds[] = $attribute->id;
}
}
}
// Create a unique key for the attribute set
$attributeIds = sortAttributesByGroup($attributeIds);
$key = implode('-', $attributeIds);
// Add or update combination if it is unique
if (!empty($attributeIds))
{
$combination = findCombinationByAttributes($mainProduct->id, $attributeIds);
if (!$combination) {
// Create new combination
$combination = new Combination();
$combination->id_product = $mainProduct->id;
$combination->quantity = 100; // Default quantity, you can adjust this
$combination->reference = (string)$productData->sku;
// ean13
$combination->ean13 = (string)$productData->sku;
$combination->id_shop_list = [1]; // Przypisanie do sklepów
$combination->price = calculatePriceImpact($mainProductData->price, $productData->price);
$combination->add();
$attributeIds = array_unique($attributeIds);
$attributeIds = sortAttributesByGroup($attributeIds); // funkcja z poprzedniej wiadomości
$combination->setAttributes($attributeIds);
$combination->save();
if (isset($addedCombinations[$key])) {
continue; // już dodana
if (!$combination) {}
addCombinationToShops($combination);
}
$combinationAdded = true;
} else {
// Update existing combination quantity if necessary
$combination->quantity = 100; // Update quantity, you can adjust this
$combination->update();
}
// Mark this combination as added
$addedCombinations[$key] = true;
}
if ( $combinationAdded )
{
if ( !productHasImages($mainProduct->id) and !empty($productData->images_2) )
{
for ($i = 1; $i <= 10; $i++) {
$imageUrl = (string)$productData->{'images_' . $i};
if (!empty($imageUrl)) {
$imageId = addProductImage($mainProduct->id, $imageUrl);
if ($imageId) {
addImageToShops($imageId, $mainProduct->id);
}
}
}
}
setCoverImage($mainProduct->id);
break; // Break if a new combination was added
}
}
if ($productAdded || $combinationAdded) {
if ($productAdded) {
echo "<p>Dodałem produkt: " . (string)$mainProductData->title . "</p>";
}
if ($combinationAdded) {
echo "<p>Dodałem kombinację: " . (string)$mainProductData->title . ". Produkt ID: " . $mainProduct->id . "</p>";
}
break; // Break if a new product or combination was added
}
}
// reload page after 1s if product or combination was added
if ($productAdded || $combinationAdded) {
// echo "<script>setTimeout(function(){location.reload();}, 250);</script>";
}
?>