db = $db; // Render upload area below the variation table on single product page add_action('woocommerce_single_product_summary', array($this, 'render_upload_area'), 35); // Server-side validation: block add-to-cart without photo (priority 1 = very early) add_filter('woocommerce_add_to_cart_validation', array($this, 'validate_add_to_cart'), 1, 6); // Safety net: if item was added without photo, remove it immediately add_action('woocommerce_add_to_cart', array($this, 'check_after_add_to_cart'), 10, 6); // AJAX: store uploaded file reference in WC session add_action('wp_ajax_studiou_wcfpp_set_upload_session', array($this, 'ajax_set_upload_session')); add_action('wp_ajax_nopriv_studiou_wcfpp_set_upload_session', array($this, 'ajax_set_upload_session')); add_action('wp_ajax_studiou_wcfpp_clear_upload_session', array($this, 'ajax_clear_upload_session')); add_action('wp_ajax_nopriv_studiou_wcfpp_clear_upload_session', array($this, 'ajax_clear_upload_session')); } public function render_upload_area() { global $product; if (!$product || !Studiou_WC_FPP_Product::is_fpp_product($product->get_id())) { return; } wp_enqueue_style('studiou-wcfpp-frontend'); wp_enqueue_script('studiou-wcfpp-frontend'); $product_id = $product->get_id(); $max_file_size = Studiou_WC_FPP_Product::get_product_max_file_size($product_id); // Pass existing state (WC session or cookie fallback) to JS for restore on page load $session_data = array(); $resolved = self::resolve_uploaded_file($this->db); if ($resolved) { $att_id = (int) $resolved['attachment_id']; $thumb = wp_get_attachment_image_src($att_id, 'thumbnail'); $preview = wp_get_attachment_image_src($att_id, 'woocommerce_single'); $session_data = array( 'attachment_id' => $att_id, 'file_record_id' => (int) $resolved['file_record_id'], 'file_name' => $resolved['file_name'], 'thumbnail_url' => $thumb ? $thumb[0] : '', 'preview_url' => $preview ? $preview[0] : ($thumb ? $thumb[0] : ''), ); } wp_localize_script('studiou-wcfpp-frontend', 'studiouWcfppSession', $session_data); include STUDIOU_WCFPP_PLUGIN_DIR . 'views/single-product-upload.php'; } /** * Resolve whether a product_id or its parent is FPP-enabled. */ private function resolve_fpp_product_id($product_id, $variation_id = 0) { // Check direct product if (Studiou_WC_FPP_Product::is_fpp_product($product_id)) { return $product_id; } // Check parent of the product (in case product_id is variation) $product = wc_get_product($product_id); if ($product && $product->get_parent_id() && Studiou_WC_FPP_Product::is_fpp_product($product->get_parent_id())) { return $product->get_parent_id(); } // Check variation_id's parent if ($variation_id && $variation_id !== $product_id) { $variation = wc_get_product($variation_id); if ($variation && $variation->get_parent_id() && Studiou_WC_FPP_Product::is_fpp_product($variation->get_parent_id())) { return $variation->get_parent_id(); } } return false; } /** * Validate before add-to-cart: block if FPP product and no photo reference resolvable. */ public function validate_add_to_cart($passed, $product_id, $quantity, $variation_id = 0, $variations = array(), $cart_item_data = array()) { $fpp_id = $this->resolve_fpp_product_id($product_id, $variation_id); if (!$fpp_id) { return $passed; } if (self::resolve_uploaded_file($this->db)) { return $passed; } wc_add_notice(__('Please upload a photo before adding to cart.', 'studiou-wc-free-photo-product'), 'error'); return false; } /** * Resolve the currently "attached" upload for this visitor, checking WC session first * and falling back to a first-party cookie written at upload time. * * The cookie fallback is required because pvtfw 1.9.3+ / WC 10.7.0+ may route * Add to Cart through the WC Store API, which identifies the session via the * Cart-Token header rather than the classic wp_woocommerce_session_* cookie — * that path sees a different (empty) session than the one the upload wrote to. * * @return array{attachment_id:int,file_record_id:int,file_name:string,thumb_url:string}|null */ public static function resolve_uploaded_file(Studiou_WC_FPP_DB $db) { $attachment_id = 0; $file_record_id = 0; $file_name = ''; $thumb_url = ''; if (function_exists('WC') && WC()->session) { $attachment_id = (int) WC()->session->get('studiou_fpp_attachment_id'); $file_record_id = (int) WC()->session->get('studiou_fpp_file_record_id'); $file_name = (string) WC()->session->get('studiou_fpp_file_name'); $thumb_url = (string) WC()->session->get('studiou_fpp_thumb_url'); } if ($attachment_id <= 0 || $file_record_id <= 0) { $cookie_att = isset($_COOKIE[Studiou_WC_FPP_Upload::COOKIE_ATTACHMENT]) ? (int) $_COOKIE[Studiou_WC_FPP_Upload::COOKIE_ATTACHMENT] : 0; $cookie_rec = isset($_COOKIE[Studiou_WC_FPP_Upload::COOKIE_RECORD]) ? (int) $_COOKIE[Studiou_WC_FPP_Upload::COOKIE_RECORD] : 0; if ($cookie_att > 0 && $cookie_rec > 0) { $attachment_id = $cookie_att; $file_record_id = $cookie_rec; $file_name = ''; $thumb_url = ''; } } if ($attachment_id <= 0 || $file_record_id <= 0) { return null; } $record = $db->get_file_record($file_record_id); if (!$record) { return null; } if ((int) $record->attachment_id !== $attachment_id) { return null; } // A record already attached to an order is no longer "in flight" for add-to-cart if ((int) $record->order_id !== 0) { return null; } if ($file_name === '') { $file_name = (string) $record->file_name; } if ($thumb_url === '') { $thumb = wp_get_attachment_image_src($attachment_id, 'thumbnail'); if ($thumb) { $thumb_url = $thumb[0]; } else { $thumb_url = (string) wp_get_attachment_url($attachment_id); } } return array( 'attachment_id' => $attachment_id, 'file_record_id' => $file_record_id, 'file_name' => $file_name, 'thumb_url' => $thumb_url, ); } /** * Safety net: if an FPP item was added without photo data, remove it. * This catches cases where validation was somehow bypassed. */ public function check_after_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) { $fpp_id = $this->resolve_fpp_product_id($product_id, $variation_id); if (!$fpp_id) { return; } // If the cart item has no FPP attachment, remove it if (empty($cart_item_data['studiou_fpp_attachment_id'])) { WC()->cart->remove_cart_item($cart_item_key); wc_add_notice(__('Please upload a photo before adding to cart.', 'studiou-wc-free-photo-product'), 'error'); } } public function ajax_set_upload_session() { check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce'); $attachment_id = isset($_POST['attachment_id']) ? absint($_POST['attachment_id']) : 0; $file_record_id = isset($_POST['file_record_id']) ? absint($_POST['file_record_id']) : 0; $file_name = isset($_POST['file_name']) ? sanitize_text_field($_POST['file_name']) : ''; if (!$attachment_id || !$file_record_id) { wp_send_json_error(array('message' => __('Invalid file data.', 'studiou-wc-free-photo-product'))); return; } if (WC()->session) { WC()->session->set('studiou_fpp_attachment_id', $attachment_id); WC()->session->set('studiou_fpp_file_record_id', $file_record_id); WC()->session->set('studiou_fpp_file_name', $file_name); // Store thumbnail URL directly so it can be used in cart without wp_get_attachment lookup $thumb_url = ''; $thumb = wp_get_attachment_image_src($attachment_id, 'thumbnail'); if ($thumb) { $thumb_url = $thumb[0]; } else { $thumb_url = wp_get_attachment_url($attachment_id); } WC()->session->set('studiou_fpp_thumb_url', $thumb_url ?: ''); } wp_send_json_success(); } public function ajax_clear_upload_session() { check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce'); if (WC()->session) { WC()->session->set('studiou_fpp_attachment_id', null); WC()->session->set('studiou_fpp_file_record_id', null); WC()->session->set('studiou_fpp_file_name', null); WC()->session->set('studiou_fpp_thumb_url', null); } Studiou_WC_FPP_Upload::clear_upload_cookies(); wp_send_json_success(); } }