Home / Field guides / Price checks

Price checks · Field guide · Ad

How can you tell whether two prices are comparable?

Suppose you sell a new copy of one edition. A cheaper used copy appears in the search results. Before comparing the prices, establish what each seller is offering.

Includes a ScraperAPI affiliate link. We earn commission on qualifying purchases.

What you are matching

A barcode helps identify a product. The edition, condition and pack size tell you which version the shopper will receive.

In our fictional example, a new copy costs £12 and a used copy costs £8. The used price is useful when checking used stock. It gives you no reason on its own to cut the new price.

Identify the exact product

Keep the barcode as text so a zero at the start survives. Then check the edition and format. If two plausible products share a title and you cannot tell which one the offer describes, leave that result for a person to check.

Compare what the customer pays

Match the condition, currency and number of items in the offer. Include postage consistently. The optional code checks the barcode, format, currency and numeric price after the edition and condition have already been checked.

Check the prices that were saved

Review a few completed lookups. Separate the prices you can use from uncertain matches and requests that failed. If the list of waiting jobs gets shorter but you cannot find good prices in your records, the project still needs work.

Optional: run the code yourself Toggle example

Save as match.php and run php match.php. The matched price is 895 pence. Removing leading zeroes results in no match.

PHP · match.php
<?php
// Map a catalogue hit to a listing only after identifier and format agree.
function priceInPence(array $want, array $hit): ?int
{
    if (!is_string($want['barcode'] ?? null) || $want['barcode'] === ''
        || ($hit['barcode'] ?? null) !== $want['barcode']
        || ($hit['format'] ?? null) !== ($want['format'] ?? null)
        || empty($want['format']) || ($hit['currency'] ?? null) !== 'GBP') {
        return null;
    }
    $price = $hit['price'] ?? '';
    if (!is_string($price) || !preg_match('/^([0-9]{1,6})\.([0-9]{2})$/D', $price, $parts)) {
        return null;
    }
    $pence = (int) $parts[1] * 100 + (int) $parts[2];
    return $pence > 0 ? $pence : null;
}

$want = ['barcode' => '0012345678905', 'format' => 'disc'];
$hit = $want + ['currency' => 'GBP', 'price' => '8.95'];
var_dump(priceInPence($want, $hit));                  // 895
var_dump(priceInPence($want, array_replace($hit,
    ['barcode' => '12345678905'])));                  // null

Further reading · ScraperAPI’s guide to collecting a page