274 lines
10 KiB
PHP
274 lines
10 KiB
PHP
<?php
|
|
// https://pacyga.pl/wp-content/uploads/woo-feed/custom/xml/komplet-produktow-3.xml
|
|
// Include PrestaShop configuration
|
|
$config['update_price'] = false;
|
|
include(dirname(__FILE__).'/config/config.inc.php');
|
|
include(dirname(__FILE__).'/init.php');
|
|
|
|
// Load XML file
|
|
$xml = simplexml_load_file('komplet-produktow-3.xml') or die("Error: Cannot create object");
|
|
|
|
// 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 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();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
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 download image from URL and associate it with a product
|
|
function addProductImage($productId, $imageUrl)
|
|
{
|
|
$image = new Image();
|
|
$image->id_product = $productId;
|
|
$image->position = Image::getHighestPosition($productId) + 1;
|
|
$image->cover = true; // Set the first image as cover
|
|
$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']);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// 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) {
|
|
sort($attributeIds);
|
|
$sql = 'SELECT c.`id_product_attribute`
|
|
FROM `'._DB_PREFIX_.'product_attribute` c
|
|
JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON c.`id_product_attribute` = pac.`id_product_attribute`
|
|
WHERE c.`id_product` = '.(int)$id_product.'
|
|
GROUP BY c.`id_product_attribute`
|
|
HAVING SUM(pac.`id_attribute` = '.implode(' OR pac.`id_attribute` = ', array_map('intval', $attributeIds)).') = '.count($attributeIds);
|
|
$result = Db::getInstance()->getRow($sql);
|
|
return $result ? new Combination($result['id_product_attribute']) : false;
|
|
}
|
|
|
|
// Parse XML and group products by Symbol
|
|
$productsBySymbol = [];
|
|
foreach ($xml->product as $productData) {
|
|
$symbol = (string)$productData->item_group_id;
|
|
if (!isset($productsBySymbol[$symbol])) {
|
|
$productsBySymbol[$symbol] = [];
|
|
}
|
|
$productsBySymbol[$symbol][] = $productData;
|
|
}
|
|
|
|
// Create or update products with combinations based on grouped data
|
|
foreach ($productsBySymbol as $symbol => $products) {
|
|
if (empty($products)) {
|
|
continue;
|
|
}
|
|
|
|
// Get the main product data from the first product in the group
|
|
$mainProductData = $products[0];
|
|
$mainProduct = findProductByReference((string)$mainProductData->sku);
|
|
|
|
$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;
|
|
$mainProduct->id_category_default = 2;
|
|
$mainProduct->link_rewrite = createLinkRewrite((string)$mainProductData->title);
|
|
$mainProduct->add();
|
|
|
|
// Add images to the product
|
|
if (!empty($mainProductData->image)) {
|
|
addProductImage($mainProduct->id, $mainProductData->image);
|
|
}
|
|
|
|
for ($i = 1; $i <= 10; $i++) {
|
|
$imageUrl = (string)$mainProductData->{'images_' . $i};
|
|
if (!empty($imageUrl)) {
|
|
addProductImage($mainProduct->id, $imageUrl);
|
|
}
|
|
}
|
|
|
|
$productAdded = true;
|
|
} else {
|
|
// Update existing product price and description
|
|
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();
|
|
}
|
|
}
|
|
|
|
// 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,
|
|
'Dlugosc' => (string)$productData->Dlugosc,
|
|
'Szerokosc' => (string)$productData->Szerokosc,
|
|
'Glebokosc' => (string)$productData->Glebokosc,
|
|
'Wysokosc' => (string)$productData->Wysokosc,
|
|
];
|
|
|
|
$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
|
|
sort($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;
|
|
$combination->add();
|
|
$combination->setAttributes($attributeIds);
|
|
$combination->save();
|
|
$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) {
|
|
break; // Break if a new combination was added
|
|
}
|
|
}
|
|
|
|
// Ensure the product has combinations enabled
|
|
$mainProduct->checkDefaultAttributes();
|
|
Product::updateDefaultAttribute($mainProduct->id);
|
|
|
|
if ($productAdded || $combinationAdded) {
|
|
if ($productAdded) {
|
|
echo "<p>Dodałem produkt: " . (string)$mainProductData->title . "</p>";
|
|
}
|
|
if ($combinationAdded) {
|
|
echo "<p>Dodałem kombinację: " . (string)$mainProductData->title . "</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>";
|
|
}
|
|
?>
|