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

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