mirror of
https://github.com/clucraft/PriceGhost.git
synced 2026-06-08 15:05:16 +02:00
Add Magento price selector support
- Added data-price-amount selector (Magento 2 stores numeric price here) - Added .price-wrapper, .price-box, .special-price selectors - Extract price directly from data-price-amount attribute - Detect currency from surrounding text when using numeric attribute - Fixes CHF price extraction from Magento/Degussa gold shop Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
925ff1cb27
commit
5a682276b6
1 changed files with 26 additions and 2 deletions
|
|
@ -686,8 +686,12 @@ const siteScrapers: SiteScraper[] = [
|
||||||
// Generic selectors as fallback
|
// Generic selectors as fallback
|
||||||
const genericPriceSelectors = [
|
const genericPriceSelectors = [
|
||||||
'[itemprop="price"]',
|
'[itemprop="price"]',
|
||||||
|
'[data-price-amount]', // Magento 2
|
||||||
'[data-price]',
|
'[data-price]',
|
||||||
'[data-product-price]',
|
'[data-product-price]',
|
||||||
|
'.price-wrapper [data-price-amount]', // Magento 2 price wrapper
|
||||||
|
'.price-box .price', // Magento price box
|
||||||
|
'.special-price .price', // Magento special/sale price
|
||||||
'.price',
|
'.price',
|
||||||
'.product-price',
|
'.product-price',
|
||||||
'.current-price',
|
'.current-price',
|
||||||
|
|
@ -1074,8 +1078,28 @@ function extractGenericPrice($: CheerioAPI): ParsedPrice | null {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const text = $el.attr('content') || $el.attr('data-price') || $el.text();
|
// Check various attributes where price might be stored
|
||||||
const parsed = parsePrice(text);
|
const priceAmount = $el.attr('data-price-amount'); // Magento 2
|
||||||
|
const dataPrice = $el.attr('data-price');
|
||||||
|
const content = $el.attr('content');
|
||||||
|
const text = $el.text();
|
||||||
|
|
||||||
|
// Try data-price-amount first (Magento stores numeric value here)
|
||||||
|
if (priceAmount) {
|
||||||
|
const price = parseFloat(priceAmount);
|
||||||
|
if (!isNaN(price) && price > 0) {
|
||||||
|
// Try to detect currency from nearby elements or default to USD
|
||||||
|
const currencyEl = $el.closest('.price-box').find('[data-price-type]').first();
|
||||||
|
const priceText = currencyEl.length ? currencyEl.text() : text;
|
||||||
|
const currencyMatch = priceText.match(/([A-Z]{3}|[$€£¥₹])/);
|
||||||
|
const currency = currencyMatch ? (currencyMatch[1] === '$' ? 'USD' : currencyMatch[1] === '€' ? 'EUR' : currencyMatch[1] === '£' ? 'GBP' : currencyMatch[1]) : 'USD';
|
||||||
|
prices.push({ price, currency });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const priceStr = content || dataPrice || text;
|
||||||
|
const parsed = parsePrice(priceStr);
|
||||||
if (parsed && parsed.price > 0) {
|
if (parsed && parsed.price > 0) {
|
||||||
prices.push(parsed);
|
prices.push(parsed);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue