market|null] */ function usedkit_markets($ids) { $lang = substr(get_locale(), 0, 2); $out = array(); $missing = array(); foreach ($ids as $id) { $m = get_transient('usedkit_market2_' . $id . '_' . $lang); if ($m === false) { $missing[] = $id; } else { $out[$id] = is_array($m) ? $m : null; } } foreach (array_chunk($missing, 100) as $chunk) { $res = usedkit_call('/v1/market/batch', array('external_ids' => array_map('strval', $chunk), 'lang' => $lang)); $found = (!is_wp_error($res) && wp_remote_retrieve_response_code($res) === 200) ? (json_decode(wp_remote_retrieve_body($res), true)['markets'] ?? array()) : null; if ($found === null) { continue; // the service is down: try again next time } foreach ($chunk as $id) { $m = $found[(string) $id] ?? null; set_transient('usedkit_market2_' . $id . '_' . $lang, $m ? $m : 'none', DAY_IN_SECONDS); $out[$id] = $m; } } return $out; } function usedkit_pricing_page() { if (!current_user_can('manage_woocommerce')) { return; } $products = wc_get_products(array('limit' => 300, 'stock_status' => 'instock', 'status' => 'publish', 'orderby' => 'date', 'order' => 'DESC')); $markets = usedkit_markets(array_map(function ($p) { return $p->get_id(); }, $products)); $rows = array(); foreach ($products as $p) { $m = $markets[$p->get_id()] ?? null; $price = (float) $p->get_price(); $diff = null; if ($m && !empty($m['cents']) && $price > 0 && ($m['currency'] ?? '') === get_woocommerce_currency()) { $diff = 100.0 * ($price - $m['cents'] / 100) / ($m['cents'] / 100); } $rows[] = array($p, $price, $m, $diff); } $own = array(); if (function_exists('usedkit_reference_on') && usedkit_reference_on()) { $keys = usedkit_model_keys($products); foreach ($products as $p) { $own[$p->get_id()] = usedkit_reference($keys[$p->get_id()] ?? '', $p->get_id()); } } // Furthest from the market first, either way; no market price last. usort($rows, function ($a, $b) { if ($a[3] === null || $b[3] === null) { return ($a[3] === null) <=> ($b[3] === null); } return abs($b[3]) <=> abs($a[3]); }); $limit = 15.0; ?>

$limit ? 'color:#b32d2e' : ($diff < -$limit ? 'color:#996800' : '')); ?> get_id()]; ?>
get_name()); ?> ' . esc_html($m['price_text']) . '' : '—'; ?> 0 ? '+' : '') . number_format_i18n($diff, 0) . ' %'); if ($diff > $limit) { echo ' ' . esc_html__('above market', 'usedkit'); } elseif ($diff < -$limit) { echo ' ' . esc_html__('below market', 'usedkit'); } } ?>