__( 'Not allowed.', 'koalab-quote' ) ), 403 ); } if ( ! WC()->cart ) { wp_send_json_success( array( 'items' => array() ) ); } WC()->cart->calculate_totals(); $excl = 'excl' === klbq_tax_mode(); $items = array(); foreach ( WC()->cart->get_cart() as $cart_item ) { if ( empty( $cart_item['data'] ) || ! $cart_item['data'] instanceof WC_Product ) { continue; } $qty = (float) $cart_item['quantity']; $line = (float) $cart_item['line_subtotal'] + ( $excl ? 0 : (float) $cart_item['line_subtotal_tax'] ); $unit = $qty > 0 ? $line / $qty : 0; $items[] = klbq_item_from_product( $cart_item['data'], $qty, round( $unit, wc_get_price_decimals() ) ); } wp_send_json_success( array( 'items' => $items ) ); } ); add_action( 'rest_api_init', function () { register_rest_route( KLBQ_REST_NS, '/products', array( 'methods' => 'GET', 'permission_callback' => 'klbq_panel_can', 'callback' => 'klbq_rest_products', 'args' => array( 'q' => array( 'type' => 'string', 'required' => true, ), ), ) ); register_rest_route( KLBQ_REST_NS, '/quotes', array( array( 'methods' => 'GET', 'permission_callback' => 'klbq_user_can', 'callback' => 'klbq_rest_list', ), array( 'methods' => 'POST', 'permission_callback' => 'klbq_user_can', 'callback' => 'klbq_rest_save', ), ) ); register_rest_route( KLBQ_REST_NS, '/quotes/(?P\d+)', array( array( 'methods' => 'GET', 'permission_callback' => 'klbq_user_can', 'callback' => 'klbq_rest_get', ), array( 'methods' => 'POST', 'permission_callback' => 'klbq_user_can', 'callback' => 'klbq_rest_save', ), ) ); } ); /** * GET /products?q= — products and variations to add to a quote. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ function klbq_rest_products( $request ) { $term = trim( (string) $request['q'] ); if ( strlen( $term ) < 2 ) { return rest_ensure_response( array() ); } $store = WC_Data_Store::load( 'product' ); $ids = $store->search_products( wc_clean( $term ), '', true, false, 20 ); $out = array(); foreach ( $ids as $id ) { $product = $id ? wc_get_product( $id ) : null; if ( ! $product || $product->is_type( 'variable' ) || $product->is_type( 'grouped' ) ) { continue; } $parent = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(); if ( 'publish' !== get_post_status( $parent ) ) { continue; } $out[] = klbq_item_from_product( $product ); if ( count( $out ) >= 12 ) { break; } } return rest_ensure_response( $out ); } /** * GET /quotes?q= — latest quotes for the history tab. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ function klbq_rest_list( $request ) { $args = array( 'post_type' => KLBQ_CPT, 'post_status' => 'publish', 'posts_per_page' => 30, 'orderby' => 'date', 'order' => 'DESC', 'no_found_rows' => true, ); $q = trim( (string) $request->get_param( 'q' ) ); if ( '' !== $q ) { // Number in the title; customer name, company, tax ID, email and phone in the excerpt. $args['s'] = $q; } $args = (array) apply_filters( 'klbq_history_query', $args, $request ); $out = array(); foreach ( get_posts( $args ) as $post ) { $quote = klbq_quote_response( $post->ID ); if ( ! $quote ) { continue; } $row = array( 'id' => $post->ID, 'number' => $quote['number'], 'customer' => (string) get_post_meta( $post->ID, '_klbq_customer', true ), 'total' => $quote['totals']['total'], 'date' => $quote['date'], 'advisor' => $quote['advisor']['name'], 'brand' => $quote['data']['brand'], ); $out[] = (array) apply_filters( 'klbq_history_row', $row, $quote ); } return rest_ensure_response( $out ); } /** * GET /quotes/{id}. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ function klbq_rest_get( $request ) { $quote = klbq_quote_response( (int) $request['id'] ); if ( ! $quote ) { return new WP_Error( 'klbq_not_found', __( 'The quote does not exist.', 'koalab-quote' ), array( 'status' => 404 ) ); } return rest_ensure_response( $quote ); } /** * POST /quotes and /quotes/{id} — create or update. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ function klbq_rest_save( $request ) { $raw = $request->get_param( 'data' ); $data = klbq_sanitize_quote( $raw ); if ( empty( $data['items'] ) ) { return new WP_Error( 'klbq_empty', __( 'Add at least one product.', 'koalab-quote' ), array( 'status' => 400 ) ); } $id = isset( $request['id'] ) ? (int) $request['id'] : 0; /** * Filters a quote about to be saved from the panel. * * @param array $data Sanitized data. * @param mixed $raw Data as posted. * @param int $id Quote ID (0 for a new quote). */ $data = (array) apply_filters( 'klbq_rest_quote_data', $data, $raw, $id ); $id = klbq_save_quote( $data, $id ); if ( is_wp_error( $id ) ) { return $id; } return rest_ensure_response( klbq_quote_response( $id ) ); }