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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // AJAX add to cart
  11. add_action('wp_ajax_studiou_wcfpp_add_to_cart', array($this, 'ajax_add_to_cart'));
  12. add_action('wp_ajax_nopriv_studiou_wcfpp_add_to_cart', array($this, 'ajax_add_to_cart'));
  13. // Display uploaded file info in cart
  14. add_filter('woocommerce_get_item_data', array($this, 'display_cart_item_data'), 10, 2);
  15. // Save file reference to order item
  16. add_action('woocommerce_checkout_create_order_line_item', array($this, 'save_order_item_meta'), 10, 4);
  17. // Link file records to order after checkout
  18. add_action('woocommerce_checkout_order_processed', array($this, 'link_files_to_order'), 10, 3);
  19. // Ensure cart item data persists
  20. add_filter('woocommerce_add_cart_item_data', array($this, 'preserve_cart_item_data'), 10, 3);
  21. }
  22. public function ajax_add_to_cart() {
  23. check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
  24. $product_id = isset($_POST['product_id']) ? absint($_POST['product_id']) : 0;
  25. $variation_id = isset($_POST['variation_id']) ? absint($_POST['variation_id']) : 0;
  26. $attachment_id = isset($_POST['attachment_id']) ? absint($_POST['attachment_id']) : 0;
  27. $file_record_id = isset($_POST['file_record_id']) ? absint($_POST['file_record_id']) : 0;
  28. if (!$product_id || !$variation_id || !$attachment_id || !$file_record_id) {
  29. wp_send_json_error(array('message' => __('Missing required data.', 'studiou-wc-free-photo-product')));
  30. return;
  31. }
  32. // Verify product is FPP enabled
  33. if (!Studiou_WC_FPP_Product::is_fpp_product($product_id)) {
  34. wp_send_json_error(array('message' => __('Invalid product.', 'studiou-wc-free-photo-product')));
  35. return;
  36. }
  37. // Verify the file record exists and belongs to this user
  38. $record = $this->db->get_file_record($file_record_id);
  39. if (!$record || (int) $record->attachment_id !== $attachment_id) {
  40. wp_send_json_error(array('message' => __('Invalid file.', 'studiou-wc-free-photo-product')));
  41. return;
  42. }
  43. // Verify variation belongs to product
  44. $variation = wc_get_product($variation_id);
  45. if (!$variation || $variation->get_parent_id() !== $product_id) {
  46. wp_send_json_error(array('message' => __('Invalid variant.', 'studiou-wc-free-photo-product')));
  47. return;
  48. }
  49. // Get variation attributes for cart
  50. $variation_data = array();
  51. $attributes = $variation->get_variation_attributes();
  52. foreach ($attributes as $key => $value) {
  53. $variation_data[$key] = $value;
  54. }
  55. // Custom cart item data
  56. $cart_item_data = array(
  57. 'studiou_fpp_attachment_id' => $attachment_id,
  58. 'studiou_fpp_file_record_id' => $file_record_id,
  59. 'studiou_fpp_file_name' => $record->file_name,
  60. );
  61. // Update file record with variation
  62. $this->db->update_file_record($file_record_id, array(
  63. 'variation_id' => $variation_id,
  64. ));
  65. // Add to WC cart
  66. $cart_item_key = WC()->cart->add_to_cart(
  67. $product_id,
  68. 1,
  69. $variation_id,
  70. $variation_data,
  71. $cart_item_data
  72. );
  73. if ($cart_item_key) {
  74. wp_send_json_success(array(
  75. 'message' => __('Product added to cart.', 'studiou-wc-free-photo-product'),
  76. 'cart_item_key' => $cart_item_key,
  77. ));
  78. } else {
  79. wp_send_json_error(array('message' => __('Could not add to cart.', 'studiou-wc-free-photo-product')));
  80. }
  81. }
  82. public function preserve_cart_item_data($cart_item_data, $product_id, $variation_id) {
  83. // Cart item data is already set by add_to_cart call above
  84. return $cart_item_data;
  85. }
  86. public function display_cart_item_data($item_data, $cart_item) {
  87. if (isset($cart_item['studiou_fpp_file_name'])) {
  88. $item_data[] = array(
  89. 'name' => __('Uploaded Photo', 'studiou-wc-free-photo-product'),
  90. 'value' => esc_html($cart_item['studiou_fpp_file_name']),
  91. 'display' => '',
  92. );
  93. }
  94. return $item_data;
  95. }
  96. public function save_order_item_meta($item, $cart_item_key, $values, $order) {
  97. if (isset($values['studiou_fpp_attachment_id'])) {
  98. $item->add_meta_data('_studiou_fpp_attachment_id', $values['studiou_fpp_attachment_id']);
  99. }
  100. if (isset($values['studiou_fpp_file_record_id'])) {
  101. $item->add_meta_data('_studiou_fpp_file_record_id', $values['studiou_fpp_file_record_id']);
  102. }
  103. if (isset($values['studiou_fpp_file_name'])) {
  104. $item->add_meta_data('_studiou_fpp_file_name', $values['studiou_fpp_file_name']);
  105. // Also add a visible meta for admin display
  106. $item->add_meta_data(__('Uploaded Photo', 'studiou-wc-free-photo-product'), $values['studiou_fpp_file_name']);
  107. }
  108. }
  109. public function link_files_to_order($order_id, $posted_data, $order) {
  110. foreach ($order->get_items() as $item_id => $item) {
  111. $file_record_id = $item->get_meta('_studiou_fpp_file_record_id');
  112. if ($file_record_id) {
  113. $this->db->link_file_to_order((int) $file_record_id, $order_id, $item_id);
  114. }
  115. }
  116. }
  117. }