| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- if (!defined('WPINC')) {
- die;
- }
- class Studiou_WC_FPP_Cart {
- /** @var Studiou_WC_FPP_DB */
- private $db;
- public function __construct($db) {
- $this->db = $db;
- // AJAX add to cart
- add_action('wp_ajax_studiou_wcfpp_add_to_cart', array($this, 'ajax_add_to_cart'));
- add_action('wp_ajax_nopriv_studiou_wcfpp_add_to_cart', array($this, 'ajax_add_to_cart'));
- // Display uploaded file info in cart
- add_filter('woocommerce_get_item_data', array($this, 'display_cart_item_data'), 10, 2);
- // Save file reference to order item
- add_action('woocommerce_checkout_create_order_line_item', array($this, 'save_order_item_meta'), 10, 4);
- // Link file records to order after checkout
- add_action('woocommerce_checkout_order_processed', array($this, 'link_files_to_order'), 10, 3);
- // Ensure cart item data persists
- add_filter('woocommerce_add_cart_item_data', array($this, 'preserve_cart_item_data'), 10, 3);
- }
- public function ajax_add_to_cart() {
- check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
- $product_id = isset($_POST['product_id']) ? absint($_POST['product_id']) : 0;
- $variation_id = isset($_POST['variation_id']) ? absint($_POST['variation_id']) : 0;
- $attachment_id = isset($_POST['attachment_id']) ? absint($_POST['attachment_id']) : 0;
- $file_record_id = isset($_POST['file_record_id']) ? absint($_POST['file_record_id']) : 0;
- if (!$product_id || !$variation_id || !$attachment_id || !$file_record_id) {
- wp_send_json_error(array('message' => __('Missing required data.', 'studiou-wc-free-photo-product')));
- return;
- }
- // Verify product is FPP enabled
- if (!Studiou_WC_FPP_Product::is_fpp_product($product_id)) {
- wp_send_json_error(array('message' => __('Invalid product.', 'studiou-wc-free-photo-product')));
- return;
- }
- // Verify the file record exists and belongs to this user
- $record = $this->db->get_file_record($file_record_id);
- if (!$record || (int) $record->attachment_id !== $attachment_id) {
- wp_send_json_error(array('message' => __('Invalid file.', 'studiou-wc-free-photo-product')));
- return;
- }
- // Verify variation belongs to product
- $variation = wc_get_product($variation_id);
- if (!$variation || $variation->get_parent_id() !== $product_id) {
- wp_send_json_error(array('message' => __('Invalid variant.', 'studiou-wc-free-photo-product')));
- return;
- }
- // Get variation attributes for cart
- $variation_data = array();
- $attributes = $variation->get_variation_attributes();
- foreach ($attributes as $key => $value) {
- $variation_data[$key] = $value;
- }
- // Custom cart item data
- $cart_item_data = array(
- 'studiou_fpp_attachment_id' => $attachment_id,
- 'studiou_fpp_file_record_id' => $file_record_id,
- 'studiou_fpp_file_name' => $record->file_name,
- );
- // Update file record with variation
- $this->db->update_file_record($file_record_id, array(
- 'variation_id' => $variation_id,
- ));
- // Add to WC cart
- $cart_item_key = WC()->cart->add_to_cart(
- $product_id,
- 1,
- $variation_id,
- $variation_data,
- $cart_item_data
- );
- if ($cart_item_key) {
- wp_send_json_success(array(
- 'message' => __('Product added to cart.', 'studiou-wc-free-photo-product'),
- 'cart_item_key' => $cart_item_key,
- ));
- } else {
- wp_send_json_error(array('message' => __('Could not add to cart.', 'studiou-wc-free-photo-product')));
- }
- }
- public function preserve_cart_item_data($cart_item_data, $product_id, $variation_id) {
- // Cart item data is already set by add_to_cart call above
- return $cart_item_data;
- }
- public function display_cart_item_data($item_data, $cart_item) {
- if (isset($cart_item['studiou_fpp_file_name'])) {
- $item_data[] = array(
- 'name' => __('Uploaded Photo', 'studiou-wc-free-photo-product'),
- 'value' => esc_html($cart_item['studiou_fpp_file_name']),
- 'display' => '',
- );
- }
- return $item_data;
- }
- public function save_order_item_meta($item, $cart_item_key, $values, $order) {
- if (isset($values['studiou_fpp_attachment_id'])) {
- $item->add_meta_data('_studiou_fpp_attachment_id', $values['studiou_fpp_attachment_id']);
- }
- if (isset($values['studiou_fpp_file_record_id'])) {
- $item->add_meta_data('_studiou_fpp_file_record_id', $values['studiou_fpp_file_record_id']);
- }
- if (isset($values['studiou_fpp_file_name'])) {
- $item->add_meta_data('_studiou_fpp_file_name', $values['studiou_fpp_file_name']);
- // Also add a visible meta for admin display
- $item->add_meta_data(__('Uploaded Photo', 'studiou-wc-free-photo-product'), $values['studiou_fpp_file_name']);
- }
- }
- public function link_files_to_order($order_id, $posted_data, $order) {
- foreach ($order->get_items() as $item_id => $item) {
- $file_record_id = $item->get_meta('_studiou_fpp_file_record_id');
- if ($file_record_id) {
- $this->db->link_file_to_order((int) $file_record_id, $order_id, $item_id);
- }
- }
- }
- }
|