'GET', 'callback' => array(__CLASS__, 'get_cart_count'), 'permission_callback' => array( __CLASS__, 'permission_check' ), ) ); register_rest_route( self::$namespace, 'items', array( 'methods' => 'GET', 'callback' => array(__CLASS__, 'get_cart_items'), 'permission_callback' => array( __CLASS__, 'permission_check' ), ) ); register_rest_route( self::$namespace, 'documents', array( 'methods' => 'GET', 'callback' => array(__CLASS__, 'get_cart_documents'), 'permission_callback' => array( __CLASS__, 'permission_check' ), ) ); register_rest_route( self::$namespace, 'add', array( 'methods' => 'post', 'callback' => array(__CLASS__, 'add_package_to_cart'), 'permission_callback' => array( __CLASS__, 'permission_check' ), ) ); register_rest_route( self::$namespace, 'remove', array( 'methods' => 'post', 'callback' => array(__CLASS__, 'remove_package_from_cart'), 'permission_callback' => array( __CLASS__, 'permission_check' ), ) ); register_rest_route( self::$namespace, 'update-quantity', array( 'methods' => 'post', 'callback' => array(__CLASS__, 'update_package_quantity'), 'permission_callback' => array( __CLASS__, 'permission_check' ), ) ); register_rest_route( self::$namespace, 'place-order', array( 'methods' => 'post', 'callback' => array(__CLASS__, 'place_order'), 'permission_callback' => array( __CLASS__, 'permission_check' ), ) ); } public static function permission_check() { if (!is_user_logged_in()) { return new WP_Error( 'wiaas_rest_authorization_required', __( 'Sorry, only authorized users can access!', 'wiaas' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Get cart count * TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed * @return WP_REST_Response */ public static function get_cart_count() { $items = WC()->cart->get_cart_contents(); $count = 0; foreach ($items as $key => $item) { if (isset($item['bundled_by'])) { continue; } $count++; } return new WP_REST_Response(array( 'count' => $count, )); } /** * Get cart items * TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed * @return WP_REST_Response */ public static function get_cart_items() { $items = WC()->cart->get_cart_contents(); $result = array(); foreach ($items as $key => $item) { if (isset($item['bundled_by'])) { continue; } $package = wc_get_product($item['product_id']); $result[] = array( 'idPackage' => $item['product_id'], 'key' => $item['key'], 'packageName' => $package->get_title(), 'additionalPackages' => array(), 'areAdditionalAvailable' => true, 'areOptionsAvailable' => true, 'bids' => array(), 'commercialLead' => 'Coor Service Management', 'country' => array( 'currency' => 'SEK' ), 'options' => array(), 'quantity' => $item['quantity'], 'idPayType' => $item['_wiaas_price']['id'], 'payType' => $item['_wiaas_price']['payment_type'], 'periodUnit' => $item['_wiaas_price']['period_unit'], 'idPrice' => $item['_wiaas_price']['id'], 'fixedPrice' => $item['_wiaas_price']['minimal_fixed_price'], 'recurentPrice' => $item['_wiaas_price']['recurrent_price'], 'servicesPrice' => $item['_wiaas_price']['minimal_services_price'], 'totalPrices' => array( 'fixedPrice' => $item['_wiaas_price']['minimal_fixed_price'], 'recurentPrice' => $item['_wiaas_price']['recurrent_price'], 'servicesPrice' => $item['_wiaas_price']['minimal_services_price'], ), 'status' => 'available', 'idCommercialLead' => 14, ); } return new WP_REST_Response( array( 'cartItems' => $result, 'totalPrice' => '' ) ); } /** * Get cart documents * TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed * @return WP_REST_Response */ public static function get_cart_documents() { return new WP_REST_Response(array( 'areFilesUploaded' => true, 'templates' => array(), 'uploaded' => array() )); } /** * Add package to cart * TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed * @return WP_REST_Response * @throws Exception */ public static function add_package_to_cart() { $package_id = $_POST['package_id']; $price_id = $_POST['price_id']; $package = wc_get_product($package_id); $prices = Wiaas_Package_Pricing::get_package_prices($package); $selected_price = $prices[$price_id]; WC()->session->set('wiaas_price_' . $package_id, $price_id); $success = WC()->cart->add_to_cart( $package_id, 1, 0, array(), array( '_wiaas_price' => $selected_price, )); if ($success) { return new WP_REST_Response(array( 'messages' => array( array( 'code' => 'success', 'message' => 'PACKAGE_ADDED' ) ) )); } return new WP_REST_Response(array( 'messages' => array( array( 'code' => 'error', 'message' => 'PACKAGE_ALREADY_IN_CART' ) ) )); } /* * Remove package from cart * TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed */ public static function remove_package_from_cart() { $package_cart_key = $_POST['package_item_key']; $success = WC()->cart->remove_cart_item($package_cart_key); if ($success) { return new WP_REST_Response(array( 'messages' => array( array( 'code' => 'success', 'message' => 'PACKAGE_REMOVED_FROM_CART' ) ) )); } return new WP_REST_Response(array( 'messages' => array( array( 'code' => 'error', 'message' => 'INVALID_PACKAGE_FOR_REMOVE' ) ) )); } /** * Update package quantity in cart * TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed * @return WP_REST_Response */ public static function update_package_quantity() { $package_item_key = $_POST['package_item_key']; $new_quantity = $_POST['quantity']; $success = WC()->cart->set_quantity($package_item_key, $new_quantity, true); if ($success) { return new WP_REST_Response(array( 'messages' => array( array( 'code' => 'success', 'message' => 'QUANTITY_UPDATED' ) ) )); } return new WP_REST_Response(array( 'messages' => array( array( 'code' => 'error', 'message' => 'QUANTITY_NOT_UPDATED' ) ) )); } /** * Placing order as part of checkout process * TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed * @return WP_REST_Response * @throws Exception */ public static function place_order() { $details = $_POST['details']; $vat_code = $_POST['vat']; $company_name = $_POST['companyName']; $delivery_address = $_POST['delivery']; $billing_address = $_POST['billing']; $order_id = WC()->checkout()->create_order(array()); $order = wc_get_order( $order_id ); $order->set_shipping_city($delivery_address['city']); $order->set_shipping_country($delivery_address['countryName']); $order->set_shipping_address_1($delivery_address['detailedAddress']); $order->set_shipping_postcode($delivery_address['zipCode']); $order->set_billing_city($billing_address['city']); $order->set_billing_country($billing_address['countryName']); $order->set_billing_address_1($billing_address['detailedAddress']); $order->set_billing_postcode($billing_address['zipCode']); $order->set_billing_first_name($billing_address['firstName']); $order->set_billing_last_name($billing_address['lastName']); $order->payment_complete(); add_post_meta($order_id, '_wiaas_vat_code', $vat_code); add_post_meta($order_id, '_wiaas_company_name', $company_name); add_post_meta($order_id, '_wiaas_project_id', $details['idProject']); add_post_meta($order_id, '_wiaas_reference', $details['reference']); add_post_meta($order_id, '_wiaas_tender', $details['tender']); // $order->get_li WC()->cart->empty_cart( true ); return new WP_REST_Response(array( 'messages' => array( array( 'code' => 'success', 'message' => 'ORDER_PLACED' ) ), 'details' => $details )); } }