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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 (classic + block checkout)
  24. add_action('woocommerce_checkout_order_processed', array($this, 'link_files_to_order'), 10, 3);
  25. add_action('woocommerce_store_api_checkout_order_processed', array($this, 'link_files_to_order_from_store_api'), 10, 1);
  26. add_action('woocommerce_thankyou', array($this, 'link_files_to_order_fallback'), 10, 1);
  27. // Replace order item thumbnail with uploaded image (admin)
  28. add_filter('woocommerce_admin_order_item_thumbnail', array($this, 'admin_order_item_thumbnail'), 10, 3);
  29. }
  30. public function add_cart_item_data($cart_item_data, $product_id, $variation_id) {
  31. // Check if this product (or its parent) is FPP enabled
  32. $fpp_product_id = $product_id;
  33. if (!Studiou_WC_FPP_Product::is_fpp_product($product_id)) {
  34. $product = wc_get_product($product_id);
  35. if ($product && $product->get_parent_id() && Studiou_WC_FPP_Product::is_fpp_product($product->get_parent_id())) {
  36. $fpp_product_id = $product->get_parent_id();
  37. } else {
  38. return $cart_item_data;
  39. }
  40. }
  41. // Read file data from WC session
  42. if (!WC()->session) {
  43. return $cart_item_data;
  44. }
  45. $attachment_id = WC()->session->get('studiou_fpp_attachment_id');
  46. $file_record_id = WC()->session->get('studiou_fpp_file_record_id');
  47. $file_name = WC()->session->get('studiou_fpp_file_name');
  48. $thumb_url = WC()->session->get('studiou_fpp_thumb_url');
  49. if (!$attachment_id || !$file_record_id) {
  50. return $cart_item_data;
  51. }
  52. // Verify the file record exists
  53. $record = $this->db->get_file_record($file_record_id);
  54. if (!$record || (int) $record->attachment_id !== (int) $attachment_id) {
  55. return $cart_item_data;
  56. }
  57. $cart_item_data['studiou_fpp_attachment_id'] = $attachment_id;
  58. $cart_item_data['studiou_fpp_file_record_id'] = $file_record_id;
  59. $cart_item_data['studiou_fpp_file_name'] = $file_name ?: $record->file_name;
  60. $cart_item_data['studiou_fpp_thumb_url'] = $thumb_url ?: '';
  61. // Update file record with variation
  62. $actual_variation_id = $variation_id ?: $product_id;
  63. $this->db->update_file_record($file_record_id, array(
  64. 'variation_id' => $actual_variation_id,
  65. ));
  66. // Keep session data so the same photo can be used for multiple variants
  67. return $cart_item_data;
  68. }
  69. public function cart_item_thumbnail($thumbnail, $cart_item, $cart_item_key) {
  70. $url = $this->get_cart_item_thumb_url($cart_item);
  71. if ($url) {
  72. $alt = isset($cart_item['studiou_fpp_file_name']) ? esc_attr($cart_item['studiou_fpp_file_name']) : '';
  73. return '<img src="' . esc_url($url) . '" alt="' . $alt . '" class="attachment-woocommerce_thumbnail" />';
  74. }
  75. return $thumbnail;
  76. }
  77. /**
  78. * Override product/variation image_id when rendered in cart context (block cart / Store API).
  79. * Returns the uploaded photo attachment_id so the block cart shows it instead of placeholder.
  80. */
  81. public function override_product_image_for_cart($image_id, $product) {
  82. if (!WC()->cart) {
  83. return $image_id;
  84. }
  85. $product_id = $product->get_id();
  86. foreach (WC()->cart->get_cart() as $cart_item) {
  87. if (empty($cart_item['studiou_fpp_attachment_id'])) {
  88. continue;
  89. }
  90. $match = false;
  91. if (isset($cart_item['variation_id']) && $cart_item['variation_id'] == $product_id) {
  92. $match = true;
  93. } elseif ($cart_item['product_id'] == $product_id) {
  94. $match = true;
  95. }
  96. if ($match) {
  97. return (int) $cart_item['studiou_fpp_attachment_id'];
  98. }
  99. }
  100. return $image_id;
  101. }
  102. public function display_cart_item_data($item_data, $cart_item) {
  103. if (isset($cart_item['studiou_fpp_file_name'])) {
  104. $item_data[] = array(
  105. 'name' => __('Uploaded Photo', 'studiou-wc-free-photo-product'),
  106. 'value' => esc_html($cart_item['studiou_fpp_file_name']),
  107. 'display' => '',
  108. );
  109. }
  110. return $item_data;
  111. }
  112. public function show_cart_item_photo_preview($cart_item, $cart_item_key) {
  113. $url = $this->get_cart_item_thumb_url($cart_item);
  114. if ($url) {
  115. echo '<div class="studiou-fpp-cart-preview">';
  116. echo '<img src="' . esc_url($url) . '" alt="' . esc_attr($cart_item['studiou_fpp_file_name'] ?? '') . '" />';
  117. echo '</div>';
  118. }
  119. }
  120. private function get_cart_item_thumb_url($cart_item) {
  121. // 1. Use stored URL from session (most reliable)
  122. if (!empty($cart_item['studiou_fpp_thumb_url'])) {
  123. return $cart_item['studiou_fpp_thumb_url'];
  124. }
  125. // 2. Try WP attachment lookup
  126. if (!empty($cart_item['studiou_fpp_attachment_id'])) {
  127. $att_id = (int) $cart_item['studiou_fpp_attachment_id'];
  128. $thumb = wp_get_attachment_image_src($att_id, 'thumbnail');
  129. if ($thumb) {
  130. return $thumb[0];
  131. }
  132. $url = wp_get_attachment_url($att_id);
  133. if ($url) {
  134. return $url;
  135. }
  136. }
  137. return '';
  138. }
  139. public function save_order_item_meta($item, $cart_item_key, $values, $order) {
  140. if (isset($values['studiou_fpp_attachment_id'])) {
  141. $item->add_meta_data('_studiou_fpp_attachment_id', $values['studiou_fpp_attachment_id']);
  142. }
  143. if (isset($values['studiou_fpp_file_record_id'])) {
  144. $item->add_meta_data('_studiou_fpp_file_record_id', $values['studiou_fpp_file_record_id']);
  145. }
  146. if (isset($values['studiou_fpp_file_name'])) {
  147. $item->add_meta_data('_studiou_fpp_file_name', $values['studiou_fpp_file_name']);
  148. $item->add_meta_data(__('Uploaded Photo', 'studiou-wc-free-photo-product'), $values['studiou_fpp_file_name']);
  149. }
  150. if (isset($values['studiou_fpp_thumb_url'])) {
  151. $item->add_meta_data('_studiou_fpp_thumb_url', $values['studiou_fpp_thumb_url']);
  152. }
  153. }
  154. public function link_files_to_order($order_id, $posted_data, $order) {
  155. $this->do_link_files($order);
  156. }
  157. /**
  158. * Block checkout fires this hook with only the $order parameter.
  159. */
  160. public function link_files_to_order_from_store_api($order) {
  161. $this->do_link_files($order);
  162. }
  163. /**
  164. * Fallback: runs on thank-you page to catch any missed linking.
  165. */
  166. public function link_files_to_order_fallback($order_id) {
  167. $order = wc_get_order($order_id);
  168. if ($order) {
  169. $this->do_link_files($order);
  170. }
  171. }
  172. private function do_link_files($order) {
  173. if (!$order) {
  174. return;
  175. }
  176. $order_id = $order->get_id();
  177. foreach ($order->get_items() as $item_id => $item) {
  178. $file_record_id = $item->get_meta('_studiou_fpp_file_record_id');
  179. if ($file_record_id) {
  180. // Only link if not already linked
  181. $record = $this->db->get_file_record((int) $file_record_id);
  182. if ($record && (int) $record->order_id === 0) {
  183. $this->db->link_file_to_order((int) $file_record_id, $order_id, $item_id);
  184. }
  185. }
  186. }
  187. }
  188. public function admin_order_item_thumbnail($thumbnail, $item_id, $item) {
  189. // Try stored URL first
  190. $thumb_url = $item->get_meta('_studiou_fpp_thumb_url');
  191. if ($thumb_url) {
  192. return '<img src="' . esc_url($thumb_url) . '" alt="" class="wc-block-components-product-image" width="50" height="50" />';
  193. }
  194. // Fallback to attachment lookup
  195. $attachment_id = $item->get_meta('_studiou_fpp_attachment_id');
  196. if ($attachment_id) {
  197. $image = wp_get_attachment_image($attachment_id, 'thumbnail');
  198. if ($image) {
  199. return $image;
  200. }
  201. $url = wp_get_attachment_url($attachment_id);
  202. if ($url) {
  203. return '<img src="' . esc_url($url) . '" alt="" width="50" height="50" />';
  204. }
  205. }
  206. return $thumbnail;
  207. }
  208. }