add_action('woocommerce_before_add_to_cart_button', function () {
global $product;
$wf = get_post_meta($product->get_id(), '_fc_workflow_id', true)
?: get_option('fc_default_workflow_id');
if (!$wf) return;
$pk = esc_js(get_option('fc_publishable_key'));
$pid = (int) $product->get_id();
echo "<div id='fc-slot-{$pid}'></div>";
echo "<input type='hidden' name='fc_job_id' id='fc-job-id-{$pid}' value=''>";
wp_enqueue_script('filecheck', "https://cdn.filecheck.io/element/{$pk}/filecheck.js", [], null, true);
wp_add_inline_script('filecheck', "
(function () {
var started = Date.now();
(function tick() {
if (window.Filecheck && window.Filecheck.mount) {
var el = window.Filecheck.mount({
publishableKey: '{$pk}',
workflowId: '{$wf}',
mountSelector: '#fc-slot-{$pid}'
});
if (el) el.on('status', function (e) {
document.getElementById('fc-job-id-{$pid}').value = e.jobId || '';
});
return;
}
if (Date.now() - started < 10000) setTimeout(tick, 50);
})();
})();
");
});