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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. // New per-card add-to-cart / remove-line endpoints (1.5.0)
  11. add_action('wp_ajax_studiou_wcfpp_add_file_to_cart', array($this, 'ajax_add_file_to_cart'));
  12. add_action('wp_ajax_nopriv_studiou_wcfpp_add_file_to_cart', array($this, 'ajax_add_file_to_cart'));
  13. add_action('wp_ajax_studiou_wcfpp_remove_cart_line', array($this, 'ajax_remove_cart_line'));
  14. add_action('wp_ajax_nopriv_studiou_wcfpp_remove_cart_line', array($this, 'ajax_remove_cart_line'));
  15. // Capture file data from WC session on add-to-cart
  16. add_filter('woocommerce_add_cart_item_data', array($this, 'add_cart_item_data'), 10, 3);
  17. // Display uploaded file info in cart
  18. add_filter('woocommerce_get_item_data', array($this, 'display_cart_item_data'), 10, 2);
  19. // Replace cart item thumbnail with uploaded image (classic cart)
  20. add_filter('woocommerce_cart_item_thumbnail', array($this, 'cart_item_thumbnail'), 10, 3);
  21. // Show uploaded photo preview directly after item name in cart (classic cart)
  22. add_action('woocommerce_after_cart_item_name', array($this, 'show_cart_item_photo_preview'), 10, 2);
  23. // Override product image for block cart (Store API) — returns uploaded photo as product image
  24. add_filter('woocommerce_product_get_image_id', array($this, 'override_product_image_for_cart'), 10, 2);
  25. add_filter('woocommerce_product_variation_get_image_id', array($this, 'override_product_image_for_cart'), 10, 2);
  26. // Save file reference to order item
  27. add_action('woocommerce_checkout_create_order_line_item', array($this, 'save_order_item_meta'), 10, 4);
  28. // Link file records to order after checkout (classic + block checkout)
  29. add_action('woocommerce_checkout_order_processed', array($this, 'link_files_to_order'), 10, 3);
  30. add_action('woocommerce_store_api_checkout_order_processed', array($this, 'link_files_to_order_from_store_api'), 10, 1);
  31. add_action('woocommerce_thankyou', array($this, 'link_files_to_order_fallback'), 10, 1);
  32. // Replace order item thumbnail with uploaded image (admin)
  33. add_filter('woocommerce_admin_order_item_thumbnail', array($this, 'admin_order_item_thumbnail'), 10, 3);
  34. // Add download link to order item in admin
  35. add_action('woocommerce_after_order_itemmeta', array($this, 'admin_order_item_download_link'), 10, 3);
  36. }
  37. public function add_cart_item_data($cart_item_data, $product_id, $variation_id) {
  38. // Check if this product (or its parent) is FPP enabled
  39. $fpp_product_id = $product_id;
  40. if (!Studiou_WC_FPP_Product::is_fpp_product($product_id)) {
  41. $product = wc_get_product($product_id);
  42. if ($product && $product->get_parent_id() && Studiou_WC_FPP_Product::is_fpp_product($product->get_parent_id())) {
  43. $fpp_product_id = $product->get_parent_id();
  44. } else {
  45. return $cart_item_data;
  46. }
  47. }
  48. // 1) New path (1.5.0+): per-card Add-to-Cart pre-stamped the file_record_id.
  49. if (!empty($cart_item_data['studiou_fpp_file_record_id'])) {
  50. $record = $this->db->get_file_record((int) $cart_item_data['studiou_fpp_file_record_id']);
  51. if ($record && (int) $record->order_id === 0) {
  52. $thumb = wp_get_attachment_image_src((int) $record->attachment_id, 'thumbnail');
  53. $cart_item_data['studiou_fpp_attachment_id'] = (int) $record->attachment_id;
  54. $cart_item_data['studiou_fpp_file_name'] = (string) $record->file_name;
  55. $cart_item_data['studiou_fpp_thumb_url'] = $thumb ? $thumb[0] : '';
  56. $this->db->update_file_record((int) $record->id, array(
  57. 'variation_id' => (int) ($variation_id ?: $product_id),
  58. ));
  59. return $cart_item_data;
  60. }
  61. // Stamped id is bogus — fall through to legacy resolver
  62. unset($cart_item_data['studiou_fpp_file_record_id']);
  63. }
  64. // 2) Legacy single-file path (shortcode / non-card callers) — fall back to the
  65. // oldest unbound upload in the current batch.
  66. $resolved = Studiou_WC_FPP_Single_Product::resolve_uploaded_file($this->db);
  67. if (!$resolved) {
  68. return $cart_item_data;
  69. }
  70. $cart_item_data['studiou_fpp_attachment_id'] = (int) $resolved['attachment_id'];
  71. $cart_item_data['studiou_fpp_file_record_id'] = (int) $resolved['file_record_id'];
  72. $cart_item_data['studiou_fpp_file_name'] = $resolved['file_name'];
  73. $cart_item_data['studiou_fpp_thumb_url'] = $resolved['thumb_url'];
  74. $this->db->update_file_record((int) $resolved['file_record_id'], array(
  75. 'variation_id' => (int) ($variation_id ?: $product_id),
  76. ));
  77. return $cart_item_data;
  78. }
  79. /**
  80. * AJAX: add one (upload × variant × qty) triple as a new cart line.
  81. * Called from the upload-card "Add to Cart" button.
  82. */
  83. public function ajax_add_file_to_cart() {
  84. check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
  85. $file_record_id = isset($_POST['file_record_id']) ? absint($_POST['file_record_id']) : 0;
  86. $variation_id = isset($_POST['variation_id']) ? absint($_POST['variation_id']) : 0;
  87. $quantity = isset($_POST['quantity']) ? max(1, absint($_POST['quantity'])) : 1;
  88. if (!$file_record_id || !$variation_id) {
  89. wp_send_json_error(array('message' => __('Invalid request.', 'studiou-wc-free-photo-product')));
  90. return;
  91. }
  92. $record = $this->db->get_file_record($file_record_id);
  93. if (!$record || (int) $record->order_id !== 0) {
  94. wp_send_json_error(array('message' => __('File not found.', 'studiou-wc-free-photo-product')));
  95. return;
  96. }
  97. // Ownership check: record must belong to the caller's batch
  98. $token = Studiou_WC_FPP_Upload::get_batch_token();
  99. if (!empty($token) && !empty($record->batch_token) && $token !== $record->batch_token) {
  100. wp_send_json_error(array('message' => __('Permission denied.', 'studiou-wc-free-photo-product')));
  101. return;
  102. }
  103. $variation = wc_get_product($variation_id);
  104. if (!$variation || $variation->get_type() !== 'variation') {
  105. wp_send_json_error(array('message' => __('Invalid variant.', 'studiou-wc-free-photo-product')));
  106. return;
  107. }
  108. $parent_id = $variation->get_parent_id();
  109. if (!Studiou_WC_FPP_Product::is_fpp_product($parent_id)) {
  110. wp_send_json_error(array('message' => __('Invalid product.', 'studiou-wc-free-photo-product')));
  111. return;
  112. }
  113. if (function_exists('wc_load_cart')) {
  114. wc_load_cart();
  115. }
  116. if (!WC()->cart) {
  117. wp_send_json_error(array('message' => __('Cart unavailable.', 'studiou-wc-free-photo-product')));
  118. return;
  119. }
  120. $variation_attrs = $variation->get_variation_attributes();
  121. $cart_item_data = array('studiou_fpp_file_record_id' => (int) $file_record_id);
  122. $cart_key = WC()->cart->add_to_cart($parent_id, $quantity, $variation_id, $variation_attrs, $cart_item_data);
  123. if (!$cart_key) {
  124. // WC emits notices on failure — surface the first error if available
  125. $notices = function_exists('wc_get_notices') ? wc_get_notices('error') : array();
  126. $msg = __('Could not add to cart.', 'studiou-wc-free-photo-product');
  127. if (!empty($notices[0]['notice'])) {
  128. $msg = wp_strip_all_tags($notices[0]['notice']);
  129. }
  130. if (function_exists('wc_clear_notices')) {
  131. wc_clear_notices();
  132. }
  133. wp_send_json_error(array('message' => $msg));
  134. return;
  135. }
  136. wp_send_json_success(array(
  137. 'cart_key' => $cart_key,
  138. 'overview_html' => $this->render_overview_html($parent_id),
  139. ));
  140. }
  141. /**
  142. * AJAX: remove one cart line (from the order-overview panel).
  143. * Does NOT delete the upload — the × on the upload card handles that.
  144. */
  145. public function ajax_remove_cart_line() {
  146. check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
  147. $cart_item_key = isset($_POST['cart_item_key']) ? sanitize_text_field($_POST['cart_item_key']) : '';
  148. if (!$cart_item_key) {
  149. wp_send_json_error(array('message' => __('Invalid request.', 'studiou-wc-free-photo-product')));
  150. return;
  151. }
  152. if (function_exists('wc_load_cart')) {
  153. wc_load_cart();
  154. }
  155. if (!WC()->cart) {
  156. wp_send_json_error(array('message' => __('Cart unavailable.', 'studiou-wc-free-photo-product')));
  157. return;
  158. }
  159. $item = WC()->cart->get_cart_item($cart_item_key);
  160. if (!$item) {
  161. wp_send_json_error(array('message' => __('Cart line not found.', 'studiou-wc-free-photo-product')));
  162. return;
  163. }
  164. $product_id = (int) ($item['product_id'] ?? 0);
  165. WC()->cart->remove_cart_item($cart_item_key);
  166. wp_send_json_success(array(
  167. 'overview_html' => $this->render_overview_html($product_id),
  168. ));
  169. }
  170. /**
  171. * Render the order-overview panel for the given product. Returns the HTML
  172. * so the JS can replace the in-page panel.
  173. */
  174. private function render_overview_html($product_id) {
  175. if (!$product_id) {
  176. return '';
  177. }
  178. $cart = WC()->cart;
  179. if (!$cart) {
  180. return '';
  181. }
  182. // Force a totals recalculation so the lines reflect the tier discount
  183. $cart->calculate_totals();
  184. ob_start();
  185. include STUDIOU_WCFPP_PLUGIN_DIR . 'views/single-product-order-overview.php';
  186. return ob_get_clean();
  187. }
  188. public function cart_item_thumbnail($thumbnail, $cart_item, $cart_item_key) {
  189. $url = $this->get_cart_item_thumb_url($cart_item);
  190. if ($url) {
  191. $alt = isset($cart_item['studiou_fpp_file_name']) ? esc_attr($cart_item['studiou_fpp_file_name']) : '';
  192. return '<img src="' . esc_url($url) . '" alt="' . $alt . '" class="attachment-woocommerce_thumbnail" />';
  193. }
  194. return $thumbnail;
  195. }
  196. /**
  197. * Override product/variation image_id when rendered in cart context (block cart / Store API).
  198. * Returns the uploaded photo attachment_id so the block cart shows it instead of placeholder.
  199. */
  200. public function override_product_image_for_cart($image_id, $product) {
  201. if (!WC()->cart) {
  202. return $image_id;
  203. }
  204. $product_id = $product->get_id();
  205. foreach (WC()->cart->get_cart() as $cart_item) {
  206. if (empty($cart_item['studiou_fpp_attachment_id'])) {
  207. continue;
  208. }
  209. $match = false;
  210. if (isset($cart_item['variation_id']) && $cart_item['variation_id'] == $product_id) {
  211. $match = true;
  212. } elseif ($cart_item['product_id'] == $product_id) {
  213. $match = true;
  214. }
  215. if ($match) {
  216. return (int) $cart_item['studiou_fpp_attachment_id'];
  217. }
  218. }
  219. return $image_id;
  220. }
  221. public function display_cart_item_data($item_data, $cart_item) {
  222. if (isset($cart_item['studiou_fpp_file_name'])) {
  223. $item_data[] = array(
  224. 'name' => __('Uploaded Photo', 'studiou-wc-free-photo-product'),
  225. 'value' => esc_html($cart_item['studiou_fpp_file_name']),
  226. 'display' => '',
  227. );
  228. }
  229. return $item_data;
  230. }
  231. public function show_cart_item_photo_preview($cart_item, $cart_item_key) {
  232. $url = $this->get_cart_item_thumb_url($cart_item);
  233. if ($url) {
  234. echo '<div class="studiou-fpp-cart-preview">';
  235. echo '<img src="' . esc_url($url) . '" alt="' . esc_attr($cart_item['studiou_fpp_file_name'] ?? '') . '" />';
  236. echo '</div>';
  237. }
  238. }
  239. private function get_cart_item_thumb_url($cart_item) {
  240. // 1. Use stored URL from session (most reliable)
  241. if (!empty($cart_item['studiou_fpp_thumb_url'])) {
  242. return $cart_item['studiou_fpp_thumb_url'];
  243. }
  244. // 2. Try WP attachment lookup
  245. if (!empty($cart_item['studiou_fpp_attachment_id'])) {
  246. $att_id = (int) $cart_item['studiou_fpp_attachment_id'];
  247. $thumb = wp_get_attachment_image_src($att_id, 'thumbnail');
  248. if ($thumb) {
  249. return $thumb[0];
  250. }
  251. $url = wp_get_attachment_url($att_id);
  252. if ($url) {
  253. return $url;
  254. }
  255. }
  256. return '';
  257. }
  258. public function save_order_item_meta($item, $cart_item_key, $values, $order) {
  259. if (isset($values['studiou_fpp_attachment_id'])) {
  260. $item->add_meta_data('_studiou_fpp_attachment_id', $values['studiou_fpp_attachment_id']);
  261. }
  262. if (isset($values['studiou_fpp_file_record_id'])) {
  263. $item->add_meta_data('_studiou_fpp_file_record_id', $values['studiou_fpp_file_record_id']);
  264. }
  265. if (isset($values['studiou_fpp_file_name'])) {
  266. $item->add_meta_data('_studiou_fpp_file_name', $values['studiou_fpp_file_name']);
  267. $item->add_meta_data(__('Uploaded Photo', 'studiou-wc-free-photo-product'), $values['studiou_fpp_file_name']);
  268. }
  269. if (isset($values['studiou_fpp_thumb_url'])) {
  270. $item->add_meta_data('_studiou_fpp_thumb_url', $values['studiou_fpp_thumb_url']);
  271. }
  272. }
  273. public function link_files_to_order($order_id, $posted_data, $order) {
  274. $this->do_link_files($order);
  275. }
  276. /**
  277. * Block checkout fires this hook with only the $order parameter.
  278. */
  279. public function link_files_to_order_from_store_api($order) {
  280. $this->do_link_files($order);
  281. }
  282. /**
  283. * Fallback: runs on thank-you page to catch any missed linking.
  284. */
  285. public function link_files_to_order_fallback($order_id) {
  286. $order = wc_get_order($order_id);
  287. if ($order) {
  288. $this->do_link_files($order);
  289. }
  290. }
  291. private function do_link_files($order) {
  292. if (!$order) {
  293. return;
  294. }
  295. $order_id = $order->get_id();
  296. foreach ($order->get_items() as $item_id => $item) {
  297. $file_record_id = $item->get_meta('_studiou_fpp_file_record_id');
  298. if ($file_record_id) {
  299. // Only link if not already linked
  300. $record = $this->db->get_file_record((int) $file_record_id);
  301. if ($record && (int) $record->order_id === 0) {
  302. $this->db->link_file_to_order((int) $file_record_id, $order_id, $item_id);
  303. }
  304. }
  305. }
  306. }
  307. public function admin_order_item_thumbnail($thumbnail, $item_id, $item) {
  308. // Try stored URL first
  309. $thumb_url = $item->get_meta('_studiou_fpp_thumb_url');
  310. if ($thumb_url) {
  311. return '<img src="' . esc_url($thumb_url) . '" alt="" class="wc-block-components-product-image" width="50" height="50" />';
  312. }
  313. // Fallback to attachment lookup
  314. $attachment_id = $item->get_meta('_studiou_fpp_attachment_id');
  315. if ($attachment_id) {
  316. $image = wp_get_attachment_image($attachment_id, 'thumbnail');
  317. if ($image) {
  318. return $image;
  319. }
  320. $url = wp_get_attachment_url($attachment_id);
  321. if ($url) {
  322. return '<img src="' . esc_url($url) . '" alt="" width="50" height="50" />';
  323. }
  324. }
  325. return $thumbnail;
  326. }
  327. public function admin_order_item_download_link($item_id, $item, $product) {
  328. $attachment_id = $item->get_meta('_studiou_fpp_attachment_id');
  329. if (!$attachment_id) {
  330. return;
  331. }
  332. $file_url = wp_get_attachment_url($attachment_id);
  333. $file_name = $item->get_meta('_studiou_fpp_file_name');
  334. if (!$file_url) {
  335. return;
  336. }
  337. $file_record_id = $item->get_meta('_studiou_fpp_file_record_id');
  338. // Build download URL that also marks file as downloaded
  339. $download_url = add_query_arg(array(
  340. 'studiou_fpp_download_file' => 1,
  341. 'file_record_id' => $file_record_id ?: 0,
  342. 'attachment_id' => $attachment_id,
  343. '_wpnonce' => wp_create_nonce('studiou-fpp-download-file'),
  344. ), admin_url('admin.php'));
  345. echo '<p class="studiou-fpp-order-download">';
  346. echo '<a href="' . esc_url($download_url) . '" class="button button-small">';
  347. echo '<span class="dashicons dashicons-download" style="line-height:1.4;vertical-align:middle;"></span> ';
  348. echo esc_html__('Download uploaded photo', 'studiou-wc-free-photo-product');
  349. echo '</a>';
  350. if ($file_name) {
  351. echo ' <small>(' . esc_html($file_name) . ')</small>';
  352. }
  353. echo '</p>';
  354. }
  355. }