db = $db; // AJAX add to cart add_action('wp_ajax_studiou_wcfpp_add_to_cart', array($this, 'ajax_add_to_cart')); add_action('wp_ajax_nopriv_studiou_wcfpp_add_to_cart', array($this, 'ajax_add_to_cart')); // Display uploaded file info in cart add_filter('woocommerce_get_item_data', array($this, 'display_cart_item_data'), 10, 2); // Save file reference to order item add_action('woocommerce_checkout_create_order_line_item', array($this, 'save_order_item_meta'), 10, 4); // Link file records to order after checkout add_action('woocommerce_checkout_order_processed', array($this, 'link_files_to_order'), 10, 3); // Ensure cart item data persists add_filter('woocommerce_add_cart_item_data', array($this, 'preserve_cart_item_data'), 10, 3); } public function ajax_add_to_cart() { check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce'); $product_id = isset($_POST['product_id']) ? absint($_POST['product_id']) : 0; $variation_id = isset($_POST['variation_id']) ? absint($_POST['variation_id']) : 0; $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; if (!$product_id || !$variation_id || !$attachment_id || !$file_record_id) { wp_send_json_error(array('message' => __('Missing required data.', 'studiou-wc-free-photo-product'))); return; } // Verify product is FPP enabled if (!Studiou_WC_FPP_Product::is_fpp_product($product_id)) { wp_send_json_error(array('message' => __('Invalid product.', 'studiou-wc-free-photo-product'))); return; } // Verify the file record exists and belongs to this user $record = $this->db->get_file_record($file_record_id); if (!$record || (int) $record->attachment_id !== $attachment_id) { wp_send_json_error(array('message' => __('Invalid file.', 'studiou-wc-free-photo-product'))); return; } // Verify variation belongs to product $variation = wc_get_product($variation_id); if (!$variation || $variation->get_parent_id() !== $product_id) { wp_send_json_error(array('message' => __('Invalid variant.', 'studiou-wc-free-photo-product'))); return; } // Get variation attributes for cart $variation_data = array(); $attributes = $variation->get_variation_attributes(); foreach ($attributes as $key => $value) { $variation_data[$key] = $value; } // Custom cart item data $cart_item_data = array( 'studiou_fpp_attachment_id' => $attachment_id, 'studiou_fpp_file_record_id' => $file_record_id, 'studiou_fpp_file_name' => $record->file_name, ); // Update file record with variation $this->db->update_file_record($file_record_id, array( 'variation_id' => $variation_id, )); // Add to WC cart $cart_item_key = WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation_data, $cart_item_data ); if ($cart_item_key) { wp_send_json_success(array( 'message' => __('Product added to cart.', 'studiou-wc-free-photo-product'), 'cart_item_key' => $cart_item_key, )); } else { wp_send_json_error(array('message' => __('Could not add to cart.', 'studiou-wc-free-photo-product'))); } } public function preserve_cart_item_data($cart_item_data, $product_id, $variation_id) { // Cart item data is already set by add_to_cart call above return $cart_item_data; } public function display_cart_item_data($item_data, $cart_item) { if (isset($cart_item['studiou_fpp_file_name'])) { $item_data[] = array( 'name' => __('Uploaded Photo', 'studiou-wc-free-photo-product'), 'value' => esc_html($cart_item['studiou_fpp_file_name']), 'display' => '', ); } return $item_data; } public function save_order_item_meta($item, $cart_item_key, $values, $order) { if (isset($values['studiou_fpp_attachment_id'])) { $item->add_meta_data('_studiou_fpp_attachment_id', $values['studiou_fpp_attachment_id']); } if (isset($values['studiou_fpp_file_record_id'])) { $item->add_meta_data('_studiou_fpp_file_record_id', $values['studiou_fpp_file_record_id']); } if (isset($values['studiou_fpp_file_name'])) { $item->add_meta_data('_studiou_fpp_file_name', $values['studiou_fpp_file_name']); // Also add a visible meta for admin display $item->add_meta_data(__('Uploaded Photo', 'studiou-wc-free-photo-product'), $values['studiou_fpp_file_name']); } } public function link_files_to_order($order_id, $posted_data, $order) { foreach ($order->get_items() as $item_id => $item) { $file_record_id = $item->get_meta('_studiou_fpp_file_record_id'); if ($file_record_id) { $this->db->link_file_to_order((int) $file_record_id, $order_id, $item_id); } } } }