get_items(); $customer_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); $customer_tel = $order->get_billing_phone(); $customer_email = $order->get_billing_email(); $customer_address = $order->get_formatted_billing_address(); ob_start(); require_once OB_CHILD_THEME_URI . '/inc/templates/return-request.php'; $output = ob_get_clean(); $json['type'] = 'success'; $json['message'] = 'Redirect to second step'; $json['output'] = $output; wp_send_json( $json ); } } } } } // First step exchange request public function softoba_first_exchange_request() { if ( isset( $_POST['form_data'] ) ) { parse_str( $_POST['form_data'], $form_data ); $email = isset( $form_data['exchange-order-email'] ) ? sanitize_text_field( $form_data['exchange-order-email'] ) : ''; $order_number = isset( $form_data['exchange-order-number'] ) ? sanitize_text_field( $form_data['exchange-order-number'] ) : ''; $json = array(); if( ! wp_verify_nonce( $form_data['exchange_nonce'], 'exchange_request_nonce' ) ) { $json['type'] = 'error'; $json['message'] = esc_html__('Security check failed.'); wp_send_json( $json ); } if( ! softoba_check_email_and_order_number($order_number, $email) ){ $json['type'] = 'error'; $json['message'] = 'Invalid Email or Order Number'; wp_send_json( $json ); } // if ( ! empty( softoba_check_existing_return_request($order_number, $email) ) ) { // $json['type'] = 'error'; // $json['message'] = 'You have already submitted a request for return'; // wp_send_json( $json ); // } if( ! softoba_has_variable_product_item( $order_number ) ) { $json['type'] = 'error'; $json['message'] = esc_html__( 'You can only exchange variant products', 'astra' ); wp_send_json( $json ); }elseif ( ! softoba_is_order_completed_or_paid( $order_number ) ) { $json['type'] = 'error'; $json['message'] = esc_html__( 'Your order status not paid or completed!', 'astra' ); wp_send_json( $json ); }elseif( softoba_check_order_exchangable($order_number) ) { $json['type'] = 'error'; $json['message'] = 'Your order\'s exchange period has expired!'; wp_send_json( $json ); } $exchange_fee_status = softoba_exchange_payment_status($order_number, $email); if ($exchange_fee_status['status'] === 'pending') { $json['type'] = 'error'; $json['message'] = 'Please pay the exchange shipping fee to proceed!'; $json['exchange_post_id'] = $exchange_fee_status['post_id']; wp_send_json( $json ); }elseif( $exchange_fee_status['status'] === 'on-hold' ){ $json['type'] = 'error'; $json['message'] = 'Your exchange fee status on hold!'; wp_send_json( $json ); } $existing_exchange_requests = softoba_check_existing_exchange_request($order_number, $email); if ( ! empty( $existing_exchange_requests ) ) { $json['type'] = 'error'; if( $existing_exchange_requests === 'softoba-new' ) { $json['message'] = 'You have already submitted a request!'; }elseif( $existing_exchange_requests === 'softoba-approved' ){ $json['message'] = 'Your request status is completed!'; }else{ $json['message'] = 'Your request status is '.softoba_get_request_status_by_key( $existing_exchange_requests ); } wp_send_json( $json ); }else{ $order = wc_get_order($order_number); $items = $order->get_items(); $customer_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); $customer_tel = $order->get_billing_phone(); $customer_email = $order->get_billing_email(); $customer_address = $order->get_formatted_billing_address(); ob_start(); require_once OB_CHILD_THEME_URI . '/inc/templates/exchange-request.php'; $output = ob_get_clean(); $json['type'] = 'success'; $json['message'] = 'Redirect to second step'; $json['output'] = $output; wp_send_json( $json ); } } } // Get Product card public function softoba_get_product_data() { $order_id = intval($_POST['order_id']); $product_id = intval($_POST['product_id']); $order = wc_get_order($order_id); if ($order) { foreach ($order->get_items() as $item) { if ($item->get_product_id() === $product_id) { // $product = wc_get_product($product_id); $variation_id = $item->get_variation_id(); $product = wc_get_product($variation_id ? $variation_id : $product_id); if ($product) { $product_name = $item->get_name(); $product_price = $item->get_total(); $product_image = $product->get_image(); $product_quantity = $item->get_quantity(); ob_start(); ?>
    • Quantity:
  • get_items() as $item) { if ($item->get_product_id() === $product_id) { $variation_id = $item->get_variation_id(); $product = wc_get_product($product_id); if ($product) { $product_name = $item->get_name(); $product_price = $item->get_total(); $product_image = $product->get_image(); $product_quantity = $item->get_quantity(); // $variation_attributes = $item->get_variation_attributes(); ob_start(); ?>
    • is_type('variable')) { $get_variation = new WC_Product_Variation($variation_id); $get_variation_attributes = $get_variation->get_variation_attributes(); echo '
    • ' . wc_get_formatted_variation( $get_variation_attributes, true ) . '
    • '; } ?>
    • Quantity:
    • is_type('variable')) { $available_variations = $product->get_available_variations(); echo ''; } ?>
  • get_discount_total(); $softoba_cpn_tax = $softoba_obj->get_discount_tax(); $softoba_dis_tot = $softoba_dis + $softoba_cpn_tax; $softoba_dis_tot = 0; $products = []; $products['order_id'] = $orderid; $products['status'] = 'pending'; $products['products'] = $_POST['products']; foreach ( $_POST['products'] as $key => $value ) { $product_id = isset($value['product_id']) ? intval($value['product_id']) : 0; $item_id = isset($value['item_id']) ? intval($value['item_id']) : 0; $products['pids'][] = $product_id; $products['itemids'][] = $item_id; } $request = new SOFTOBA_Return_Request(); $request->order_id = $orderid; $request->products_data = $products; $save_request = $request->save(); if( $save_request ) { update_post_meta( $orderid, 'sobaoba_return_products', $products ); ob_start(); require_once OB_CHILD_THEME_URI . '/inc/templates/return-thankyou.php'; $output = ob_get_clean(); $json['type'] = 'success'; $json['message'] = 'Request submitted successfully!'; $json['html'] = $output; // Email WC()->mailer(); do_action( 'softoba_return_new_request_user', $save_request ); do_action( 'softoba_return_new_request_admin', $save_request ); wp_send_json($json); }else{ $json['type'] = 'error'; $json['message'] = 'We\'re sorry, something went wrong.'; wp_send_json($json); } } wp_die(); } // Submit final return request public function softoba_final_return_request() { $json = array(); $order_id = ! empty( $_POST['order_id'] ) ? $_POST['order_id'] : false; $item_id = ! empty( $_POST['selected-item-id'] ) ? $_POST['selected-item-id'] : false; $product_id = ! empty( $_POST['oont-selected-product'] ) ? $_POST['oont-selected-product'] : false; $return_reason = ! empty( $_POST['return_reason'] ) ? $_POST['return_reason'] : false; $return_comment = ! empty( $_POST['return_comment'] ) ? $_POST['return_comment'] : false; $product_price = ! empty( $_POST['product_price'] ) ? $_POST['product_price'] : false; $return_qty = ! empty( $_POST['return_qty'] ) ? $_POST['return_qty'] : false; $total_qty = ! empty( $_POST['total_qty'] ) ? $_POST['total_qty'] : false; $return_file = ! empty( $_FILES['return_upload-file'] ) ? $_FILES['return_upload-file'] : false; if( ! wp_verify_nonce( $_POST['softoba_nonce'], 'return_request_nonce' ) ) { $json['type'] = 'error'; $json['message'] = esc_html__('Security check failed.'); wp_send_json( $json ); } if( $return_file ) { // Check if the directory exists, create if not if (!file_exists(SOFTOBA_UPLOADS_DIR)) { wp_mkdir_p(SOFTOBA_UPLOADS_DIR); } } if ( isset( $_FILES['return_upload-file'] ) && $_FILES['return_upload-file'] ) { $tmp_name = $_FILES['return_upload-file']['tmp_name']; $upload_file_name = basename( $_FILES['return_upload-file']['name'] ); $extension = pathinfo( $upload_file_name, PATHINFO_EXTENSION ); $id_name = uniqid( 'softoba_' ) . '.' . $extension; $uploaded_result = move_uploaded_file( $tmp_name, SOFTOBA_UPLOADS_DIR . $id_name ); } if( $order_id ) { $request = new SOFTOBA_Return_Request(); $request->order_id = $order_id; $request->product_id = $product_id; $request->item_id = $item_id; $request->item_total = $product_price; $request->qty = $return_qty; $request->qty_total = $total_qty; $request->item_value = $product_price; $request->reason = $return_reason; $request->return_comments = $return_comment; $line_tax = wc_get_order_item_meta($item_id, '_line_tax'); $request->tax_value = empty( $line_tax ) ? 0 : $line_tax / $request->qty_total; $request->tax_total = $request->tax_value * $request->qty_total; $request->tax_refund_total = $request->tax_value * $request->qty; $line_tax_data = maybe_unserialize( wc_get_order_item_meta( $item_id, '_line_tax_data' ) ); if ( $line_tax_data ) { foreach ( $line_tax_data['total'] as &$tax ) { $single_tax_value = $tax / $request->qty_total; $tax = $single_tax_value * $request->qty; } } $request->item_tax_data = $line_tax_data ? $line_tax_data['total'] : ''; // Save only 'total' array. $request->item_refund_total = $request->item_value * $request->qty; $request->refund_total = $request->item_refund_total + $request->tax_refund_total; $request->whole_order = 0; $request->refund_id = 0; $request->refunded_amount = 0; $request->coupon_id = 0; if ($uploaded_result) { $request->attachments = SOFTOBA_UPLOADS_URL . $id_name; } else { $request->attachments = null; } wc_update_order_item_meta( $request->item_id, '_softoba_item_requested', 'yes' ); wc_update_order_item_meta( $request->item_id, '_softoba_requested_qty', $request->qty ); $save_request = $request->save(); if( $save_request ) { ob_start(); require_once OB_CHILD_THEME_URI . '/inc/templates/return-thankyou.php'; $output = ob_get_clean(); $json['type'] = 'success'; $json['message'] = 'Request submitted successfully!'; $json['html'] = $output; wp_send_json($json); }else{ $json['type'] = 'error'; $json['message'] = 'We\'re sorry, something went wrong.'; wp_send_json($json); } } wp_die(); } // Return attachments uploads public function oont_return_attachments() { if ( isset( $_FILES['file'] ) && $_FILES['file'] ) { $json = array(); // Check if the directory exists, create if not if (!file_exists(SOFTOBA_UPLOADS_DIR)) { wp_mkdir_p(SOFTOBA_UPLOADS_DIR); } $tmp_name = $_FILES['file']['tmp_name']; $upload_file_name = basename( $_FILES['file']['name'] ); $extension = pathinfo( $upload_file_name, PATHINFO_EXTENSION ); $id_name = uniqid( 'softoba_' ) . '.' . $extension; $uploaded_result = move_uploaded_file( $tmp_name, SOFTOBA_UPLOADS_DIR . $id_name ); if( $uploaded_result ) { $json['type'] = 'success'; $json['message'] = 'Return attachment uploaded successfully'; $json['imgurl'] = SOFTOBA_UPLOADS_URL . $id_name; wp_send_json( $json ); }else{ $json['type'] = 'error'; $json['message'] = 'Something went wrong, upload again.'; wp_send_json( $json ); } } } public function oont_exchange_products() { $json = array(); if ( isset( $_POST['orderid'] ) ) { $orderid = ! empty( $_POST['orderid'] ) ? $_POST['orderid'] : false; $softoba_obj = wc_get_order( $orderid ); $softoba_dis = $softoba_obj->get_discount_total(); $softoba_cpn_tax = $softoba_obj->get_discount_tax(); $softoba_dis_tot = $softoba_dis + $softoba_cpn_tax; $softoba_dis_tot = 0; $products = []; $products['oder_id'] = $orderid; $products['status'] = 'pending'; $products['products'] = $_POST['products']; foreach ( $_POST['products'] as $key => $value ) { $product_id = isset($value['product_id']) ? intval($value['product_id']) : 0; $item_id = isset($value['item_id']) ? intval($value['item_id']) : 0; $products['pids'][] = $product_id; $products['itemids'][] = $item_id; } $request = new SOFTOBA_Exchange_Request(); $request->order_id = $orderid; $request->products_data = $products; $save_request = $request->save(); if( $save_request ) { update_post_meta( $orderid, 'sobaoba_exchange_products', $products ); ob_start(); $new_request_id = $save_request; require_once OB_CHILD_THEME_URI . '/inc/templates/exchange-thankyou.php'; $output = ob_get_clean(); $json['type'] = 'success'; $json['message'] = 'Exchange Request submitted successfully!'; $json['html'] = $output; // Email WC()->mailer(); do_action( 'softoba_return_new_exchange_user', $save_request ); do_action( 'softoba_return_new_exchange_admin', $save_request ); wp_send_json($json); }else{ $json['type'] = 'error'; $json['message'] = 'We\'re sorry, something went wrong.'; wp_send_json($json); } } wp_die(); } // Submit final exchange request public function softoba_final_exchange_request() { $json = array(); // Check nonce if( ! wp_verify_nonce( $_POST['exchange_nonce'], 'exchange_nonce' ) ) { $json['type'] = 'error'; $json['message'] = esc_html__('Security check failed.'); wp_send_json( $json ); } $order_id = ! empty( $_POST['order_id'] ) ? $_POST['order_id'] : false; $item_ids = ! empty( $_POST['selected-item-id'] ) ? $_POST['selected-item-id'] : false; $product_ids = ! empty( $_POST['oont-selected-product'] ) ? $_POST['oont-selected-product'] : false; $variation_ids = ! empty( $_POST['selected_variation_id'] ) ? $_POST['selected_variation_id'] : false; $order_variation_ids = ! empty( $_POST['order_variation_id'] ) ? $_POST['order_variation_id'] : false; $product_prices = ! empty( $_POST['product_price'] ) ? $_POST['product_price'] : false; $return_qtys = ! empty( $_POST['return_qty'] ) ? $_POST['return_qty'] : false; $total_qtys = ! empty( $_POST['total_qty'] ) ? $_POST['total_qty'] : false; // if( ! softoba_has_variable_product_item( $order_id ) ) { // $json['type'] = 'error'; // $json['message'] = esc_html__( 'You can only exchange variant products', 'astra' ); // wp_send_json( $json ); // } // if ( ! softoba_is_order_completed_or_paid( $order_id ) ) { // $json['type'] = 'error'; // $json['message'] = esc_html__( 'Your order status not paid or completed!', 'astra' ); // wp_send_json( $json ); // } if (isset($_POST['exchange_checkbox']) && is_array($_POST['exchange_checkbox']) && !empty($_POST['exchange_checkbox'])) { if( $order_id ) { $request_items_data = array(); // Loop through the items foreach ($item_ids as $key => $item_id) { // Collect data for each item $request_item_data = array( 'item_id' => $item_id, 'product_id' => $product_ids[$key], 'variation_id' => $variation_ids[$key], 'order_variation_id' => $order_variation_ids[$key], 'product_price' => $product_prices[$key], 'return_qty' => $return_qtys[$key], 'total_qty' => $total_qtys[$key] ); // Add to the items data array $request_items_data[] = $request_item_data; } $request = new SOFTOBA_Exchange_Request(); $request->order_id = $order_id; $request->product_id = $product_id; $request->item_id = $item_id; $request->variation_id = $variation_id; $request->order_variation_id = $order_variation_id; $request->item_total = $product_price; $request->qty = $return_qty; $request->qty_total = $total_qty; $request->item_value = $product_price; $line_tax = wc_get_order_item_meta($item_id, '_line_tax'); $request->tax_value = empty( $line_tax ) ? 0 : $line_tax / $request->qty_total; $request->tax_total = $request->tax_value * $request->qty_total; $request->tax_refund_total = $request->tax_value * $request->qty; $line_tax_data = maybe_unserialize( wc_get_order_item_meta( $item_id, '_line_tax_data' ) ); if ( $line_tax_data ) { foreach ( $line_tax_data['total'] as &$tax ) { $single_tax_value = $tax / $request->qty_total; $tax = $single_tax_value * $request->qty; } } $request->item_tax_data = $line_tax_data ? $line_tax_data['total'] : ''; // Save only 'total' array. $request->item_refund_total = $request->item_value * $request->qty; $request->refund_total = $request->item_refund_total + $request->tax_refund_total; wc_update_order_item_meta( $request->item_id, '_softoba_item_exchange', 'yes' ); wc_update_order_item_meta( $request->item_id, '_softoba_exchange_qty', $request->qty ); $save_request = $request->save(); if( $save_request ) { ob_start(); $new_request_id = $save_request; require_once OB_CHILD_THEME_URI . '/inc/templates/exchange-thankyou.php'; $output = ob_get_clean(); $json['type'] = 'success'; $json['message'] = 'Exchange Request submitted successfully!'; $json['html'] = $output; wp_send_json($json); }else{ $json['type'] = 'error'; $json['message'] = 'We\'re sorry, something went wrong.'; wp_send_json($json); } } }else{ $json['type'] = 'error'; $json['message'] = 'Choose any product to exchange!'; wp_send_json( $json ); } wp_die(); } /** * Change Request status * * @return void */ public function change_status() { check_ajax_referer( 'change-status', 'security' ); $request_id = ! empty( $_POST['softoba_request_id'] ) ? $_POST['softoba_request_id'] : false; $status = ! empty( $_POST['softoba_status'] ) ? $_POST['softoba_status'] : false; try { if ( ! $request_id || ! $status ) { throw new Exception( esc_html__( 'Empty data.', 'astra' ) ); } if ( ! current_user_can( apply_filters( 'softoba_can_manage_requests', 'manage_woocommerce' ) ) ) { throw new Exception( esc_html__( 'You do not have permission to change any request status.', 'astra' ) ); } $request = new SOFTOBA_Return_Request( $request_id ); if ( ! $request->exists() ) { throw new Exception( esc_html__( 'Refund request does not exist.', 'astra' ) ); } switch ( $status ) { case 'softoba-rejected' : if ( 'softoba-rejected' != $request->status ) $request->set_rejected(); break; case 'softoba-processing' : if ( 'softoba-processing' != $request->status ) $request->set_processing(); break; case 'softoba-on-hold' : if ( 'softoba-on-hold' != $request->status ) $request->set_on_hold(); break; case 'softoba-close-request' : $request->close_request(); break; default : throw new Exception( esc_html__( 'Wrong status code.', 'astra' ) ); break; } wp_send_json_success(); } catch ( Exception $e ) { wp_send_json_error( array( 'error' => $e->getMessage() ) ); } } /** * Change Exchange Request status * * @return void */ public function ex_change_status() { check_ajax_referer( 'change-status', 'security' ); $request_id = ! empty( $_POST['softoba_request_id'] ) ? $_POST['softoba_request_id'] : false; $status = ! empty( $_POST['softoba_status'] ) ? $_POST['softoba_status'] : false; try { if ( ! $request_id || ! $status ) { throw new Exception( esc_html__( 'Empty data.', 'astra' ) ); } if ( ! current_user_can( apply_filters( 'softoba_can_manage_requests', 'manage_woocommerce' ) ) ) { throw new Exception( esc_html__( 'You do not have permission to change any request status.', 'astra' ) ); } $request = new SOFTOBA_Exchange_Request( $request_id ); if ( ! $request->exists() ) { throw new Exception( esc_html__( 'Refund request does not exist.', 'astra' ) ); } switch ( $status ) { case 'softoba-rejected' : if ( 'softoba-rejected' != $request->status ) $request->set_rejected(); break; case 'softoba-approved' : if ( 'softoba-approved' != $request->status ) $request->set_approved(); break; case 'softoba-processing' : if ( 'softoba-processing' != $request->status ) $request->set_processing(); break; case 'softoba-on-hold' : if ( 'softoba-on-hold' != $request->status ) $request->set_on_hold(); break; case 'softoba-close-request' : $request->close_request(); break; default : throw new Exception( esc_html__( 'Wrong status code.', 'astra' ) ); break; } wp_send_json_success(); } catch ( Exception $e ) { wp_send_json_error( array( 'error' => $e->getMessage() ) ); } } /** * Process Exchange fee * * @return void */ public function softoba_pay_exchange_fee() { $json = array(); $post_id = isset( $_POST['exchangeId'] ) ? intval( sanitize_text_field( $_POST['exchangeId'] ) ) : null; $request = new SOFTOBA_Exchange_Request( $post_id ); if( $request ) { $customer_id = $request->customer_id; $unique_id = $request->unique_id; $price = $request->price; $custom_price = 3; $cart_item_data = []; // Add the form data as cart item attributes $cart_item_data['softoba_cart_data']['order_type'] = 'exchange_request'; $cart_item_data['softoba_cart_data']['post_id'] = $post_id; $cart_item_data['softoba_cart_data']['unique_id'] = $unique_id; $cart_item_data['softoba_cart_data']['post_permalink'] = get_permalink( $post_id ); $cart_item_data['softoba_cart_data']['post_author'] = $customer_id; $cart_item_data['softoba_cart_data']['price_total'] = $price; // Empty cart first WC()->cart->empty_cart(); // Add to cart $cart_item = WC()->cart->add_to_cart( $post_id, 1, '0', array(), $cart_item_data ); if( $cart_item ) { $json['type'] = 'success'; $json['message'] = esc_html__( 'Added to cart and send checkout page', 'astra' ); $json['redirect_to'] = wc_get_checkout_url(); wp_send_json( $json ); } // echo $price; // $error_messages = wc_get_notices('error'); // print_r( $error_messages ); // print_r( $cart_item ); }else{ $json['type'] = 'error'; $json['message'] = esc_html__( 'Something went wrong, pleae try again.', 'astra' ); wp_send_json( $json ); } wp_die(); } /** * Update quantity mini cart */ public static function softoba_quantity_mini_cart() { if (!isset($_REQUEST['hash']) || !isset($_REQUEST['quantity'])) { wp_die(); } // Set item key as the hash found in input.qty's name $cart_item_key = $_REQUEST['hash']; // Get the array of values owned by the product we're updating $product_values = WC()->cart->get_cart_item($cart_item_key); // Get the quantity of the item in the cart $product_quantity = apply_filters('woocommerce_stock_amount_cart_item', apply_filters('woocommerce_stock_amount', preg_replace("/[^0-9\.]/", '', filter_var($_REQUEST['quantity'], FILTER_SANITIZE_NUMBER_INT))), $cart_item_key); // Update cart validation $passed_validation = apply_filters('woocommerce_update_cart_validation', true, $cart_item_key, $product_values, $product_quantity); // Update the quantity of the item in the cart if ($passed_validation) { WC()->cart->set_quantity($cart_item_key, $product_quantity, true); } $woo_mess = wc_print_notices(true); $woo_mess = empty($woo_mess) ? '' : $woo_mess; ob_start(); woocommerce_mini_cart(); $mini_cart = ob_get_clean(); // Fragments and mini cart are returned $data = array( 'fragments' => apply_filters( 'woocommerce_add_to_cart_fragments', array( 'div.widget_shopping_cart_content' => '
    ' . $mini_cart . '
    ', '.woocommerce-message' => $woo_mess ) ), 'cart_hash' => WC()->cart->get_cart_hash() ); wp_send_json($data); // wp_send_json_success( // array( // 'fragments' => apply_filters( // 'woocommerce_add_to_cart_fragments', // array( // 'div.widget_shopping_cart_content' => '
    ' . $mini_cart . '
    ', // '.woocommerce-message' => $woo_mess // ) // ), // 'cart_hash' => WC()->cart->get_cart_hash() // ) // ); } } }