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

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