fix(prestashop): import combinations as distinct products in 1.1.10
This commit is contained in:
+84
-44
@@ -2,7 +2,11 @@ const { Log, Core } = require("@orbito/api-lib");
|
||||
const PsOrder = require('./models/PsOrder');
|
||||
const { resolveStatusId, normalizeStatusId } = require('./status-mapper');
|
||||
const { paymentTransactionId, parsePrestashopDate, upsertMapping } = require('./automation-jobs');
|
||||
const { resolveProductMetadata } = require('./product-metadata');
|
||||
const {
|
||||
buildProductSourceId,
|
||||
parseProductSourceId,
|
||||
resolveProductMetadata,
|
||||
} = require('./product-metadata');
|
||||
|
||||
function getPSMultiLangField(param, langId) {
|
||||
if (Array.isArray(param)) {
|
||||
@@ -19,6 +23,34 @@ function getPSMultiLangField(param, langId) {
|
||||
return param;
|
||||
}
|
||||
|
||||
function normalizeCombinationId(value) {
|
||||
const id = String(value ?? '').trim();
|
||||
return /^\d+$/.test(id) && id !== '0' ? id : '0';
|
||||
}
|
||||
|
||||
function isCombinationForProduct(combination, productId) {
|
||||
return combination
|
||||
&& String(combination.id_product ?? '').trim() === String(productId ?? '').trim();
|
||||
}
|
||||
|
||||
function resolveProductWeight(product, combination = null) {
|
||||
const baseWeight = Number.parseFloat(product?.weight);
|
||||
if (!combination) return Number.isFinite(baseWeight) ? baseWeight : undefined;
|
||||
|
||||
// In PrestaShop a combination's weight is an impact added to the base
|
||||
// product weight, not the final weight of the variant.
|
||||
const weightImpact = Number.parseFloat(combination.weight);
|
||||
if (Number.isFinite(baseWeight) && Number.isFinite(weightImpact)) return baseWeight + weightImpact;
|
||||
if (Number.isFinite(baseWeight)) return baseWeight;
|
||||
return Number.isFinite(weightImpact) ? weightImpact : undefined;
|
||||
}
|
||||
|
||||
function applyCombinationFields(target, combination) {
|
||||
if (!combination) return;
|
||||
if (String(combination.reference || '').trim()) target.sku = combination.reference;
|
||||
if (Object.hasOwn(combination, 'ean13')) target.ean = combination.ean13 || '';
|
||||
}
|
||||
|
||||
async function resolveImportStatusId(config) {
|
||||
const statusResult = await Core.callModule( 'core/orders', 'status.search', {});
|
||||
const statuses = statusResult?.results ?? [];
|
||||
@@ -55,12 +87,14 @@ async function importFromPSOrder(orderId, api, config) {
|
||||
|
||||
let products = [];
|
||||
for (let product of order.associations.order_rows) {
|
||||
const productId = String(product.product_id);
|
||||
const combinationId = normalizeCombinationId(product.product_attribute_id);
|
||||
let unitPriceNet = parseFloat(product.unit_price_tax_excl); // jednak cen nie możemy zaokrąglać, bo presta pracuje w cenach brutto
|
||||
let unitPriceGross = parseFloat(product.unit_price_tax_incl);
|
||||
let taxRate = unitPriceNet ? (unitPriceGross - unitPriceNet) / unitPriceNet : 0;
|
||||
const p = {
|
||||
name: product.product_name,
|
||||
sourceId: product.product_id,
|
||||
sourceId: buildProductSourceId(productId, combinationId),
|
||||
sku: product.product_reference,
|
||||
ean: product.product_ean13,
|
||||
unitPrice: unitPriceNet,
|
||||
@@ -68,31 +102,31 @@ async function importFromPSOrder(orderId, api, config) {
|
||||
qty: parseFloat(product.product_quantity)
|
||||
};
|
||||
|
||||
const psProduct = await api.getProduct(product.product_id);
|
||||
const psProduct = await api.getProduct(productId);
|
||||
Log.debug( "Fetched product details: " + getPSMultiLangField(psProduct.name));
|
||||
p.weight = parseFloat(psProduct.weight);
|
||||
|
||||
let productCombination = null;
|
||||
if (combinationId !== '0') {
|
||||
const combination = await api.getProductCombination(combinationId);
|
||||
if (isCombinationForProduct(combination, productId)) {
|
||||
productCombination = combination;
|
||||
applyCombinationFields(p, combination);
|
||||
}
|
||||
}
|
||||
const productWeight = resolveProductWeight(psProduct, productCombination);
|
||||
if (productWeight !== undefined) p.weight = productWeight;
|
||||
|
||||
try {
|
||||
const imgUrl = await api.getProductCoverImageUrl(product.product_id);
|
||||
const imgUrl = await api.getProductCoverImageUrl(productId, productCombination);
|
||||
if (imgUrl) p.coverImageUrl = imgUrl;
|
||||
} catch (_) { /* no image available */ }
|
||||
|
||||
let productCombination = null;
|
||||
if (parseInt(product.product_attribute_id) > 0) {
|
||||
const combination = await api.getProductCombination(product.product_attribute_id);
|
||||
if (combination) {
|
||||
productCombination = combination;
|
||||
const w = parseFloat(combination.weight);
|
||||
if (!isNaN(w)) p.weight = w;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Object.assign(p, await resolveProductMetadata(api, psProduct, {
|
||||
sourceId: p.sourceId,
|
||||
sourceId: productId,
|
||||
sku: p.sku,
|
||||
combination: productCombination,
|
||||
requiresCombination: parseInt(product.product_attribute_id) > 0,
|
||||
requiresCombination: combinationId !== '0',
|
||||
}));
|
||||
} catch (_) { /* optional product metadata must not block order import */ }
|
||||
|
||||
@@ -100,38 +134,39 @@ async function importFromPSOrder(orderId, api, config) {
|
||||
p.isPack = true;
|
||||
p.pack = [];
|
||||
for (const packItem of psProduct.associations.product_bundle) {
|
||||
const psItem = await api.getProduct(packItem.id);
|
||||
const packProductId = String(packItem.id);
|
||||
const packCombinationId = normalizeCombinationId(packItem.id_product_attribute);
|
||||
const psItem = await api.getProduct(packProductId);
|
||||
const pData = {
|
||||
name: getPSMultiLangField(psItem.name),
|
||||
sourceId: psItem.id,
|
||||
sourceId: buildProductSourceId(packProductId, packCombinationId),
|
||||
sku: psItem.reference,
|
||||
ean: psItem.ean13,
|
||||
weight: parseFloat(psItem.weight),
|
||||
qty: parseFloat(packItem.quantity)
|
||||
};
|
||||
|
||||
let packCombination = null;
|
||||
if (parseInt(packItem.id_product_attribute) > 0) {
|
||||
const combination = await api.getProductCombination(packItem.id_product_attribute);
|
||||
if (combination) {
|
||||
if (packCombinationId !== '0') {
|
||||
const combination = await api.getProductCombination(packCombinationId);
|
||||
if (isCombinationForProduct(combination, packProductId)) {
|
||||
packCombination = combination;
|
||||
const w = parseFloat(combination.weight);
|
||||
if (!isNaN(w)) pData.weight = w;
|
||||
pData.sku = combination.reference || pData.sku;
|
||||
applyCombinationFields(pData, combination);
|
||||
}
|
||||
}
|
||||
const packWeight = resolveProductWeight(psItem, packCombination);
|
||||
if (packWeight !== undefined) pData.weight = packWeight;
|
||||
|
||||
try {
|
||||
Object.assign(pData, await resolveProductMetadata(api, psItem, {
|
||||
sourceId: pData.sourceId,
|
||||
sourceId: packProductId,
|
||||
sku: pData.sku,
|
||||
combination: packCombination,
|
||||
requiresCombination: parseInt(packItem.id_product_attribute) > 0,
|
||||
requiresCombination: packCombinationId !== '0',
|
||||
}));
|
||||
} catch (_) { /* optional product metadata must not block order import */ }
|
||||
|
||||
try {
|
||||
const imgUrl = await api.getProductCoverImageUrl(packItem.id);
|
||||
const imgUrl = await api.getProductCoverImageUrl(packProductId, packCombination);
|
||||
if (imgUrl) pData.coverImageUrl = imgUrl;
|
||||
} catch (_) { /* no image */ }
|
||||
|
||||
@@ -143,22 +178,27 @@ async function importFromPSOrder(orderId, api, config) {
|
||||
}
|
||||
|
||||
const productsForUpsert = products.flatMap(product => [product, ...(Array.isArray(product.pack) ? product.pack : [])]);
|
||||
await Promise.all(productsForUpsert
|
||||
.filter(product => product.sourceId && product.name)
|
||||
.map(product => {
|
||||
const upsert = {
|
||||
// własny kod modułu bierzemy z core (po rejestracji), nie z hardcode'u
|
||||
sourceModule: Core.getModuleInfo().code,
|
||||
sourceId: String(product.sourceId),
|
||||
for (const product of productsForUpsert.filter(item => item.sourceId && item.name)) {
|
||||
const sourceIdentity = parseProductSourceId(product.sourceId);
|
||||
const upsert = {
|
||||
// własny kod modułu bierzemy z core (po rejestracji), nie z hardcode'u
|
||||
sourceModule: Core.getModuleInfo().code,
|
||||
sourceId: String(product.sourceId),
|
||||
previousSourceId: sourceIdentity?.productId || undefined,
|
||||
sku: product.sku || '',
|
||||
name: product.name,
|
||||
};
|
||||
for (const field of ['unitPrice', 'tax', 'weight', 'ean', 'coverImageUrl', 'brand', 'warehouseLocation']) {
|
||||
if (Object.hasOwn(product, field) && product[field] !== undefined) upsert[field] = product[field];
|
||||
}
|
||||
await Core.callModule('core/products', 'upsert', upsert).catch(error => {
|
||||
Log.warn('PrestaShop product catalog upsert failed', {
|
||||
sourceId: product.sourceId,
|
||||
sku: product.sku || '',
|
||||
name: product.name,
|
||||
};
|
||||
for (const field of ['unitPrice', 'ean', 'coverImageUrl', 'brand', 'warehouseLocation']) {
|
||||
if (Object.hasOwn(product, field) && product[field] !== undefined) upsert[field] = product[field];
|
||||
}
|
||||
return Core.callModule('core/products', 'upsert', upsert).catch(() => {});
|
||||
})
|
||||
);
|
||||
error: error?.message,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const fallbackStatusId = await resolveImportStatusId(config);
|
||||
if (!fallbackStatusId) {
|
||||
|
||||
Reference in New Issue
Block a user