| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- if (!defined('WPINC')) {
- die;
- }
- class Studiou_WC_FPP_Single_Product {
- public function __construct() {
- // Render upload area below the variation table on single product page
- add_action('woocommerce_single_product_summary', array($this, 'render_upload_area'), 35);
- // Validation before add to cart
- add_filter('woocommerce_add_to_cart_validation', array($this, 'validate_add_to_cart'), 10, 3);
- // AJAX: store uploaded file reference in WC session
- add_action('wp_ajax_studiou_wcfpp_set_upload_session', array($this, 'ajax_set_upload_session'));
- add_action('wp_ajax_nopriv_studiou_wcfpp_set_upload_session', array($this, 'ajax_set_upload_session'));
- add_action('wp_ajax_studiou_wcfpp_clear_upload_session', array($this, 'ajax_clear_upload_session'));
- add_action('wp_ajax_nopriv_studiou_wcfpp_clear_upload_session', array($this, 'ajax_clear_upload_session'));
- }
- public function render_upload_area() {
- global $product;
- if (!$product || !Studiou_WC_FPP_Product::is_fpp_product($product->get_id())) {
- return;
- }
- wp_enqueue_style('studiou-wcfpp-frontend');
- wp_enqueue_script('studiou-wcfpp-frontend');
- $product_id = $product->get_id();
- $max_file_size = Studiou_WC_FPP_Product::get_product_max_file_size($product_id);
- // Pass existing session state to JS for restore on page load
- $session_data = array();
- if (WC()->session) {
- $att_id = WC()->session->get('studiou_fpp_attachment_id');
- if ($att_id) {
- $thumb = wp_get_attachment_image_src($att_id, 'thumbnail');
- $preview = wp_get_attachment_image_src($att_id, 'woocommerce_single');
- $session_data = array(
- 'attachment_id' => $att_id,
- 'file_record_id' => WC()->session->get('studiou_fpp_file_record_id'),
- 'file_name' => WC()->session->get('studiou_fpp_file_name'),
- 'thumbnail_url' => $thumb ? $thumb[0] : '',
- 'preview_url' => $preview ? $preview[0] : ($thumb ? $thumb[0] : ''),
- );
- }
- }
- wp_localize_script('studiou-wcfpp-frontend', 'studiouWcfppSession', $session_data);
- include STUDIOU_WCFPP_PLUGIN_DIR . 'views/single-product-upload.php';
- }
- public function validate_add_to_cart($passed, $product_id, $quantity) {
- if (!Studiou_WC_FPP_Product::is_fpp_product($product_id)) {
- // Also check parent product for variations
- $product = wc_get_product($product_id);
- if ($product && $product->get_parent_id()) {
- $parent_id = $product->get_parent_id();
- if (!Studiou_WC_FPP_Product::is_fpp_product($parent_id)) {
- return $passed;
- }
- } else {
- return $passed;
- }
- }
- // Check WC session for uploaded file data
- if (WC()->session) {
- $attachment_id = WC()->session->get('studiou_fpp_attachment_id');
- if ($attachment_id) {
- return $passed;
- }
- }
- wc_add_notice(__('Please upload a photo before adding to cart.', 'studiou-wc-free-photo-product'), 'error');
- return false;
- }
- public function ajax_set_upload_session() {
- check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
- $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;
- $file_name = isset($_POST['file_name']) ? sanitize_text_field($_POST['file_name']) : '';
- if (!$attachment_id || !$file_record_id) {
- wp_send_json_error(array('message' => __('Invalid file data.', 'studiou-wc-free-photo-product')));
- return;
- }
- if (WC()->session) {
- WC()->session->set('studiou_fpp_attachment_id', $attachment_id);
- WC()->session->set('studiou_fpp_file_record_id', $file_record_id);
- WC()->session->set('studiou_fpp_file_name', $file_name);
- }
- wp_send_json_success();
- }
- public function ajax_clear_upload_session() {
- check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
- if (WC()->session) {
- WC()->session->set('studiou_fpp_attachment_id', null);
- WC()->session->set('studiou_fpp_file_record_id', null);
- WC()->session->set('studiou_fpp_file_name', null);
- }
- wp_send_json_success();
- }
- }
|