class-studiou-wc-fpp-cart.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. if (!defined('WPINC')) {
  3. die;
  4. }
  5. class Studiou_WC_FPP_Cart {
  6. /** @var Studiou_WC_FPP_DB */
  7. private $db;
  8. public function __construct($db) {
  9. $this->db = $db;
  10. // Capture file data from WC session on add-to-cart
  11. add_filter('woocommerce_add_cart_item_data', array($this, 'add_cart_item_data'), 10, 3);
  12. // Display uploaded file info in cart
  13. add_filter('woocommerce_get_item_data', array($this, 'display_cart_item_data'), 10, 2);
  14. // Replace cart item thumbnail with uploaded image (classic cart)
  15. add_filter('woocommerce_cart_item_thumbnail', array($this, 'cart_item_thumbnail'), 10, 3);
  16. // Show uploaded photo preview directly after item name in cart (classic cart)
  17. add_action('woocommerce_after_cart_item_name', array($this, 'show_cart_item_photo_preview'), 10, 2);
  18. // Override product image for block cart (Store API) — returns uploaded photo as product image
  19. add_filter('woocommerce_product_get_image_id', array($this, 'override_product_image_for_cart'), 10, 2);
  20. add_filter('woocommerce_product_variation_get_image_id', array($this, 'override_product_image_for_cart'), 10, 2);
  21. // Save file reference to order item
  22. add_action('woocommerce_checkout_create_order_line_item', array($this, 'save_order_item_meta'), 10, 4);
  23. // Link file records to order after checkout
  24. add_action('woocommerce_checkout_order_processed', array($this, 'link_files_to_order'), 10, 3);
  25. // Replace order item thumbnail with uploaded image (admin)
  26. add_filter('woocommerce_admin_order_item_thumbnail', array($this, 'admin_order_item_thumbnail'), 10, 3);
  27. }
  28. public function add_cart_item_data($cart_item_data, $product_id, $variation_id) {
  29. // Check if this product (or its parent) is FPP enabled
  30. $fpp_product_id = $product_id;
  31. if (!Studiou_WC_FPP_Product::is_fpp_product($product_id)) {
  32. $product = wc_get_product($product_id);
  33. if ($product && $product->get_parent_id() && Studiou_WC_FPP_Product::is_fpp_product($product->get_parent_id())) {
  34. $fpp_product_id = $product->get_parent_id();
  35. } else {
  36. return $cart_item_data;
  37. }
  38. }
  39. // Read file data from WC session
  40. if (!WC()->session) {
  41. return $cart_item_data;
  42. }
  43. $attachment_id = WC()->session->get('studiou_fpp_attachment_id');
  44. $file_record_id = WC()->session->get('studiou_fpp_file_record_id');
  45. $file_name = WC()->session->get('studiou_fpp_file_name');
  46. $thumb_url = WC()->session->get('studiou_fpp_thumb_url');
  47. if (!$attachment_id || !$file_record_id) {
  48. return $cart_item_data;
  49. }
  50. // Verify the file record exists
  51. $record = $this->db->get_file_record($file_record_id);
  52. if (!$record || (int) $record->attachment_id !== (int) $attachment_id) {
  53. return $cart_item_data;
  54. }
  55. $cart_item_data['studiou_fpp_attachment_id'] = $attachment_id;
  56. $cart_item_data['studiou_fpp_file_record_id'] = $file_record_id;
  57. $cart_item_data['studiou_fpp_file_name'] = $file_name ?: $record->file_name;
  58. $cart_item_data['studiou_fpp_thumb_url'] = $thumb_url ?: '';
  59. // Update file record with variation
  60. $actual_variation_id = $variation_id ?: $product_id;
  61. $this->db->update_file_record($file_record_id, array(
  62. 'variation_id' => $actual_variation_id,
  63. ));
  64. // Keep session data so the same photo can be used for multiple variants
  65. return $cart_item_data;
  66. }
  67. public function cart_item_thumbnail($thumbnail, $cart_item, $cart_item_key) {
  68. $url = $this->get_cart_item_thumb_url($cart_item);
  69. if ($url) {
  70. $alt = isset($cart_item['studiou_fpp_file_name']) ? esc_attr($cart_item['studiou_fpp_file_name']) : '';
  71. return '<img src="' . esc_url($url) . '" alt="' . $alt . '" class="attachment-woocommerce_thumbnail" />';
  72. }
  73. return $thumbnail;
  74. }
  75. /**
  76. * Override product/variation image_id when rendered in cart context (block cart / Store API).
  77. * Returns the uploaded photo attachment_id so the block cart shows it instead of placeholder.
  78. */
  79. public function override_product_image_for_cart($image_id, $product) {
  80. if (!WC()->cart) {
  81. return $image_id;
  82. }
  83. $product_id = $product->get_id();
  84. foreach (WC()->cart->get_cart() as $cart_item) {
  85. if (empty($cart_item['studiou_fpp_attachment_id'])) {
  86. continue;
  87. }
  88. $match = false;
  89. if (isset($cart_item['variation_id']) && $cart_item['variation_id'] == $product_id) {
  90. $match = true;
  91. } elseif ($cart_item['product_id'] == $product_id) {
  92. $match = true;
  93. }
  94. if ($match) {
  95. return (int) $cart_item['studiou_fpp_attachment_id'];
  96. }
  97. }
  98. return $image_id;
  99. }
  100. public function display_cart_item_data($item_data, $cart_item) {
  101. if (isset($cart_item['studiou_fpp_file_name'])) {
  102. $item_data[] = array(
  103. 'name' => __('Uploaded Photo', 'studiou-wc-free-photo-product'),
  104. 'value' => esc_html($cart_item['studiou_fpp_file_name']),
  105. 'display' => '',
  106. );
  107. }
  108. return $item_data;
  109. }
  110. public function show_cart_item_photo_preview($cart_item, $cart_item_key) {
  111. $url = $this->get_cart_item_thumb_url($cart_item);
  112. if ($url) {
  113. echo '<div class="studiou-fpp-cart-preview">';
  114. echo '<img src="' . esc_url($url) . '" alt="' . esc_attr($cart_item['studiou_fpp_file_name'] ?? '') . '" />';
  115. echo '</div>';
  116. }
  117. }
  118. private function get_cart_item_thumb_url($cart_item) {
  119. // 1. Use stored URL from session (most reliable)
  120. if (!empty($cart_item['studiou_fpp_thumb_url'])) {
  121. return $cart_item['studiou_fpp_thumb_url'];
  122. }
  123. // 2. Try WP attachment lookup
  124. if (!empty($cart_item['studiou_fpp_attachment_id'])) {
  125. $att_id = (int) $cart_item['studiou_fpp_attachment_id'];
  126. $thumb = wp_get_attachment_image_src($att_id, 'thumbnail');
  127. if ($thumb) {
  128. return $thumb[0];
  129. }
  130. $url = wp_get_attachment_url($att_id);
  131. if ($url) {
  132. return $url;
  133. }
  134. }
  135. return '';
  136. }
  137. public function save_order_item_meta($item, $cart_item_key, $values, $order) {
  138. if (isset($values['studiou_fpp_attachment_id'])) {
  139. $item->add_meta_data('_studiou_fpp_attachment_id', $values['studiou_fpp_attachment_id']);
  140. }
  141. if (isset($values['studiou_fpp_file_record_id'])) {
  142. $item->add_meta_data('_studiou_fpp_file_record_id', $values['studiou_fpp_file_record_id']);
  143. }
  144. if (isset($values['studiou_fpp_file_name'])) {
  145. $item->add_meta_data('_studiou_fpp_file_name', $values['studiou_fpp_file_name']);
  146. $item->add_meta_data(__('Uploaded Photo', 'studiou-wc-free-photo-product'), $values['studiou_fpp_file_name']);
  147. }
  148. if (isset($values['studiou_fpp_thumb_url'])) {
  149. $item->add_meta_data('_studiou_fpp_thumb_url', $values['studiou_fpp_thumb_url']);
  150. }
  151. }
  152. public function link_files_to_order($order_id, $posted_data, $order) {
  153. foreach ($order->get_items() as $item_id => $item) {
  154. $file_record_id = $item->get_meta('_studiou_fpp_file_record_id');
  155. if ($file_record_id) {
  156. $this->db->link_file_to_order((int) $file_record_id, $order_id, $item_id);
  157. }
  158. }
  159. }
  160. public function admin_order_item_thumbnail($thumbnail, $item_id, $item) {
  161. // Try stored URL first
  162. $thumb_url = $item->get_meta('_studiou_fpp_thumb_url');
  163. if ($thumb_url) {
  164. return '<img src="' . esc_url($thumb_url) . '" alt="" class="wc-block-components-product-image" width="50" height="50" />';
  165. }
  166. // Fallback to attachment lookup
  167. $attachment_id = $item->get_meta('_studiou_fpp_attachment_id');
  168. if ($attachment_id) {
  169. $image = wp_get_attachment_image($attachment_id, 'thumbnail');
  170. if ($image) {
  171. return $image;
  172. }
  173. $url = wp_get_attachment_url($attachment_id);
  174. if ($url) {
  175. return '<img src="' . esc_url($url) . '" alt="" width="50" height="50" />';
  176. }
  177. }
  178. return $thumbnail;
  179. }
  180. }