| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- if (!defined('WPINC')) {
- die;
- }
- class Studiou_WC_FPP_Cart {
- /** @var Studiou_WC_FPP_DB */
- private $db;
- public function __construct($db) {
- $this->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 '<img src="' . esc_url($url) . '" alt="' . $alt . '" class="attachment-woocommerce_thumbnail" />';
- }
- 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 '<div class="studiou-fpp-cart-preview">';
- echo '<img src="' . esc_url($url) . '" alt="' . esc_attr($cart_item['studiou_fpp_file_name'] ?? '') . '" />';
- echo '</div>';
- }
- }
- private function get_cart_item_thumb_url($cart_item) {
- // 1. Use stored URL from session (most reliable)
- if (!empty($cart_item['studiou_fpp_thumb_url'])) {
- return $cart_item['studiou_fpp_thumb_url'];
- }
- // 2. Try WP attachment lookup
- if (!empty($cart_item['studiou_fpp_attachment_id'])) {
- $att_id = (int) $cart_item['studiou_fpp_attachment_id'];
- $thumb = wp_get_attachment_image_src($att_id, 'thumbnail');
- if ($thumb) {
- return $thumb[0];
- }
- $url = wp_get_attachment_url($att_id);
- if ($url) {
- return $url;
- }
- }
- return '';
- }
- 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']);
- $item->add_meta_data(__('Uploaded Photo', 'studiou-wc-free-photo-product'), $values['studiou_fpp_file_name']);
- }
- if (isset($values['studiou_fpp_thumb_url'])) {
- $item->add_meta_data('_studiou_fpp_thumb_url', $values['studiou_fpp_thumb_url']);
- }
- }
- public function link_files_to_order($order_id, $posted_data, $order) {
- $this->do_link_files($order);
- }
- /**
- * Block checkout fires this hook with only the $order parameter.
- */
- public function link_files_to_order_from_store_api($order) {
- $this->do_link_files($order);
- }
- /**
- * Fallback: runs on thank-you page to catch any missed linking.
- */
- public function link_files_to_order_fallback($order_id) {
- $order = wc_get_order($order_id);
- if ($order) {
- $this->do_link_files($order);
- }
- }
- private function do_link_files($order) {
- if (!$order) {
- return;
- }
- $order_id = $order->get_id();
- foreach ($order->get_items() as $item_id => $item) {
- $file_record_id = $item->get_meta('_studiou_fpp_file_record_id');
- if ($file_record_id) {
- // Only link if not already linked
- $record = $this->db->get_file_record((int) $file_record_id);
- if ($record && (int) $record->order_id === 0) {
- $this->db->link_file_to_order((int) $file_record_id, $order_id, $item_id);
- }
- }
- }
- }
- public function admin_order_item_thumbnail($thumbnail, $item_id, $item) {
- // Try stored URL first
- $thumb_url = $item->get_meta('_studiou_fpp_thumb_url');
- if ($thumb_url) {
- return '<img src="' . esc_url($thumb_url) . '" alt="" class="wc-block-components-product-image" width="50" height="50" />';
- }
- // Fallback to attachment lookup
- $attachment_id = $item->get_meta('_studiou_fpp_attachment_id');
- if ($attachment_id) {
- $image = wp_get_attachment_image($attachment_id, 'thumbnail');
- if ($image) {
- return $image;
- }
- $url = wp_get_attachment_url($attachment_id);
- if ($url) {
- return '<img src="' . esc_url($url) . '" alt="" width="50" height="50" />';
- }
- }
- return $thumbnail;
- }
- public function admin_order_item_download_link($item_id, $item, $product) {
- $attachment_id = $item->get_meta('_studiou_fpp_attachment_id');
- if (!$attachment_id) {
- return;
- }
- $file_url = wp_get_attachment_url($attachment_id);
- $file_name = $item->get_meta('_studiou_fpp_file_name');
- if (!$file_url) {
- return;
- }
- $file_record_id = $item->get_meta('_studiou_fpp_file_record_id');
- // Build download URL that also marks file as downloaded
- $download_url = add_query_arg(array(
- 'studiou_fpp_download_file' => 1,
- 'file_record_id' => $file_record_id ?: 0,
- 'attachment_id' => $attachment_id,
- '_wpnonce' => wp_create_nonce('studiou-fpp-download-file'),
- ), admin_url('admin.php'));
- echo '<p class="studiou-fpp-order-download">';
- echo '<a href="' . esc_url($download_url) . '" class="button button-small">';
- echo '<span class="dashicons dashicons-download" style="line-height:1.4;vertical-align:middle;"></span> ';
- echo esc_html__('Download uploaded photo', 'studiou-wc-free-photo-product');
- echo '</a>';
- if ($file_name) {
- echo ' <small>(' . esc_html($file_name) . ')</small>';
- }
- echo '</p>';
- }
- }
|