Dodanie funkcji do przypisania produktów, kombinacji i obrazów do sklepów oraz skrócenie czasu odświeżania strony po dodaniu produktu lub kombinacji

This commit is contained in:
2025-02-10 19:57:54 +01:00
parent 073c6b2686
commit bc3e4fcf19

View File

@@ -213,6 +213,32 @@ function calculatePriceImpact($basePrice, $combinationPrice) {
return floatval(str_replace(',', '', $combinationPrice)) - floatval(str_replace(',', '', $basePrice));
}
function addProductToShops($product) {
$shops = [1, 2];
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, 2];
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, 2];
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);
@@ -327,8 +353,11 @@ foreach ($productsBySymbol as $symbol => $products) {
$mainProduct->reference = (string)$mainProductData->sku;
$mainProduct->id_category_default = 2;
$mainProduct->link_rewrite = createLinkRewrite((string)$mainProductData->title);
$mainProduct->id_shop_list = [1, 2]; // Przypisanie do sklepów
$mainProduct->add();
addProductToShops($mainProduct);
if (!empty($mainProductData->Cechyproduktu)) {
importProductFeatures($mainProduct->id, (string)$mainProductData->Cechyproduktu);
}
@@ -403,6 +432,9 @@ foreach ($productsBySymbol as $symbol => $products) {
$combination->add();
$combination->setAttributes($attributeIds);
$combination->save();
addCombinationToShops($combination);
$combinationAdded = true;
} else {
// Update existing combination quantity if necessary
@@ -421,7 +453,10 @@ foreach ($productsBySymbol as $symbol => $products) {
for ($i = 1; $i <= 10; $i++) {
$imageUrl = (string)$productData->{'images_' . $i};
if (!empty($imageUrl)) {
addProductImage($mainProduct->id, $imageUrl);
$imageId = addProductImage($mainProduct->id, $imageUrl);
if ($imageId) {
addImageToShops($imageId, $mainProduct->id);
}
}
}
}
@@ -451,6 +486,6 @@ foreach ($productsBySymbol as $symbol => $products) {
}
// reload page after 1s if product or combination was added
if ($productAdded || $combinationAdded) {
echo "<script>setTimeout(function(){location.reload();}, 5000);</script>";
echo "<script>setTimeout(function(){location.reload();}, 2000);</script>";
}
?>