db = $db;
// Capture file data from WC session on add-to-cart
add_filter('woocommerce_add_cart_item_data', array($this, 'add_cart_item_data'), 10, 3);
// Display uploaded file info in cart
add_filter('woocommerce_get_item_data', array($this, 'display_cart_item_data'), 10, 2);
// Replace cart item thumbnail with uploaded image (classic cart)
add_filter('woocommerce_cart_item_thumbnail', array($this, 'cart_item_thumbnail'), 10, 3);
// Show uploaded photo preview directly after item name in cart (classic cart)
add_action('woocommerce_after_cart_item_name', array($this, 'show_cart_item_photo_preview'), 10, 2);
// Override product image for block cart (Store API) — returns uploaded photo as product image
add_filter('woocommerce_product_get_image_id', array($this, 'override_product_image_for_cart'), 10, 2);
add_filter('woocommerce_product_variation_get_image_id', array($this, 'override_product_image_for_cart'), 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 (classic + block checkout)
add_action('woocommerce_checkout_order_processed', array($this, 'link_files_to_order'), 10, 3);
add_action('woocommerce_store_api_checkout_order_processed', array($this, 'link_files_to_order_from_store_api'), 10, 1);
add_action('woocommerce_thankyou', array($this, 'link_files_to_order_fallback'), 10, 1);
// Replace order item thumbnail with uploaded image (admin)
add_filter('woocommerce_admin_order_item_thumbnail', array($this, 'admin_order_item_thumbnail'), 10, 3);
// Add download link to order item in admin
add_action('woocommerce_after_order_itemmeta', array($this, 'admin_order_item_download_link'), 10, 3);
}
public function add_cart_item_data($cart_item_data, $product_id, $variation_id) {
// Check if this product (or its parent) is FPP enabled
$fpp_product_id = $product_id;
if (!Studiou_WC_FPP_Product::is_fpp_product($product_id)) {
$product = wc_get_product($product_id);
if ($product && $product->get_parent_id() && Studiou_WC_FPP_Product::is_fpp_product($product->get_parent_id())) {
$fpp_product_id = $product->get_parent_id();
} else {
return $cart_item_data;
}
}
// Read file data from WC session
if (!WC()->session) {
return $cart_item_data;
}
$attachment_id = WC()->session->get('studiou_fpp_attachment_id');
$file_record_id = WC()->session->get('studiou_fpp_file_record_id');
$file_name = WC()->session->get('studiou_fpp_file_name');
$thumb_url = WC()->session->get('studiou_fpp_thumb_url');
if (!$attachment_id || !$file_record_id) {
return $cart_item_data;
}
// Verify the file record exists
$record = $this->db->get_file_record($file_record_id);
if (!$record || (int) $record->attachment_id !== (int) $attachment_id) {
return $cart_item_data;
}
$cart_item_data['studiou_fpp_attachment_id'] = $attachment_id;
$cart_item_data['studiou_fpp_file_record_id'] = $file_record_id;
$cart_item_data['studiou_fpp_file_name'] = $file_name ?: $record->file_name;
$cart_item_data['studiou_fpp_thumb_url'] = $thumb_url ?: '';
// Update file record with variation
$actual_variation_id = $variation_id ?: $product_id;
$this->db->update_file_record($file_record_id, array(
'variation_id' => $actual_variation_id,
));
// Keep session data so the same photo can be used for multiple variants
return $cart_item_data;
}
public function cart_item_thumbnail($thumbnail, $cart_item, $cart_item_key) {
$url = $this->get_cart_item_thumb_url($cart_item);
if ($url) {
$alt = isset($cart_item['studiou_fpp_file_name']) ? esc_attr($cart_item['studiou_fpp_file_name']) : '';
return '';
}
return $thumbnail;
}
/**
* Override product/variation image_id when rendered in cart context (block cart / Store API).
* Returns the uploaded photo attachment_id so the block cart shows it instead of placeholder.
*/
public function override_product_image_for_cart($image_id, $product) {
if (!WC()->cart) {
return $image_id;
}
$product_id = $product->get_id();
foreach (WC()->cart->get_cart() as $cart_item) {
if (empty($cart_item['studiou_fpp_attachment_id'])) {
continue;
}
$match = false;
if (isset($cart_item['variation_id']) && $cart_item['variation_id'] == $product_id) {
$match = true;
} elseif ($cart_item['product_id'] == $product_id) {
$match = true;
}
if ($match) {
return (int) $cart_item['studiou_fpp_attachment_id'];
}
}
return $image_id;
}
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 show_cart_item_photo_preview($cart_item, $cart_item_key) {
$url = $this->get_cart_item_thumb_url($cart_item);
if ($url) {
echo '
'; echo ''; echo ' '; echo esc_html__('Download uploaded photo', 'studiou-wc-free-photo-product'); echo ''; if ($file_name) { echo ' (' . esc_html($file_name) . ')'; } echo '
'; } }