Handle order project and refactor api
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Admin_Order_Projects {
|
||||
|
||||
public static function init() {
|
||||
// Add admin page and subpage since woocommerce orders have custom menu page
|
||||
// so this will not be automatic
|
||||
add_filter('admin_menu', array(__CLASS__, 'add_admin_page'));
|
||||
add_action( 'parent_file', array(__CLASS__, 'highlight_order_projects_parent_menu') );
|
||||
|
||||
// Add is available fields to create and edit forms
|
||||
add_action( 'shop_order_project_add_form_fields', array( __CLASS__, 'add_is_available_field' ) );
|
||||
add_action( 'shop_order_project_edit_form_fields', array( __CLASS__, 'edit_is_available_field' ) );
|
||||
|
||||
// Add is available column
|
||||
add_filter( 'manage_edit-shop_order_project_columns', array( __CLASS__, 'add_is_available_column' ) );
|
||||
add_filter( 'manage_shop_order_project_custom_column', array( __CLASS__, 'add_is_available_column_content' ), 10, 3 );
|
||||
|
||||
// Save is available field when creating and editing
|
||||
add_action( 'created_term', array( __CLASS__, 'save_is_available_field' ), 10, 3 );
|
||||
add_action( 'edit_term', array( __CLASS__, 'save_is_available_field' ), 10, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add admin submenu page for order projects
|
||||
*/
|
||||
public static function add_admin_page() {
|
||||
add_submenu_page( 'woocommerce',
|
||||
esc_html__( 'Order projects', 'wiaas' ),
|
||||
esc_html__( 'Order projects', 'wiaas' ),
|
||||
'manage_woocommerce',
|
||||
'edit-tags.php?taxonomy=shop_order_project'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Correctly highlight parent menu page when order projects submenu is selected
|
||||
* @param $parent_file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function highlight_order_projects_parent_menu($parent_file) {
|
||||
if ( get_current_screen()->taxonomy == 'shop_order_project' ) {
|
||||
$parent_file = 'woocommerce';
|
||||
}
|
||||
return $parent_file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add is available field to order project creation form
|
||||
*/
|
||||
public static function add_is_available_field() {
|
||||
?>
|
||||
<div class="form-field form-required">
|
||||
<input
|
||||
type="checkbox"
|
||||
class="postform"
|
||||
style="float:left; margin-top: 4px; margin-right: 10px;"
|
||||
id="is_available"
|
||||
name="is_available"
|
||||
/>
|
||||
<label for="is_available"><?php _e( 'Is Available?', 'wiaas' ); ?></label>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Add is available field to order project edit form
|
||||
* @param $order_project
|
||||
*/
|
||||
public static function edit_is_available_field($order_project) {
|
||||
?>
|
||||
<tr class="form-field">
|
||||
<th scope="row" valign="top"><label><?php _e( 'Is Available?', 'wiaas' ); ?></label></th>
|
||||
<td>
|
||||
<input
|
||||
type="checkbox"
|
||||
class="checkbox"
|
||||
id="is_available"
|
||||
name="is_available"
|
||||
<?php
|
||||
checked( true, Wiaas_Order_Project::is_order_project_available($order_project), true )
|
||||
?>
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Add is_available column header to available order projects table headers
|
||||
* @param array $columns Available order project table headers
|
||||
*
|
||||
* @return array Edited order project table headers
|
||||
*/
|
||||
public static function add_is_available_column($columns) {
|
||||
$columns['is_available'] = __( 'Is Available?', 'wiaas' );
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render content for order project is available table column
|
||||
* @param string $columns Row content for order projects table
|
||||
* @param string $column Current column header
|
||||
* @param int $id Order project id
|
||||
*
|
||||
* @return string Edited row content with appended is available info
|
||||
*/
|
||||
public static function add_is_available_column_content($columns, $column, $id) {
|
||||
if ($column === 'is_available') {
|
||||
$is_available = Wiaas_Order_Project::is_order_project_available($id);
|
||||
$columns .= $is_available ? 'Yes' : 'No';
|
||||
}
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save posted is available field for created or edited order project
|
||||
* @param $term_id
|
||||
* @param string $tt_id
|
||||
* @param string $taxonomy
|
||||
*/
|
||||
public static function save_is_available_field($term_id, $tt_id = '', $taxonomy = '') {
|
||||
if ( 'shop_order_project' === $taxonomy ) {
|
||||
Wiaas_Order_Project::set_is_order_project_available($term_id, $_POST['is_available'] === 'on');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin_Order_Projects::init();
|
||||
@@ -6,348 +6,224 @@ class Wiaas_Cart_API {
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $namespace = 'wiaas/cart';
|
||||
private static $namespace = 'wiaas';
|
||||
|
||||
private static $rest_base = 'cart';
|
||||
|
||||
public static function register_routes() {
|
||||
register_rest_route( self::$namespace, 'count', array(
|
||||
'methods' => 'GET',
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/count', array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array(__CLASS__, 'get_cart_count'),
|
||||
'permission_callback' => array( __CLASS__, 'permission_check' ),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
) );
|
||||
|
||||
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, '/' . self::$rest_base . '/items', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array(__CLASS__, 'get_cart_items'),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array(__CLASS__, 'add_package_to_cart'),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
'args' => array(
|
||||
'package_id' => array(
|
||||
'description' => __( 'Wiaas package ID.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
),
|
||||
'price_id' => array(
|
||||
'description' => __( 'Selected price ID for Wiaas package.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'enum' => array_keys(Wiaas_Package_Pricing::get_available_pay_types()),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
),
|
||||
'options_ids' => array(
|
||||
'description' => __( 'Wiaas package options IDs.', 'wiaas' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
)
|
||||
),
|
||||
'addons_ids' => array(
|
||||
'description' => __( 'Wiaas package addons IDs.', 'wiaas' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
)
|
||||
)
|
||||
)
|
||||
),
|
||||
) );
|
||||
|
||||
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, '/' . self::$rest_base . '/items/(?P<key>[\w-]+)', array(
|
||||
'args' => array(
|
||||
'key' => array(
|
||||
'description' => __( 'Unique key identifier for cart package item.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array(__CLASS__, 'remove_package_from_cart'),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array(__CLASS__, 'update_package_quantity'),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
'args' => array(
|
||||
'quantity' => array(
|
||||
'description' => __( 'New quantity cart package item.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
)
|
||||
)
|
||||
)
|
||||
) );
|
||||
|
||||
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, '/' . self::$rest_base . '/checkout', array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array(__CLASS__, 'checkout'),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
'args' => array(
|
||||
'vat' => array(
|
||||
'description' => __( 'Vat Code for new order.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
),
|
||||
'company' => array(
|
||||
'description' => __( 'Company name for new order.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
),
|
||||
'reference' => array(
|
||||
'description' => __( 'Location details for new order.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
),
|
||||
'tender' => array(
|
||||
'description' => __( 'Invoice reference for new order.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
),
|
||||
'project_id' => array(
|
||||
'description' => __( 'Order project ID for new order.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'delivery_address_id' => array(
|
||||
'description' => __( 'ID of delivery address for new order.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'billing_address_id' => array(
|
||||
'description' => __( 'ID of billing address for new order.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
)
|
||||
) );
|
||||
|
||||
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['_wiaas_standard_package'])) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'count' => $count,
|
||||
return rest_ensure_response(array(
|
||||
'count' => Wiaas_Cart::get_cart_packages_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['_wiaas_standard_package'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$package = wc_get_product($item['product_id']);
|
||||
|
||||
// Retrieve package addons
|
||||
$addon_cart_items = wiaas_get_cart_item_addons($item);
|
||||
$additional_packages = array();
|
||||
|
||||
foreach ($addon_cart_items as $addon_cart_item) {
|
||||
$additional_package = wc_get_product($addon_cart_item['product_id']);
|
||||
$additional_packages[] = array(
|
||||
'idAdditionalPackage' => $additional_package->get_id(),
|
||||
'packageName' => $additional_package->get_title(),
|
||||
'prices' => array(
|
||||
'fixedExtra' => $addon_cart_item['_wiaas_payment']['fixed_extra'],
|
||||
'recurrentExtra' => $addon_cart_item['_wiaas_payment']['recurrent_extra'],
|
||||
'servicesExtra' => $addon_cart_item['_wiaas_payment']['services_extra'],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Retrieve package options
|
||||
$option_cart_items = wiaas_get_cart_item_options($item);
|
||||
$package_options = array();
|
||||
foreach ($option_cart_items as $option_cart_item) {
|
||||
$option_package = wc_get_product($option_cart_item['product_id']);
|
||||
$package_options[] = array(
|
||||
'idOptionPackage' => $option_package->get_id(),
|
||||
'groupName' => $option_cart_item['_wiaas_option_group_name'],
|
||||
'packageName' => $option_package->get_title(),
|
||||
'prices' => array(
|
||||
'fixedExtra' => $option_cart_item['_wiaas_payment']['fixed_extra'],
|
||||
'recurrentExtra' => $option_cart_item['_wiaas_payment']['recurrent_extra'],
|
||||
'servicesExtra' => $option_cart_item['_wiaas_payment']['services_extra'],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$totalPrices = Wiaas_Cart::get_cart_item_total($item);
|
||||
|
||||
$country = Wiaas_Countries::get_package_country($package);
|
||||
return rest_ensure_response(array(
|
||||
'items' => Wiaas_Cart::get_cart_packages(),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
$result[] = array(
|
||||
'idPackage' => $item['product_id'],
|
||||
'key' => $item['key'],
|
||||
'packageName' => $package->get_title(),
|
||||
'additionalPackages' => $additional_packages,
|
||||
'areAdditionalAvailable' => true,
|
||||
'areOptionsAvailable' => true,
|
||||
'bids' => array(),
|
||||
'commercialLead' => 'Coor Service Management',
|
||||
'country' => array(
|
||||
'currency' => $country['currency']
|
||||
),
|
||||
'options' => $package_options,
|
||||
'quantity' => $item['quantity'],
|
||||
/**
|
||||
* @param WP_REST_Request $request Request data.
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public static function add_package_to_cart($request) {
|
||||
|
||||
'idPayType' => $item['_wiaas_payment']['id'],
|
||||
'payType' => $item['_wiaas_payment']['payment_type'],
|
||||
'periodUnit' => $item['_wiaas_payment']['period_unit'],
|
||||
'idPrice' => $item['_wiaas_payment']['id'],
|
||||
'fixedPrice' => $item['_wiaas_payment']['fixed_extra'],
|
||||
'recurrentPrice' => $item['_wiaas_payment']['recurrent_extra'],
|
||||
'servicesPrice' => $item['_wiaas_payment']['services_extra'],
|
||||
|
||||
'totalPrices' => array(
|
||||
'fixedPrice' => $totalPrices['fixed_extra'],
|
||||
'recurrentPrice' => $totalPrices['recurrent_extra'],
|
||||
'servicesPrice' => $totalPrices['services_extra'],
|
||||
),
|
||||
|
||||
'status' => 'available',
|
||||
'idCommercialLead' => 14,
|
||||
);
|
||||
}
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'cartItems' => $result,
|
||||
'items' => $items,
|
||||
'totalPrice' => ''
|
||||
)
|
||||
$success = Wiaas_Cart::add_package_to_cart(
|
||||
$request['package_id'],
|
||||
$request['price_id'],
|
||||
$request['addons_ids'],
|
||||
$request['options_ids']
|
||||
);
|
||||
|
||||
if ($success) {
|
||||
return wiaas_api_notice('PACKAGE_ADDED', 'success');
|
||||
}
|
||||
|
||||
return wiaas_api_notice('PACKAGE_ALREADY_IN_CART', 'error');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cart documents
|
||||
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
|
||||
* @param WP_REST_Request $request Request data.
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public static function get_cart_documents() {
|
||||
return new WP_REST_Response(array(
|
||||
'areFilesUploaded' => true,
|
||||
'templates' => array(),
|
||||
'uploaded' => array()
|
||||
));
|
||||
}
|
||||
public static function remove_package_from_cart($request) {
|
||||
|
||||
/**
|
||||
* 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'];
|
||||
|
||||
$success = WC()->cart->add_to_cart($package_id, 1, 0, array(), array(
|
||||
'_wiaas_standard_package' => true
|
||||
));
|
||||
$success = Wiaas_Cart::remove_package_from_cart($request['key']);
|
||||
|
||||
if ($success) {
|
||||
return new WP_REST_Response(array(
|
||||
'messages' => array(
|
||||
array(
|
||||
'code' => 'success',
|
||||
'message' => 'PACKAGE_ADDED'
|
||||
)
|
||||
)
|
||||
));
|
||||
return wiaas_api_notice('PACKAGE_REMOVED_FROM_CART', 'success');
|
||||
}
|
||||
|
||||
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'
|
||||
)
|
||||
)
|
||||
));
|
||||
return wiaas_api_notice('INVALID_PACKAGE_FOR_REMOVE', 'error');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'];
|
||||
public static function update_package_quantity($request) {
|
||||
|
||||
$success = WC()->cart->set_quantity($package_item_key, $new_quantity, true);
|
||||
$success = Wiaas_Cart::update_package_quantity($request['key'], $request['quantity']);
|
||||
|
||||
if ($success) {
|
||||
return new WP_REST_Response(array(
|
||||
'messages' => array(
|
||||
array(
|
||||
'code' => 'success',
|
||||
'message' => 'QUANTITY_UPDATED'
|
||||
)
|
||||
)
|
||||
));
|
||||
return wiaas_api_notice('QUANTITY_UPDATED', 'success');
|
||||
}
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'messages' => array(
|
||||
array(
|
||||
'code' => 'error',
|
||||
'message' => 'QUANTITY_NOT_UPDATED'
|
||||
)
|
||||
)
|
||||
));
|
||||
return wiaas_api_notice('QUANTITY_NOT_UPDATED', 'error');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Placing order as part of checkout process
|
||||
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
|
||||
* @param WP_REST_Request $request Request data.
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function place_order() {
|
||||
public static function checkout($request) {
|
||||
|
||||
$details = $_POST['details'];
|
||||
$vat_code = $_POST['vat'];
|
||||
$company_name = $_POST['companyName'];
|
||||
$delivery_address = $_POST['delivery'];
|
||||
$billing_address = $_POST['billing'];
|
||||
Wiaas_Checkout::process_checkout($request->get_body_params());
|
||||
|
||||
$order_id = WC()->checkout()->create_order(array());
|
||||
$order = wc_get_order( $order_id );
|
||||
|
||||
// set order currency
|
||||
$line_items = $order->get_items();
|
||||
foreach ($line_items as $line_item) {
|
||||
if (isset($line_item['wiaas_currency'])) {
|
||||
$order->set_currency($line_item['wiaas_currency']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$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
|
||||
));
|
||||
return wiaas_api_notice('ORDER_PLACED', 'success');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Order_Projects_API {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $namespace = 'wiaas';
|
||||
|
||||
private static $rest_base = 'order-projects';
|
||||
|
||||
public static function register_routes() {
|
||||
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( __CLASS__, 'get_order_projects' ),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
'args' => array(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( __CLASS__, 'create_order_project' ),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
'args' => array(
|
||||
'name' => array(
|
||||
'type' => 'string',
|
||||
'description' => __( 'Order project name.', 'wiaas' ),
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'sanitize_title',
|
||||
'validate_callback' => function($param, $request, $key) {
|
||||
if ($param === '') {
|
||||
return new WP_Error(
|
||||
'rest_invalid_param',
|
||||
'Order project name cannot be empty!'
|
||||
);
|
||||
}
|
||||
if (strlen($param) > 100) {
|
||||
return new WP_Error(
|
||||
'rest_invalid_param',
|
||||
'Order project name cannot be longer than 100 characters!'
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves available order projects
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public static function get_order_projects() {
|
||||
$projects = Wiaas_Order_Project::get_available_order_projects();
|
||||
|
||||
return rest_ensure_response($projects);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new available order project
|
||||
*
|
||||
* @param WP_REST_Request $request Request data.
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public static function create_order_project($request) {
|
||||
$name = $request['name'];
|
||||
|
||||
$success = Wiaas_Order_Project::add_order_project($name);
|
||||
if ($success) {
|
||||
return wiaas_api_notice('PROJECT_ADDED', 'success');
|
||||
}
|
||||
|
||||
return wiaas_api_notice('INVALID_DATA', 'error');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -62,10 +62,10 @@ class Wiaas_REST_Customer_API {
|
||||
$new_address = json_decode($params['profile_address']);
|
||||
|
||||
if (!Wiaas_Customer::update_customer_profile_addresses($customer_id, $new_address)){
|
||||
return self::generate_wiaas_response('PROFILE_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('PROFILE_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('PROFILE_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('PROFILE_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
public static function delete_customer_profile_address(WP_REST_Request $request){
|
||||
@@ -73,10 +73,10 @@ class Wiaas_REST_Customer_API {
|
||||
$address_id = $request['address_id'];
|
||||
|
||||
if (!Wiaas_Customer::delete_customer_profile_address($customer_id, $address_id)){
|
||||
return self::generate_wiaas_response('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
public static function update_customer_billing_addresses(WP_REST_Request $request){
|
||||
@@ -85,10 +85,10 @@ class Wiaas_REST_Customer_API {
|
||||
$new_address = json_decode($params['billing_address']);
|
||||
|
||||
if (!Wiaas_Customer::update_customer_billing_addresses($customer_id, $new_address)){
|
||||
return self::generate_wiaas_response('BILLING_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('BILLING_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('BILLING_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('BILLING_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
|
||||
}
|
||||
|
||||
@@ -97,10 +97,10 @@ class Wiaas_REST_Customer_API {
|
||||
$address_id = $request['address_id'];
|
||||
|
||||
if (!Wiaas_Customer::delete_customer_billing_address($customer_id, $address_id)){
|
||||
return self::generate_wiaas_response('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
public static function update_customer_personal_info(WP_REST_Request $request){
|
||||
@@ -113,18 +113,18 @@ class Wiaas_REST_Customer_API {
|
||||
$name = $first_name . ' ' . $last_name;
|
||||
|
||||
if (!is_string($name) || strlen($name) === 1){
|
||||
return self::generate_wiaas_response('ADD_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('ADD_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
if (!is_string($phone) || strlen($phone) < 1){
|
||||
return self::generate_wiaas_response('ADD_PHONE_NUMBER', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('ADD_PHONE_NUMBER', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
if (!Wiaas_Customer::update_customer_profile_info($customer_id, $first_name, $last_name, $phone)){
|
||||
return self::generate_wiaas_response('PROFILE_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('PROFILE_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('PROFILE_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('PROFILE_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
public static function update_customer_company_info(WP_REST_Request $request){
|
||||
@@ -136,38 +136,18 @@ class Wiaas_REST_Customer_API {
|
||||
|
||||
|
||||
if (!is_string($company_name) || strlen($company_name) < 1){
|
||||
return self::generate_wiaas_response('ADD_COMPANY_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('ADD_COMPANY_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
if (!is_string($vat_code) || strlen($vat_code) < 1){
|
||||
return self::generate_wiaas_response('ADD_VAT', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('ADD_VAT', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
if (!Wiaas_Customer::update_customer_company_info($customer_id, $company_name, $vat_code)){
|
||||
return self::generate_wiaas_response('COMPANY_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
return wiaas_api_notice('COMPANY_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('COMPANY_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
|
||||
|
||||
return wiaas_api_notice('COMPANY_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private static function generate_wiaas_response($message, $code, $data = NULL){
|
||||
$response = array(
|
||||
'messages' => [
|
||||
array(
|
||||
'code' => $code,
|
||||
'message' => $message
|
||||
)
|
||||
],
|
||||
'data' => $data
|
||||
);
|
||||
|
||||
return new WP_REST_Response($response);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -93,15 +93,13 @@ class Wiass_REST_Delivery_Process_API {
|
||||
);
|
||||
}
|
||||
|
||||
$response = new WP_REST_Response( $data );
|
||||
|
||||
return $response;
|
||||
return rest_ensure_response($data);
|
||||
}
|
||||
|
||||
public static function get_customer_acceptance(WP_REST_Request $request){
|
||||
$entry = GFAPI::get_entry($request['entry_id']);
|
||||
if (is_wp_error($entry)){
|
||||
return self::generate_error('Customer acceptance entry not found', 404);
|
||||
return wiaas_api_generate_error('Customer acceptance entry not found', 404);
|
||||
}
|
||||
|
||||
$acceptance_documents = array();
|
||||
@@ -130,27 +128,25 @@ class Wiass_REST_Delivery_Process_API {
|
||||
$acceptance_status = ($entry[self::ACCEPTANCE_STATUS_FIELD_ID] === 'accept') ? 1 : -1;
|
||||
}
|
||||
|
||||
$result = array(
|
||||
return rest_ensure_response(array(
|
||||
'documents' => $acceptance_documents,
|
||||
'expiration' => $entry[self::EXPIRATION_DATE_FIELD_ID],
|
||||
'status' => $acceptance_status,
|
||||
'decline_reason' => $entry[self::DECLINE_REASON_FIELD_ID]
|
||||
);
|
||||
|
||||
return new WP_REST_Response($result);
|
||||
));
|
||||
}
|
||||
|
||||
public static function submit_customer_acceptance(WP_REST_Request $request){
|
||||
$entry = GFAPI::get_entry($request['entry_id']);
|
||||
if (is_wp_error($entry)){
|
||||
return self::generate_error('Customer acceptance entry not found', 404);
|
||||
return wiaas_api_generate_error('Customer acceptance entry not found', 404);
|
||||
}
|
||||
|
||||
$status = $request['actionType'];
|
||||
$reason = $request['declineReason'];
|
||||
|
||||
if (!in_array($status, self::ACCEPTABLE_STATUS)){
|
||||
return self::generate_wiaas_response('ACCEPTANCE_STATUS_MISSING', 'error');
|
||||
return wiaas_api_notice('ACCEPTANCE_STATUS_MISSING', 'error');
|
||||
}
|
||||
|
||||
$installation_declined = ($status === self::DECLINE_STATUS_LABEL);
|
||||
@@ -158,25 +154,25 @@ class Wiass_REST_Delivery_Process_API {
|
||||
$uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]);
|
||||
|
||||
if ($installation_declined && $reason === ''){
|
||||
return self::generate_wiaas_response('DECLINE_REASON_EMPTY', 'error');
|
||||
return wiaas_api_notice('DECLINE_REASON_EMPTY', 'error');
|
||||
}
|
||||
|
||||
if (!$installation_declined && (count($uploaded_files)===0)){
|
||||
return self::generate_wiaas_response('ACCEPTANCE_NOT_UPLOADED', 'error');
|
||||
return wiaas_api_notice('ACCEPTANCE_NOT_UPLOADED', 'error');
|
||||
}
|
||||
|
||||
$entry[self::DECLINE_REASON_FIELD_ID] = $reason;
|
||||
$entry[self::ACCEPTANCE_STATUS_FIELD_ID] = $status;
|
||||
|
||||
if (!GFAPI::update_entry( $entry )){
|
||||
return self::generate_wiaas_response('INTERNAL_SERVER_ERROR', 'error');
|
||||
return wiaas_api_notice('INTERNAL_SERVER_ERROR', 'error');
|
||||
}
|
||||
|
||||
//Check if step is already completed, to not submit again
|
||||
$gf_api = new Gravity_Flow_API($entry['form_id']);
|
||||
$current_step = $gf_api->get_current_step($entry);
|
||||
if ($current_step->get_name() !== self::USER_INPUT_STEP_NAME){
|
||||
return self::generate_wiaas_response('ACCEPTANCE_STATUS_UPDATED', 'success');
|
||||
return wiaas_api_notice('ACCEPTANCE_STATUS_UPDATED', 'success');
|
||||
}
|
||||
|
||||
if ( $current_step ) {
|
||||
@@ -195,20 +191,20 @@ class Wiass_REST_Delivery_Process_API {
|
||||
|
||||
|
||||
if ($installation_declined){
|
||||
return self::generate_wiaas_response('INSTALLATION_DECLINED', 'success');
|
||||
return wiaas_api_notice('INSTALLATION_DECLINED', 'success');
|
||||
}
|
||||
return self::generate_wiaas_response('INSTALLATION_ACCEPTED', 'success');
|
||||
return wiaas_api_notice('INSTALLATION_ACCEPTED', 'success');
|
||||
}
|
||||
|
||||
public static function upload_file(WP_REST_Request $request){
|
||||
$files = $request->get_file_params();
|
||||
if (!$files[self::FILE_KEY_NAME]){
|
||||
return self::generate_wiaas_response('NO_FILES_UPLOADED', 'error');
|
||||
return wiaas_api_notice('NO_FILES_UPLOADED', 'error');
|
||||
}
|
||||
|
||||
$entry = GFAPI::get_entry($request['entry_id']);
|
||||
if (is_wp_error($entry)){
|
||||
return self::generate_error('Customer acceptance entry not found', 404);
|
||||
return wiaas_api_generate_error('Customer acceptance entry not found', 404);
|
||||
}
|
||||
|
||||
$form = GFAPI::get_form($entry['form_id']);
|
||||
@@ -223,12 +219,12 @@ class Wiass_REST_Delivery_Process_API {
|
||||
$file_path_details = pathinfo($file_name);
|
||||
|
||||
if ( GFCommon::file_name_has_disallowed_extension( $file_name ) ) {
|
||||
return self::generate_wiaas_response('INVALID_FILE_ACCEPTANCE', 'error');
|
||||
return wiaas_api_notice('INVALID_FILE_ACCEPTANCE', 'error');
|
||||
}
|
||||
$allowed_extensions = ! empty( $upload_file_field->allowedExtensions ) ? GFCommon::clean_extensions( explode( ',', strtolower( $upload_file_field->allowedExtensions ) ) ) : array();
|
||||
if ( ! empty( $allowed_extensions ) ) {
|
||||
if ( ! GFCommon::match_file_extension( $file_name, $allowed_extensions ) ) {
|
||||
return self::generate_wiaas_response('INVALID_FILE_ACCEPTANCE', 'error');
|
||||
return wiaas_api_notice('INVALID_FILE_ACCEPTANCE', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,13 +232,13 @@ class Wiass_REST_Delivery_Process_API {
|
||||
|
||||
// Bypasses security checks when running unit tests.
|
||||
if ( defined( 'WP_TEST_IN_PROGRESS' ) && WP_TEST_IN_PROGRESS ) {
|
||||
return self::generate_wiaas_response('FILE_UPLOADED', 'success');
|
||||
return wiaas_api_notice('FILE_UPLOADED', 'success');
|
||||
}
|
||||
|
||||
if ( move_uploaded_file($files[self::FILE_KEY_NAME]['tmp_name'], $target_path . $new_file_name ) ) {
|
||||
GFFormsModel::set_permissions( $target_path . $new_file_name );
|
||||
} else {
|
||||
return self::generate_wiaas_response('INTERNAL_SERVER_ERROR', 'error');
|
||||
return wiaas_api_notice('INTERNAL_SERVER_ERROR', 'error');
|
||||
}
|
||||
|
||||
//Extract path relative to the root
|
||||
@@ -269,35 +265,9 @@ class Wiass_REST_Delivery_Process_API {
|
||||
$entry[self::UPLOADED_FILES_FIELD_ID] = json_encode($uploaded_files);
|
||||
|
||||
if (GFAPI::update_entry( $entry )) {
|
||||
return self::generate_wiaas_response('FILE_UPLOADED','success');
|
||||
return wiaas_api_notice('FILE_UPLOADED','success');
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('NOT_UPLOADED', 'error');
|
||||
}
|
||||
|
||||
//Helper function
|
||||
private static function generate_error($message, $code = 500){
|
||||
$error = array(
|
||||
'status' => $code,
|
||||
'message' => $message,
|
||||
);
|
||||
|
||||
$result = new WP_REST_Response($error);
|
||||
$result->set_status($code);
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function generate_wiaas_response($message, $code, $data = NULL){
|
||||
$response = array(
|
||||
'messages' => [
|
||||
array(
|
||||
'code' => $code,
|
||||
'message' => $message
|
||||
)
|
||||
],
|
||||
'data' => $data
|
||||
);
|
||||
|
||||
return new WP_REST_Response($response);
|
||||
return wiaas_api_notice('NOT_UPLOADED', 'error');
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ class Wiass_REST_User_API {
|
||||
public static function validate_token() {
|
||||
$user = wp_get_current_user();
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
return rest_ensure_response(array(
|
||||
'userInfo' => array(
|
||||
'wiaas_id_user' => $user->ID,
|
||||
'wiaas_is_company_admin' => 1, //TODO: don't hardcode this
|
||||
@@ -45,6 +45,6 @@ class Wiass_REST_User_API {
|
||||
public static function get_countries(){
|
||||
$countries = Wiaas_Countries::get_list_of_countries();
|
||||
|
||||
return new WP_REST_Response($countries);
|
||||
return rest_ensure_response($countries);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* Generates REST API notice response
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $code
|
||||
* @param array|null $data
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
function wiaas_api_notice($message, $code, $data = null) {
|
||||
return rest_ensure_response(array(
|
||||
'messages' => [
|
||||
array(
|
||||
'code' => $code,
|
||||
'message' => $message
|
||||
)
|
||||
],
|
||||
'data' => $data
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate REST API error
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
function wiaas_api_generate_error($message, $code = 500) {
|
||||
$response = rest_ensure_response(array(
|
||||
'status' => $code,
|
||||
'message' => $message,
|
||||
));
|
||||
$response->set_status($code);
|
||||
return $response;
|
||||
}
|
||||
@@ -12,6 +12,9 @@ class Wiaas_Admin {
|
||||
require_once dirname(__FILE__) . '/admin/template/class-wiaas-admin-template-selection.php';
|
||||
require_once dirname(__FILE__) . '/admin/template/class-wiaas-template-products.php';
|
||||
require_once dirname(__FILE__) . '/admin/template/class-wiaas-template-admin-ajax.php';
|
||||
|
||||
// Admin order projects interface
|
||||
require_once dirname(__FILE__) . '/admin/class-wiaas-admin-order-projects.php';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,11 @@ class Wiaas_API {
|
||||
#Customer controller
|
||||
include_once dirname( __FILE__ ) . '/api/class-wiaas-rest-customer.php';
|
||||
|
||||
include_once dirname( __FILE__ ) . '/api/class-wiaas-order-projects-api.php';
|
||||
|
||||
// API functions
|
||||
include_once dirname( __FILE__ ) . '/api/wiaas-api-functions.php';
|
||||
|
||||
}
|
||||
|
||||
public static function register_rest_routes() {
|
||||
@@ -49,7 +54,8 @@ class Wiaas_API {
|
||||
'Wiaas_Cart_API',
|
||||
'Wiaas_Document_API',
|
||||
'Wiass_REST_User_API',
|
||||
'Wiaas_REST_Customer_API'
|
||||
'Wiaas_REST_Customer_API',
|
||||
'Wiaas_Order_Projects_API'
|
||||
);
|
||||
|
||||
foreach ( $controllers as $controller ) {
|
||||
|
||||
@@ -6,7 +6,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
|
||||
|
||||
/**
|
||||
* TODO: This implementation is temporary and will probably be changed during implementation of currently pending cart task
|
||||
* Handles Wiaas Cart specific tasks
|
||||
*
|
||||
* Class Wiaas_Cart
|
||||
*/
|
||||
@@ -18,109 +18,140 @@ class Wiaas_Cart {
|
||||
|
||||
add_filter( 'woocommerce_hidden_order_itemmeta', array( __CLASS__, 'hidden_order_item_meta' ) );
|
||||
|
||||
add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ), 10, 2 );
|
||||
|
||||
add_action( 'woocommerce_before_calculate_totals', array( __CLASS__, 'on_calculate_totals' ), 99, 1);
|
||||
|
||||
add_action( 'woocommerce_cart_loaded_from_session', array( __CLASS__, 'on_calculate_totals' ), 99, 1);
|
||||
|
||||
// Add options and addons to cart.
|
||||
add_action( 'woocommerce_add_to_cart', array( __CLASS__, 'add_additional_packages_to_cart' ), 10, 6 );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handles adding standard wiaas package to cart along with selected addons and options
|
||||
*
|
||||
* Extend cart item data with wiaas package payment info and addons and options
|
||||
*
|
||||
* Every package cart item will be extended with its payment info but only standard package type
|
||||
* will be extended with addons and options arrays
|
||||
*
|
||||
* @param $cart_item_data
|
||||
* @param $package_id
|
||||
* @param int $package_id Package ID of selected package
|
||||
* @param string $price_id Price ID of selected package payment
|
||||
* @param array $addons_ids Array of selected additional packages IDs
|
||||
* @param array $options_ids Array of selected option packages IDs
|
||||
*
|
||||
* @return array
|
||||
* @return bool TRUE if all packages are succesfully added to cart, FALSE otherwise
|
||||
*/
|
||||
public static function add_cart_item_data($cart_item_data, $package_id) {
|
||||
public static function add_package_to_cart($package_id, $price_id, $addons_ids, $options_ids) {
|
||||
// try adding package to cart
|
||||
try {
|
||||
//Check if package exists
|
||||
$package = wc_get_product( $package_id );
|
||||
if (!$package) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$is_valid = isset( $_POST[ 'price_id' ]) &&
|
||||
WC_Product_Factory::get_product_type( $package_id ) === 'bundle';
|
||||
// Retrieve package country
|
||||
$country = Wiaas_Countries::get_package_country($package);
|
||||
|
||||
if (!$is_valid) {
|
||||
return $cart_item_data;
|
||||
// Retrieve package price
|
||||
$package_prices = Wiaas_Pricing::get_standard_package_customer_prices($package);
|
||||
$selected_price_index = array_search($price_id, array_column($package_prices, 'id'));
|
||||
|
||||
// Initialize additional cart item data for wiaas packages
|
||||
$wiaas_cart_item_data = array(
|
||||
'_wiaas_standard_package' => true,
|
||||
'_wiaas_addon_items' => array(),
|
||||
'_wiaas_option_items' => array(),
|
||||
'_wiaas_currency' => isset($country) ? $country['currency'] : get_woocommerce_currency(),
|
||||
'_wiaas_payment' => $package_prices[$selected_price_index] ? $package_prices[$selected_price_index] : null
|
||||
);
|
||||
|
||||
$cart_item_key = WC()->cart->add_to_cart($package_id, 1, 0, array(), $wiaas_cart_item_data);
|
||||
|
||||
if (!$cart_item_key) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add selected additional packages and options
|
||||
self::_add_additional_packages_to_cart($cart_item_key, $price_id, $addons_ids, $options_ids);
|
||||
|
||||
// Trigger calculation of total prices after additional packages are added
|
||||
WC()->cart->calculate_totals();
|
||||
|
||||
return true;
|
||||
|
||||
} catch( Exception $e) {
|
||||
|
||||
error_log($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove given package item key from cart
|
||||
*
|
||||
* @param string $package_cart_item_key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function remove_package_from_cart($package_cart_item_key) {
|
||||
|
||||
$cart_item = WC()->cart->get_cart_item($package_cart_item_key);
|
||||
|
||||
if (!$cart_item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$package_type = Wiaas_Package_Type::get_package_type($package_id);
|
||||
$package_addon_item_keys = $cart_item['_wiaas_addon_items'];
|
||||
$package_option_item_keys = $cart_item['_wiaas_option_items'];
|
||||
|
||||
$package = wc_get_product( $package_id );
|
||||
$success = WC()->cart->remove_cart_item($package_cart_item_key);
|
||||
|
||||
switch ($package_type) {
|
||||
case 'standard':
|
||||
if ($success) {
|
||||
foreach ($package_addon_item_keys as $package_addon_item_key) {
|
||||
WC()->cart->remove_cart_item($package_addon_item_key);
|
||||
}
|
||||
|
||||
$cart_item['_wiaas_standard_package'] = true;
|
||||
|
||||
// Prepare addons additional data for later use.
|
||||
if ( ! isset( $cart_item_data['_wiaas_addon_items'] ) ) {
|
||||
$cart_item_data['_wiaas_addon_items' ] = array();
|
||||
}
|
||||
// Prepare options additional data for later use.
|
||||
if ( ! isset( $cart_item_data['_wiaas_option_items'] ) ) {
|
||||
$cart_item_data['_wiaas_option_items' ] = array();
|
||||
}
|
||||
|
||||
$country = Wiaas_Countries::get_package_country($package);
|
||||
if (isset($country)) {
|
||||
$cart_item_data['_wiaas_currency'] = $country['currency'];
|
||||
}
|
||||
|
||||
$package_prices = Wiaas_Pricing::get_standard_package_customer_prices($package);
|
||||
break;
|
||||
case 'add_on':
|
||||
$parent_key = $cart_item_data['_wiaas_addon_for'];
|
||||
$parent_item = WC()->cart->get_cart_item($parent_key);
|
||||
|
||||
$package_prices = Wiaas_Pricing::get_addon_package_customer_price($package, $parent_item['data']);
|
||||
break;
|
||||
case 'option':
|
||||
$parent_key = $cart_item_data['_wiaas_option_for'];
|
||||
$parent_item = WC()->cart->get_cart_item($parent_key);
|
||||
|
||||
$option_group_name = Wiaas_Package_Option_Groups::get_group_name_for_package_option($parent_item['data'], $package);
|
||||
$cart_item_data['_wiaas_option_group_name' ] = $option_group_name;
|
||||
|
||||
$package_prices = Wiaas_Pricing::get_option_package_customer_price($package, $parent_item['data']);
|
||||
break;
|
||||
foreach ($package_option_item_keys as $package_option_item_key) {
|
||||
WC()->cart->remove_cart_item($package_option_item_key);
|
||||
}
|
||||
}
|
||||
|
||||
$selected_price_index = array_search($_POST['price_id'], array_column($package_prices, 'id'));
|
||||
|
||||
if (is_numeric($selected_price_index) && isset($package_prices[$selected_price_index])) {
|
||||
$cart_item_data['_wiaas_payment'] = $package_prices[$selected_price_index];
|
||||
}
|
||||
|
||||
return $cart_item_data;
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add selected package options and addons after parent standard package is added to cart
|
||||
* Update quantity of package in cart
|
||||
* @param string $package_cart_item_key
|
||||
* @param int $new_quantity
|
||||
*
|
||||
* @param $cart_item_key
|
||||
* @param $package_id
|
||||
* @param $quantity
|
||||
* @param $variation_id
|
||||
* @param $variation
|
||||
* @param $cart_item_data
|
||||
* @return bool
|
||||
*/
|
||||
public static function add_additional_packages_to_cart($cart_item_key, $package_id, $quantity, $variation_id, $variation, $cart_item_data) {
|
||||
public static function update_package_quantity($package_cart_item_key, $new_quantity) {
|
||||
$cart_item = WC()->cart->get_cart_item($package_cart_item_key);
|
||||
|
||||
remove_action( 'woocommerce_add_to_cart', array( __CLASS__, 'add_additional_packages_to_cart' ));
|
||||
if (!$cart_item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
self::_add_options_to_cart($cart_item_key, $package_id, $cart_item_data);
|
||||
$package_addon_item_keys = $cart_item['_wiaas_addon_items'];
|
||||
$package_option_item_keys = $cart_item['_wiaas_option_items'];
|
||||
|
||||
self::_add_addons_to_cart($cart_item_key, $package_id, $cart_item_data);
|
||||
$success = WC()->cart->set_quantity($package_cart_item_key, $new_quantity, true);
|
||||
|
||||
add_action( 'woocommerce_add_to_cart', array( __CLASS__, 'add_additional_packages_to_cart' ), 10, 6 );
|
||||
if ($success) {
|
||||
foreach ($package_addon_item_keys as $package_addon_item_key) {
|
||||
WC()->cart->set_quantity($package_addon_item_key, $new_quantity, true);
|
||||
}
|
||||
|
||||
foreach ($package_option_item_keys as $package_option_item_key) {
|
||||
WC()->cart->set_quantity($package_option_item_key, $new_quantity, true);
|
||||
}
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update package cart item with `minimal_fixed_price` as its price
|
||||
* so resulting totals would be sum of these prices
|
||||
@@ -264,72 +295,201 @@ class Wiaas_Cart {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves count of wiaas standard packages in cart
|
||||
*
|
||||
* @return int Number of wiaas standard packages in cart
|
||||
*/
|
||||
public static function get_cart_packages_count() {
|
||||
$items = WC()->cart->get_cart_contents();
|
||||
|
||||
return array_reduce($items, function($count, $item) {
|
||||
if (isset($item['_wiaas_standard_package'])) {
|
||||
$count++;
|
||||
}
|
||||
return $count;
|
||||
}, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves packages data from cart
|
||||
* @return array
|
||||
*/
|
||||
public static function get_cart_packages() {
|
||||
$items = WC()->cart->get_cart_contents();
|
||||
|
||||
$package_items = array();
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
if (!isset($item['_wiaas_standard_package'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$package = wc_get_product($item['product_id']);
|
||||
|
||||
// Collect additional packages
|
||||
$additional_packages = array_map(
|
||||
function($addon_cart_item) {
|
||||
$additional_package = wc_get_product($addon_cart_item['product_id']);
|
||||
return array(
|
||||
'package_id' => $additional_package->get_id(),
|
||||
'package_name' => $additional_package->get_title(),
|
||||
'prices' => array(
|
||||
'fixed_extra' => $addon_cart_item['_wiaas_payment']['fixed_extra'],
|
||||
'recurrent_extra' => $addon_cart_item['_wiaas_payment']['recurrent_extra'],
|
||||
'services_extra' => $addon_cart_item['_wiaas_payment']['services_extra'],
|
||||
)
|
||||
);
|
||||
},
|
||||
wiaas_get_cart_item_addons($item));
|
||||
|
||||
|
||||
// Collect package options
|
||||
$package_options = array_map(
|
||||
function($option_cart_item) {
|
||||
$option_package = wc_get_product($option_cart_item['product_id']);
|
||||
return array(
|
||||
'package_id' => $option_package->get_id(),
|
||||
'group_name' => $option_cart_item['_wiaas_option_group_name'],
|
||||
'package_name' => $option_package->get_title(),
|
||||
'prices' => array(
|
||||
'fixed_extra' => $option_cart_item['_wiaas_payment']['fixed_extra'],
|
||||
'recurrent_extra' => $option_cart_item['_wiaas_payment']['recurrent_extra'],
|
||||
'services_extra' => $option_cart_item['_wiaas_payment']['services_extra'],
|
||||
)
|
||||
);
|
||||
},
|
||||
wiaas_get_cart_item_options($item));
|
||||
|
||||
$package_items[] = array(
|
||||
'package_id' => $item['product_id'],
|
||||
'key' => $item['key'],
|
||||
'package_name' => $package->get_title(),
|
||||
'quantity' => $item['quantity'],
|
||||
|
||||
'commercial_lead_id' => 14,
|
||||
'commercial_lead' => 'Coor Service Management',
|
||||
'country' => Wiaas_Countries::get_package_country($package),
|
||||
|
||||
'are_additional_available' => true,
|
||||
'additional_packages' => $additional_packages,
|
||||
|
||||
'are_options_available' => true,
|
||||
'options' => $package_options,
|
||||
|
||||
'bids' => array(),
|
||||
|
||||
'payment_info' => array(
|
||||
'id' => $item['_wiaas_payment']['id'],
|
||||
'type' => $item['_wiaas_payment']['payment_type'],
|
||||
'period_unit' => $item['_wiaas_payment']['period_unit'],
|
||||
'fixed_extra' => $item['_wiaas_payment']['fixed_extra'],
|
||||
'recurrent_extra' => $item['_wiaas_payment']['recurrent_extra'],
|
||||
'services_extra' => $item['_wiaas_payment']['services_extra'],
|
||||
),
|
||||
|
||||
'total_prices' => Wiaas_Cart::get_cart_item_total($item),
|
||||
|
||||
'status' => 'available',
|
||||
);
|
||||
}
|
||||
|
||||
return $package_items;
|
||||
}
|
||||
|
||||
|
||||
//PRIVATE
|
||||
|
||||
/**
|
||||
* Add selected package options to cart
|
||||
* @param $cart_item_key
|
||||
* @param $package_id
|
||||
* @param $cart_item_data
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function _add_options_to_cart($cart_item_key, $package_id, $cart_item_data) {
|
||||
$is_option_parent = $_POST['package_id'] = $package_id && isset($cart_item_data['_wiaas_option_items']);
|
||||
$has_selected_options = isset($_POST['options']) && is_array($_POST['options']);
|
||||
|
||||
if ($is_option_parent && $has_selected_options) {
|
||||
|
||||
$options_ids = $_POST['options'];
|
||||
|
||||
foreach ($options_ids as $option_id) {
|
||||
$option_package = wc_get_product($option_id);
|
||||
if (is_object($option_package)) {
|
||||
|
||||
$option_cart_item_key = WC()->cart->add_to_cart($option_id, 1, 0, array(), array(
|
||||
'_wiaas_option_for' => $cart_item_key
|
||||
));
|
||||
|
||||
if ($option_cart_item_key) {
|
||||
WC()->cart->cart_contents[ $cart_item_key ]['_wiaas_option_items'][] = $option_cart_item_key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add selected package addons to cart
|
||||
* Add selected package options and addons after parent standard package is added to cart
|
||||
*
|
||||
* @param string $package_cart_item_key
|
||||
* @param int $price_id
|
||||
* @param array $addons_ids
|
||||
* @param array $options_ids
|
||||
*
|
||||
* @param $cart_item_key
|
||||
* @param $package_id
|
||||
* @param $cart_item_data
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws Exception if any of the addons or options cannot be added to cart
|
||||
*/
|
||||
private static function _add_addons_to_cart($cart_item_key, $package_id, $cart_item_data) {
|
||||
private static function _add_additional_packages_to_cart($package_cart_item_key, $price_id, $addons_ids, $options_ids) {
|
||||
|
||||
$is_addon_parent = $_POST['package_id'] = $package_id && isset($cart_item_data['_wiaas_addon_items']);
|
||||
$has_selected_addons = isset($_POST['addons']) && is_array($_POST['addons']);
|
||||
$parent_item = WC()->cart->get_cart_item($package_cart_item_key);
|
||||
|
||||
if ($is_addon_parent && $has_selected_addons) {
|
||||
$addons_ids = $_POST['addons'];
|
||||
$addon_items_keys = array();
|
||||
$option_items_keys = array();
|
||||
|
||||
foreach ($addons_ids as $addon_id) {
|
||||
$addon_package = wc_get_product($addon_id);
|
||||
if (is_object($addon_package)) {
|
||||
|
||||
$addon_cart_item_key = WC()->cart->add_to_cart($addon_id, 1, 0, array(), array(
|
||||
'_wiaas_addon_for' => $cart_item_key
|
||||
));
|
||||
|
||||
if ($addon_cart_item_key) {
|
||||
WC()->cart->cart_contents[ $cart_item_key ]['_wiaas_addon_items'][] = $addon_cart_item_key;
|
||||
}
|
||||
}
|
||||
// Try adding package addons to cart
|
||||
foreach ($addons_ids as $addon_id) {
|
||||
//Check if addon package exists
|
||||
$addon_package = wc_get_product($addon_id);
|
||||
if (!$addon_package) {
|
||||
throw new Exception( __( 'Sorry, additional package does not exist.', 'wiaas' ) );
|
||||
}
|
||||
|
||||
// Retrieve addon package price
|
||||
$package_prices = Wiaas_Pricing::get_addon_package_customer_price($addon_package, $parent_item['data']);
|
||||
$selected_price_index = array_search($price_id, array_column($package_prices, 'id'));
|
||||
|
||||
// Initialize additional cart item data for wiaas addon packages
|
||||
$wiaas_addon_cart_item_data = array(
|
||||
'_wiaas_addon_for' => $package_cart_item_key,
|
||||
'_wiaas_payment' => $package_prices[$selected_price_index] ? $package_prices[$selected_price_index] : null
|
||||
);
|
||||
|
||||
$addon_cart_item_key = WC()->cart->add_to_cart($addon_id,
|
||||
1,
|
||||
0,
|
||||
array(),
|
||||
$wiaas_addon_cart_item_data
|
||||
);
|
||||
|
||||
if (!$addon_cart_item_key) {
|
||||
throw new Exception( __( 'Sorry, additional package could not be added to cart.', 'wiaas' ) );
|
||||
}
|
||||
|
||||
$addon_items_keys[] = $addon_cart_item_key;
|
||||
}
|
||||
|
||||
// Try adding package options to cart
|
||||
foreach ($options_ids as $option_id) {
|
||||
//Check if option package exists
|
||||
$option_package = wc_get_product($option_id);
|
||||
if (!$option_package) {
|
||||
throw new Exception( __( 'Sorry, option package does not exist.', 'wiaas' ) );
|
||||
}
|
||||
|
||||
// Retrieve option package price
|
||||
$package_prices = Wiaas_Pricing::get_option_package_customer_price($option_package, $parent_item['data']);
|
||||
$selected_price_index = array_search($price_id, array_column($package_prices, 'id'));
|
||||
|
||||
// Retrieve option package group name
|
||||
$option_group_name = Wiaas_Package_Option_Groups::get_group_name_for_package_option($parent_item['data'], $option_package);
|
||||
|
||||
|
||||
// Initialize additional cart item data for wiaas option packages
|
||||
$wiaas_option_cart_item_data = array(
|
||||
'_wiaas_option_for' => $package_cart_item_key,
|
||||
'_wiaas_payment' => $package_prices[$selected_price_index] ? $package_prices[$selected_price_index] : null,
|
||||
'_wiaas_option_group_name' => $option_group_name,
|
||||
);
|
||||
|
||||
$option_cart_item_key = WC()->cart->add_to_cart($option_id,
|
||||
1,
|
||||
0,
|
||||
array(),
|
||||
$wiaas_option_cart_item_data
|
||||
);
|
||||
|
||||
if (!$option_cart_item_key) {
|
||||
throw new Exception( __( 'Sorry, package option could not be added to cart.', 'wiaas' ) );
|
||||
}
|
||||
|
||||
$option_items_keys[] = $option_cart_item_key;
|
||||
}
|
||||
|
||||
WC()->cart->cart_contents[ $package_cart_item_key ]['_wiaas_addon_items'] = $addon_items_keys;
|
||||
WC()->cart->cart_contents[ $package_cart_item_key ]['_wiaas_option_items'] = $option_items_keys;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
76
backend/app/plugins/wiaas/includes/class-wiaas-checkout.php
Normal file
76
backend/app/plugins/wiaas/includes/class-wiaas-checkout.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Checkout {
|
||||
|
||||
public static function init() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Process the order checkout.
|
||||
*
|
||||
* @param array $data Posted data.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function process_checkout($data) {
|
||||
try {
|
||||
|
||||
$customer = wp_get_current_user();
|
||||
|
||||
$order_id = WC()->checkout()->create_order(array());
|
||||
$order = wc_get_order( $order_id );
|
||||
|
||||
// set order currency
|
||||
$line_items = $order->get_items();
|
||||
foreach ($line_items as $line_item) {
|
||||
if (isset($line_item['wiaas_currency'])) {
|
||||
$order->set_currency($line_item['wiaas_currency']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$delivery_address = Wiaas_Customer::get_customer_profile_address($customer->ID, $data['delivery_address_id']);
|
||||
$billing_address = Wiaas_Customer::get_customer_billing_address($customer->ID, $data['billing_address_id']);
|
||||
|
||||
if (isset($delivery_address)) {
|
||||
$order->set_shipping_city($delivery_address['city']);
|
||||
$order->set_shipping_country($delivery_address['country_name']);
|
||||
$order->set_shipping_address_1($delivery_address['detailed_address']);
|
||||
$order->set_shipping_postcode($delivery_address['zip_code']);
|
||||
$order->set_shipping_first_name($delivery_address['first_name']);
|
||||
$order->set_shipping_last_name($delivery_address['last_name']);
|
||||
}
|
||||
|
||||
if (isset($billing_address)) {
|
||||
$order->set_billing_city($billing_address['city']);
|
||||
$order->set_billing_country($billing_address['country_name']);
|
||||
$order->set_billing_address_1($billing_address['detailed_address']);
|
||||
$order->set_billing_postcode($billing_address['zip_code']);
|
||||
$order->set_billing_first_name($billing_address['first_name']);
|
||||
$order->set_billing_last_name($billing_address['last_name']);
|
||||
}
|
||||
|
||||
Wiaas_Order::set_order_vat($order_id, $data['vat']);
|
||||
Wiaas_Order::set_order_company($order_id, $data['company_name']);
|
||||
Wiaas_Order::set_order_reference($order_id, $data['reference']);
|
||||
Wiaas_Order::set_order_tender($order_id, $data['tender']);
|
||||
|
||||
if (isset($data['project_id'])) {
|
||||
Wiaas_Order_Project::set_project_for_order($order_id, $data['project_id']);
|
||||
}
|
||||
|
||||
$order->payment_complete();
|
||||
|
||||
WC()->cart->empty_cart( true );
|
||||
|
||||
return true;
|
||||
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Checkout::init();
|
||||
@@ -125,6 +125,38 @@ class Wiaas_Order {
|
||||
return $response;
|
||||
}
|
||||
|
||||
public static function set_order_vat($order_id, $vat_code) {
|
||||
add_post_meta($order_id, '_wiaas_vat_code', $vat_code);
|
||||
}
|
||||
|
||||
public static function get_order_vat($order_id) {
|
||||
return get_post_meta($order_id, '_wiaas_vat_code', true);
|
||||
}
|
||||
|
||||
public static function set_order_company($order_id, $company_name) {
|
||||
add_post_meta($order_id, '_wiaas_company_name', $company_name);
|
||||
}
|
||||
|
||||
public static function get_order_company($order_id) {
|
||||
return get_post_meta($order_id, '_wiaas_company_name', true);
|
||||
}
|
||||
|
||||
public static function set_order_reference($order_id, $reference) {
|
||||
add_post_meta($order_id, '_wiaas_reference', $reference);
|
||||
}
|
||||
|
||||
public static function get_order_reference($order_id) {
|
||||
return get_post_meta($order_id, '_wiaas_reference', true);
|
||||
}
|
||||
|
||||
public static function set_order_tender($order_id, $tender) {
|
||||
add_post_meta($order_id, '_wiaas_tender', $tender);
|
||||
}
|
||||
|
||||
public static function get_order_tender($order_id) {
|
||||
return get_post_meta($order_id, '_wiaas_tender', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* PRIVATE
|
||||
*/
|
||||
@@ -136,7 +168,12 @@ class Wiaas_Order {
|
||||
* @param $request
|
||||
*/
|
||||
private static function _append_wiaas_order_details($data, $order, $request) {
|
||||
$data['reference'] = get_post_meta($order->get_id(), '_wiaas_reference', true);
|
||||
$data['reference'] = self::get_order_reference($order->get_id());
|
||||
$data['tender'] = self::get_order_tender($order->get_id());
|
||||
$data['vat'] = self::get_order_vat($order->get_id());
|
||||
$data['company_name'] = self::get_order_company($order->get_id());
|
||||
|
||||
$data['project_name'] = Wiaas_Order_Project::get_project_name_for_order($order->get_id());
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,9 @@ class Wiaas_Order_Project {
|
||||
add_action('woocommerce_after_register_taxonomy', array(__CLASS__, 'register_order_project_taxonomy'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register order project taxonomy
|
||||
*/
|
||||
public static function register_order_project_taxonomy() {
|
||||
$labels = array(
|
||||
'name' => _x( 'Project', 'taxonomy general name', 'wiaas' ),
|
||||
@@ -33,6 +36,102 @@ class Wiaas_Order_Project {
|
||||
register_taxonomy( 'shop_order_project', array( 'shop_order' ), $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves available order projects
|
||||
* @return array List of available order projects
|
||||
*/
|
||||
public static function get_available_order_projects() {
|
||||
$all_terms = get_terms(array(
|
||||
'taxonomy' => 'shop_order_project',
|
||||
'hide_empty' => false
|
||||
));
|
||||
|
||||
if (is_wp_error($all_terms)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$available_terms = array_filter($all_terms, function($term) {
|
||||
return self::is_order_project_available($term->term_id);
|
||||
});
|
||||
|
||||
return array_map(function($term) {
|
||||
return array(
|
||||
'id' => $term->term_id,
|
||||
'name' => $term->name
|
||||
);
|
||||
}, $available_terms);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves order project name
|
||||
* @param int $order_id
|
||||
*
|
||||
* @return string|null Project name if order is assigned to project, null otherwise
|
||||
*/
|
||||
public static function get_project_name_for_order($order_id) {
|
||||
$terms = wp_get_object_terms($order_id, 'shop_order_project');
|
||||
return isset($terms[0]) ? $terms[0]->name : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns giver order to given project
|
||||
* @param int $order_id
|
||||
* @param int $project_id
|
||||
*/
|
||||
public static function set_project_for_order($order_id, $project_id) {
|
||||
wp_set_object_terms($order_id, $project_id, 'shop_order_project', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new order project
|
||||
* @param string $name Order Project name
|
||||
* @param bool $is_available Flag indicating is order project available
|
||||
*
|
||||
* @return bool True on success, False on failure.
|
||||
*/
|
||||
public static function add_order_project($name, $is_available = true) {
|
||||
$result = wp_create_term($name, 'shop_order_project');
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// save is term available
|
||||
$id = is_numeric($result) ? $result : $result['term_id'];
|
||||
self::_save_available_field($id, $is_available);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|WP_Term $project Order project or order project id
|
||||
* @param bool $is_available
|
||||
*/
|
||||
public static function set_is_order_project_available($project, $is_available) {
|
||||
$project_id = is_numeric($project) ? $project : $project->term_id;
|
||||
self::_save_available_field($project_id, $is_available);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves if order project is available
|
||||
* @param int|WP_Term $project Order project objects or id of order project
|
||||
*
|
||||
* @return bool TRUE if available, FALSE if not available
|
||||
*/
|
||||
public static function is_order_project_available($project) {
|
||||
$project_id = is_numeric($project) ? $project : $project->term_id;
|
||||
return get_term_meta($project_id, '_wiaas_is_available', true) === 'yes';
|
||||
}
|
||||
|
||||
// PRIVATE
|
||||
|
||||
/**
|
||||
* @param int $id Order project id
|
||||
* @param bool $is_available
|
||||
*/
|
||||
private static function _save_available_field($id, $is_available) {
|
||||
update_term_meta($id, '_wiaas_is_available', $is_available ? 'yes' : 'no');
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Order_Project::init();
|
||||
@@ -34,10 +34,44 @@ class Wiaas_Customer {
|
||||
return get_user_meta($customer_id, 'profile_addresses', true) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customer profile address by id
|
||||
*
|
||||
* @param int $customer_id
|
||||
* @param int $address_id
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public static function get_customer_profile_address($customer_id, $address_id) {
|
||||
$profile_addresses = self::get_customer_profile_addresses($customer_id);
|
||||
$index = array_search($address_id, array_column($profile_addresses, 'id'));
|
||||
if (is_numeric($index)) {
|
||||
return $profile_addresses[$index];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function get_customer_billing_addresses($customer_id){
|
||||
return get_user_meta($customer_id, 'billing_addresses', true) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customer billing address by id
|
||||
*
|
||||
* @param int $customer_id
|
||||
* @param int $address_id
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public static function get_customer_billing_address($customer_id, $address_id) {
|
||||
$billing_addresses = self::get_customer_billing_addresses($customer_id);
|
||||
$index = array_search($address_id, array_column($billing_addresses, 'id'));
|
||||
if (is_numeric($index)) {
|
||||
return $billing_addresses[$index];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function get_customer_vat_code($customer_id){
|
||||
return get_user_meta($customer_id, 'vat_code', true) ?: '';
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ require $_tests_dir . '/includes/bootstrap.php';
|
||||
|
||||
# Require Wiaas Unit Test case class
|
||||
require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/tests/wiaas-unit-test-case.php';
|
||||
require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/tests/wiaas-api-unit-test-case.php';
|
||||
|
||||
function load_active_plugins_list() {
|
||||
return array(
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Order_Project_API_Test extends Wiaas_API_Unit_Test_Case {
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order_Projects_API::get_order_projects()
|
||||
*/
|
||||
function test_get_order_projects_as_guest() {
|
||||
$this->check_endpoint_forbidden_for_guest(
|
||||
new WP_REST_Request( 'GET', '/wiaas/order-projects')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order_Projects_API::get_order_projects()
|
||||
*/
|
||||
function test_get_order_projects_as_customer() {
|
||||
Wiaas_Order_Project::add_order_project('Test Available 1', true);
|
||||
Wiaas_Order_Project::add_order_project('Test Available 2', true);
|
||||
|
||||
$customer_id = $this->create_new_customer();
|
||||
wp_set_current_user($customer_id);
|
||||
|
||||
$data = $this->dispatch_endpoint_request(
|
||||
new WP_REST_Request( 'GET', '/wiaas/order-projects')
|
||||
);
|
||||
$this->assertNotNull($data);
|
||||
$this->assertTrue(is_array($data));
|
||||
|
||||
$this->assertCount(2, $data);
|
||||
foreach ($data as $project) {
|
||||
$this->assertArrayHasKey('id', $project);
|
||||
$this->assertArrayHasKey('name', $project);
|
||||
|
||||
$this->assertContains($project['name'], array(
|
||||
'Test Available 1',
|
||||
'Test Available 2',
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order_Projects_API::create_order_project()
|
||||
*/
|
||||
function test_create_order_project_as_guest() {
|
||||
$request = new WP_REST_Request( 'POST', '/wiaas/order-projects');
|
||||
$request->set_body_params(array(
|
||||
'name' => 'Test'
|
||||
));
|
||||
$this->check_endpoint_forbidden_for_guest($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order_Projects_API::create_order_project()
|
||||
*/
|
||||
function test_create_valid_order_project_as_customer() {
|
||||
$customer_id = $this->create_new_customer();
|
||||
wp_set_current_user($customer_id);
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wiaas/order-projects');
|
||||
$request->set_body_params(array(
|
||||
'name' => 'Test Project'
|
||||
));
|
||||
$data = $this->dispatch_endpoint_request($request);
|
||||
|
||||
$this->assertNotNull($data);
|
||||
$this->assertTrue(is_array($data));
|
||||
|
||||
$this->assertArrayHasKey('messages', $data);
|
||||
$this->assertNotEmpty($data['messages']);
|
||||
|
||||
$message = $data['messages'][0];
|
||||
$this->assertArrayHasKey('code', $message);
|
||||
$this->assertEquals('success', $message['code']);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Order_Project_Test extends Wiaas_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order_Project::register_order_project_taxonomy()
|
||||
*/
|
||||
function test_order_project_taxonomy_created() {
|
||||
$taxonomy = get_taxonomy('shop_order_project');
|
||||
$this->assertInstanceOf(WP_Taxonomy::class, $taxonomy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order_Project::add_order_project()
|
||||
*/
|
||||
function test_adding_order_project() {
|
||||
Wiaas_Order_Project::add_order_project('Test', true);
|
||||
|
||||
$added_order_project = get_term_by('name', 'Test', 'shop_order_project');
|
||||
|
||||
$this->assertInstanceOf(WP_Term::class, $added_order_project);
|
||||
$this->assertEquals($added_order_project->name, 'Test');
|
||||
|
||||
$saved_available_flag = get_term_meta($added_order_project->term_id, '_wiaas_is_available', true);
|
||||
$this->assertEquals('yes', $saved_available_flag);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order_Project::is_order_project_available()
|
||||
*/
|
||||
function test_is_order_project_available() {
|
||||
Wiaas_Order_Project::add_order_project('Test', true);
|
||||
$added_order_project = get_term_by('name', 'Test', 'shop_order_project');
|
||||
|
||||
$this->assertTrue(Wiaas_Order_Project::is_order_project_available($added_order_project));
|
||||
|
||||
$this->assertTrue(Wiaas_Order_Project::is_order_project_available($added_order_project->term_id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order_Project::set_is_order_project_available()
|
||||
*/
|
||||
function test_set_is_order_project_available() {
|
||||
Wiaas_Order_Project::add_order_project('Test', true);
|
||||
$added_order_project = get_term_by('name', 'Test', 'shop_order_project');
|
||||
|
||||
Wiaas_Order_Project::set_is_order_project_available($added_order_project, false);
|
||||
$saved_available_flag = get_term_meta($added_order_project->term_id, '_wiaas_is_available', true);
|
||||
$this->assertEquals('no', $saved_available_flag);
|
||||
|
||||
Wiaas_Order_Project::set_is_order_project_available($added_order_project->term_id, true);
|
||||
$saved_available_flag = get_term_meta($added_order_project->term_id, '_wiaas_is_available', true);
|
||||
$this->assertEquals('yes', $saved_available_flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order_Project::get_available_order_projects()
|
||||
*/
|
||||
function test_get_available_order_projects() {
|
||||
Wiaas_Order_Project::add_order_project('Test Available 1', true);
|
||||
Wiaas_Order_Project::add_order_project('Test Available 2', true);
|
||||
Wiaas_Order_Project::add_order_project('Test Available 3', true);
|
||||
|
||||
Wiaas_Order_Project::add_order_project('Test Not Available 1', false);
|
||||
Wiaas_Order_Project::add_order_project('Test Not Available 2', false);
|
||||
Wiaas_Order_Project::add_order_project('Test Not Available 3', false);
|
||||
|
||||
$available_projects = Wiaas_Order_Project::get_available_order_projects();
|
||||
|
||||
$this->assertCount(3, $available_projects);
|
||||
|
||||
foreach ($available_projects as $project) {
|
||||
$this->assertArrayHasKey('id', $project);
|
||||
$this->assertArrayHasKey('name', $project);
|
||||
|
||||
$this->assertContains($project['name'], array(
|
||||
'Test Available 1',
|
||||
'Test Available 2',
|
||||
'Test Available 3'
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
63
backend/app/plugins/wiaas/tests/wiaas-api-unit-test-case.php
Normal file
63
backend/app/plugins/wiaas/tests/wiaas-api-unit-test-case.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Implements common logic for testing Wiaas API
|
||||
*
|
||||
* Class Wiaas_API_Unit_Test_Case
|
||||
*/
|
||||
class Wiaas_API_Unit_Test_Case extends Wiaas_Unit_Test_Case {
|
||||
protected $server;
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
/** @var WP_REST_Server $wp_rest_server */
|
||||
global $wp_rest_server;
|
||||
$this->server = $wp_rest_server = new \WP_REST_Server;
|
||||
do_action( 'rest_api_init' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that given endpoint is forbidden when accessed as guest
|
||||
*
|
||||
* @param WP_REST_Request $request Request to test
|
||||
*/
|
||||
function check_endpoint_forbidden_for_guest($request) {
|
||||
wp_set_current_user(0);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertNotNull($response);
|
||||
$this->assertInstanceOf('WP_REST_Response',$response);
|
||||
$this->assertTrue($response->is_error());
|
||||
$this->assertEquals($response->get_status(), 401);
|
||||
|
||||
$error_data = $response->as_error();
|
||||
$this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles common action during dispatching of rest request
|
||||
*
|
||||
* @param WP_REST_Request $request
|
||||
* @param int $expected_status_code
|
||||
* @param bool $is_error_expected
|
||||
*
|
||||
* @return mixed Result error when error is expected, result data otherwise
|
||||
*/
|
||||
function dispatch_endpoint_request($request, $expected_status_code = 200, $is_error_expected = false) {
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertNotNull($response);
|
||||
$this->assertInstanceOf('WP_REST_Response',$response);
|
||||
$this->assertEquals($response->get_status(), $expected_status_code);
|
||||
|
||||
if ($is_error_expected) {
|
||||
$this->assertTrue($response->is_error());
|
||||
return $response->as_error();
|
||||
}
|
||||
|
||||
$this->assertFalse($response->is_error());
|
||||
return $response->get_data();
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase {
|
||||
Wiaas_Product_Category::register_product_categories();
|
||||
|
||||
Wiaas_Package_Type::register_package_type_taxonomy();
|
||||
|
||||
Wiaas_Order_Project::register_order_project_taxonomy();
|
||||
|
||||
define('WP_TEST_IN_PROGRESS',true);
|
||||
}
|
||||
@@ -85,4 +87,28 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase {
|
||||
|
||||
$package->sync(true);
|
||||
}
|
||||
|
||||
function create_new_customer($login = 'customer_test', $organization_name = 'test-customer-organization') {
|
||||
$customer_id = wp_insert_user(array(
|
||||
'user_login' => $login,
|
||||
'user_pass' => 'test',
|
||||
'user_email' => $login . '@mail.com',
|
||||
'role' => 'customer',
|
||||
));
|
||||
|
||||
$organization = get_term_by('name', $organization_name, Wiaas_User_Organization::TAXONOMY_NAME);
|
||||
if (is_wp_error($organization)) {
|
||||
$organization = wp_insert_term(
|
||||
$organization_name,
|
||||
Wiaas_User_Organization::TAXONOMY_NAME
|
||||
);
|
||||
}
|
||||
|
||||
wp_set_terms_for_user(
|
||||
$customer_id,
|
||||
Wiaas_User_Organization::TAXONOMY_NAME,
|
||||
[$organization_name]);
|
||||
|
||||
return $customer_id;
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,8 @@ include_once WIAAS_DIR . '/includes/class-wiaas-documents.php';
|
||||
|
||||
include_once WIAAS_DIR . '/includes/class-wiaas-cart.php';
|
||||
|
||||
include_once WIAAS_DIR . '/includes/class-wiaas-checkout.php';
|
||||
|
||||
include_once WIAAS_DIR . '/includes/class-wiaas-api.php';
|
||||
|
||||
include_once WIAAS_DIR . '/includes/class-wiaas-admin.php';
|
||||
|
||||
@@ -29,6 +29,8 @@ import {
|
||||
cartMessages
|
||||
} from '../../constants/cartConstants';
|
||||
import {updateMessages} from '../notification/notificationActions';
|
||||
import { fromWCCartItems } from '../../helpers/CartHelper';
|
||||
|
||||
const client = new HtmlClient();
|
||||
|
||||
export const requestShopCartCount = () => ({type: REQUEST_SHOP_CART_COUNT});
|
||||
@@ -72,10 +74,13 @@ export const fetchCartItems = (isForSteps = false) => {
|
||||
return dispatch => {
|
||||
dispatch(requestShopCartItems());
|
||||
return client.fetch({url: `${API_SERVER}/wp-json/wiaas/cart/items`}).then(response => {
|
||||
if (typeof response.data !== 'undefined' && 'cartItems' in response.data) {
|
||||
dispatch(receiveShopCartItems(response.data.cartItems));
|
||||
dispatch(fetchCartDocuments(response.data.cartItems.map((cartItem) => cartItem.idPackage), isForSteps));
|
||||
dispatch(updateOrderTotalPrice(response.data.cartItems));
|
||||
if (typeof response.data !== 'undefined' && 'items' in response.data) {
|
||||
|
||||
const cartItems = response.data.items.map(wcCartItem => fromWCCartItems(wcCartItem));
|
||||
|
||||
dispatch(receiveShopCartItems(cartItems));
|
||||
dispatch(fetchCartDocuments(cartItems.map((cartItem) => cartItem.idPackage), isForSteps));
|
||||
dispatch(updateOrderTotalPrice(cartItems));
|
||||
}
|
||||
}).catch(error => {
|
||||
client.onError(error, dispatch);
|
||||
@@ -90,19 +95,14 @@ const updateCartItems = (newItem)=>(
|
||||
);
|
||||
|
||||
export const updateQuantity = (cartItem, quantity, updateCartAllItems) => {
|
||||
const params = {
|
||||
idPackage: cartItem.idPackage || 0,
|
||||
package_item_key: cartItem.key,
|
||||
idCustomerInstance: cartItem.idCustomerInstance || 0,
|
||||
idPrice: cartItem.idPrice || 0,
|
||||
quantity
|
||||
};
|
||||
|
||||
return dispatch => {
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/update-quantity`,
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/items/${cartItem.key}`,
|
||||
method: 'post',
|
||||
data: params
|
||||
data: {
|
||||
quantity
|
||||
}
|
||||
}).then(response => {
|
||||
if (typeof response.data !== 'undefined' && 'messages' in response.data) {
|
||||
const filteredMessages = response.data.messages.filter(message => {
|
||||
@@ -125,11 +125,8 @@ export const updateQuantity = (cartItem, quantity, updateCartAllItems) => {
|
||||
export const removeCartItem = (cartItemKey) => {
|
||||
return dispatch => {
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/remove`,
|
||||
method: 'post',
|
||||
data: {
|
||||
package_item_key: cartItemKey
|
||||
}
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/items/${cartItemKey}`,
|
||||
method: 'delete',
|
||||
}).then(response => {
|
||||
if (typeof response.data !== 'undefined' && 'messages' in response.data) {
|
||||
dispatch(updateMessages(response.data.messages, cartMessages));
|
||||
@@ -269,21 +266,30 @@ export const fetchCartDocuments = (packages, isForSteps = false) => {
|
||||
return dispatch => {
|
||||
dispatch(requestCartDocuments());
|
||||
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/documents`,
|
||||
method: 'get',
|
||||
data: {packages}
|
||||
}).then(response => {
|
||||
if (response.data) {
|
||||
dispatch(receiveCartDocuments(response.data));
|
||||
if(isForSteps) {
|
||||
const whitoutUploadDoc = response.data.templates.length === 0;
|
||||
dispatch(loadSteps(whitoutUploadDoc));
|
||||
}
|
||||
}
|
||||
}).catch(error => {
|
||||
client.onError(error, dispatch);
|
||||
});
|
||||
dispatch(receiveCartDocuments({
|
||||
areFilesUploaded: true,
|
||||
templates: [],
|
||||
uploaded: []
|
||||
}));
|
||||
if(isForSteps) {
|
||||
dispatch(loadSteps(true));
|
||||
}
|
||||
|
||||
// return client.fetch({
|
||||
// url: `${API_SERVER}/wp-json/wiaas/cart/documents`,
|
||||
// method: 'get',
|
||||
// data: {packages}
|
||||
// }).then(response => {
|
||||
// if (response.data) {
|
||||
// dispatch(receiveCartDocuments(response.data));
|
||||
// if(isForSteps) {
|
||||
// const whitoutUploadDoc = response.data.templates.length === 0;
|
||||
// dispatch(loadSteps(whitoutUploadDoc));
|
||||
// }
|
||||
// }
|
||||
// }).catch(error => {
|
||||
// client.onError(error, dispatch);
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,9 +325,17 @@ export const placeOrder = (orderInfo) => {
|
||||
const {orderDetails, cartItems} = orderInfo;
|
||||
return dispatch => {
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/place-order`,
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/checkout`,
|
||||
method: 'post',
|
||||
data: orderDetails
|
||||
data: {
|
||||
vat: orderDetails.vatCode,
|
||||
company: orderDetails.companyName,
|
||||
reference: orderDetails.details.reference,
|
||||
tender: orderDetails.details.tender,
|
||||
project_id: orderDetails.details.idProject,
|
||||
delivery_address_id: orderDetails.delivery.id,
|
||||
billing_address_id: orderDetails.billing.id,
|
||||
}
|
||||
}).then(response => {
|
||||
if (typeof response.data !== 'undefined' && 'messages' in response.data) {
|
||||
dispatch(updateMessages(response.data.messages, cartMessages));
|
||||
|
||||
@@ -111,13 +111,6 @@ export const addToCart = (addParams) => {
|
||||
addParams.selectedAdditionals,
|
||||
addParams.selectedAgreement);
|
||||
|
||||
const params = {
|
||||
'package_id': addParams.selectedPackage.id,
|
||||
'price_id': addParams.selectedAgreement.idPrice,
|
||||
'addons': result.additionalPackages,
|
||||
'options': result.optionPackages
|
||||
};
|
||||
|
||||
if(result.unavailablePackages.length){
|
||||
const unavailable = result.unavailablePackages.map((unavailable) =>{return unavailable.optionName || unavailable.packageName;});
|
||||
const message = coMarketMessages.UNAVAILABLE_PACKAGES + ' ' + unavailable.join();
|
||||
@@ -128,9 +121,14 @@ export const addToCart = (addParams) => {
|
||||
return dispatch => {
|
||||
dispatch(requestAddToCart());
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/add`,
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/items`,
|
||||
method: 'post',
|
||||
data: params
|
||||
data: {
|
||||
'package_id': addParams.selectedPackage.id,
|
||||
'price_id': addParams.selectedAgreement.idPrice,
|
||||
'addons_ids': result.additionalPackages,
|
||||
'options_ids': result.optionPackages
|
||||
},
|
||||
})
|
||||
.then(response => {
|
||||
if(response.data && response.data.messages){
|
||||
|
||||
@@ -23,8 +23,8 @@ const recieveOrderProjects = (json) => ({
|
||||
|
||||
const generateOptions = (orderProjects) => {
|
||||
orderProjects.forEach((orderProject) => {
|
||||
orderProject.value = orderProject.idProject;
|
||||
orderProject.label = orderProject.projectName;
|
||||
orderProject.value = orderProject.id;
|
||||
orderProject.label = orderProject.name;
|
||||
});
|
||||
|
||||
return orderProjects;
|
||||
@@ -34,40 +34,18 @@ export const getOrderProjects = () => {
|
||||
return dispatch => {
|
||||
dispatch(requestOrderProjects());
|
||||
|
||||
dispatch(recieveOrderProjects(generateOptions([
|
||||
{
|
||||
"idProject": 1,
|
||||
"projectName": "Innovation Center",
|
||||
"isAvailable": 1
|
||||
},
|
||||
{
|
||||
"idProject": 2,
|
||||
"projectName": "Demo01",
|
||||
"isAvailable": 1
|
||||
},
|
||||
{
|
||||
"idProject": 3,
|
||||
"projectName": "Kontorsrådet",
|
||||
"isAvailable": 1
|
||||
},
|
||||
{
|
||||
"idProject": 4,
|
||||
"projectName": "PerProj01",
|
||||
"isAvailable": 1
|
||||
}
|
||||
])));
|
||||
// return htmlClient.fetch({
|
||||
// url: `${API_SERVER}/orderProjects/api/getOrderProjects`
|
||||
// })
|
||||
// .then(response => {
|
||||
// if (response.data) {
|
||||
// const orderProjects = generateOptions(response.data);
|
||||
// dispatch(recieveOrderProjects(orderProjects));
|
||||
// }
|
||||
// })
|
||||
// .catch(error => {
|
||||
// htmlClient.onError(error, dispatch);
|
||||
// });
|
||||
return htmlClient.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/order-projects`
|
||||
})
|
||||
.then(response => {
|
||||
if (response.data) {
|
||||
const orderProjects = generateOptions(response.data);
|
||||
dispatch(recieveOrderProjects(orderProjects));
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
htmlClient.onError(error, dispatch);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,13 +54,15 @@ const requestAddProject = () => ({
|
||||
isLoading: true
|
||||
});
|
||||
|
||||
export const addProject = (projectData) => {
|
||||
export const addProject = (projectName) => {
|
||||
return dispatch => {
|
||||
dispatch(requestAddProject());
|
||||
return htmlClient.fetch({
|
||||
url: `${API_SERVER}/orderProjects/api/addOrderProject`,
|
||||
url: `${API_SERVER}/wp-json/wiaas/order-projects`,
|
||||
method: 'post',
|
||||
data: {projectData: JSON.stringify(projectData)}
|
||||
data: {
|
||||
name: projectName
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
if(response.data && response.data.messages){
|
||||
|
||||
@@ -87,7 +87,7 @@ class CartCustomerDetailsContainer extends Component {
|
||||
|
||||
handleProjectChange(project){
|
||||
const newDetails = this.state.details;
|
||||
newDetails.idProject = project ? project.idProject : null;
|
||||
newDetails.idProject = project ? project.id : null;
|
||||
return this.setState({details: newDetails});
|
||||
}
|
||||
|
||||
|
||||
@@ -37,10 +37,10 @@ class CartReviewOrderContainer extends Component {
|
||||
|
||||
getProjectName(idProject){
|
||||
const selectedProject = this.props.orderProjects.find(orderProject => {
|
||||
return orderProject.idProject === idProject
|
||||
return orderProject.id === idProject
|
||||
});
|
||||
|
||||
return selectedProject && selectedProject.projectName ? selectedProject.projectName : '-';
|
||||
return selectedProject && selectedProject.name ? selectedProject.name : '-';
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
@@ -23,7 +23,7 @@ class AddOrderProject extends Component {
|
||||
}
|
||||
|
||||
onProjectAdd(){
|
||||
this.props.dispatch(addProject(this.state));
|
||||
this.props.dispatch(addProject(this.state.projectName));
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
55
frontend/src/helpers/CartHelper.js
Normal file
55
frontend/src/helpers/CartHelper.js
Normal file
@@ -0,0 +1,55 @@
|
||||
|
||||
export const fromWCCartItems = wcCartItem => {
|
||||
const paymentInfo = wcCartItem['payment_info'] || {};
|
||||
const totalPrices = wcCartItem['total_prices'] || {};
|
||||
|
||||
return {
|
||||
idPackage: wcCartItem['package_id'],
|
||||
key: wcCartItem.key,
|
||||
packageName: wcCartItem['package_name'],
|
||||
quantity: wcCartItem.quantity,
|
||||
|
||||
idCommercialLead: wcCartItem['commercial_lead_id'],
|
||||
commercialLead: wcCartItem['commercial_lead'],
|
||||
country: wcCartItem.country ? wcCartItem.country : [],
|
||||
|
||||
areAdditionalAvailable: wcCartItem['are_additional_available'],
|
||||
additionalPackages: wcCartItem['additional_packages'] ? wcCartItem['additional_packages'].map(additionalPackageItem => ({
|
||||
idAdditionalPackage: additionalPackageItem['package_id'],
|
||||
packageName: additionalPackageItem['package_name'],
|
||||
prices: {
|
||||
fixedExtra: additionalPackageItem['prices'] ? additionalPackageItem['prices']['fixed_extra'] : 0,
|
||||
recurrentExtra: additionalPackageItem['prices'] ? additionalPackageItem['prices']['recurrent_extra'] : 0,
|
||||
servicesExtra: additionalPackageItem['prices'] ? additionalPackageItem['prices']['services_extra'] : 0,
|
||||
}
|
||||
})) : [],
|
||||
|
||||
areOptionsAvailable: wcCartItem['are_options_available'],
|
||||
options: wcCartItem.options ? wcCartItem.options.map(optionPackageItem => ({
|
||||
idOptionPackage: optionPackageItem['package_id'],
|
||||
packageName: optionPackageItem['package_name'],
|
||||
groupName: optionPackageItem['group_name'],
|
||||
prices: {
|
||||
fixedExtra: optionPackageItem['prices'] ? optionPackageItem['prices']['fixed_extra'] : 0,
|
||||
recurrentExtra: optionPackageItem['prices'] ? optionPackageItem['prices']['recurrent_extra'] : 0,
|
||||
servicesExtra: optionPackageItem['prices'] ? optionPackageItem['prices']['services_extra'] : 0,
|
||||
}
|
||||
})) : [],
|
||||
|
||||
bids: wcCartItem.bids,
|
||||
|
||||
idPayType: paymentInfo.id,
|
||||
payType: paymentInfo.type,
|
||||
periodUnit: paymentInfo['period_unit'] || 'month',
|
||||
idPrice: paymentInfo.id,
|
||||
fixedPrice: paymentInfo['fixed_extra'] !== undefined ? paymentInfo['fixed_extra'] : 0,
|
||||
recurrentPrice: paymentInfo['recurrent_extra'] !== undefined ? paymentInfo['recurrent_extra'] : 0,
|
||||
servicesPrice: paymentInfo['services_extra'] !== undefined ? paymentInfo['services_extra'] : 0,
|
||||
|
||||
totalPrices: {
|
||||
fixedPrice: totalPrices['fixed_extra'] !== undefined ? totalPrices['fixed_extra'] : 0,
|
||||
recurrentPrice: totalPrices['recurrent_extra'] !== undefined ? totalPrices['recurrent_extra'] : 0,
|
||||
servicesPrice: totalPrices['services_extra'] !== undefined ? totalPrices['services_extra'] : 0,
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -60,6 +60,24 @@ class HtmlClient {
|
||||
}
|
||||
|
||||
onError(error) {
|
||||
const response = error.response;
|
||||
if (response.data) {
|
||||
switch (response.data.code) {
|
||||
case 'rest_missing_callback_param':
|
||||
store.dispatch(updateMessages([{code:'error', message: response.data.message }]));
|
||||
return;
|
||||
case 'rest_invalid_param':
|
||||
const messages = Object.keys(response.data.data.params).map(paramName => {
|
||||
const errorMessage = response.data.data.params[paramName];
|
||||
if (errorMessage === 'Invalid parameter.') {
|
||||
return { code: 'error', message: `Invalid ${paramName} parameter!` };
|
||||
}
|
||||
return { code: 'error', message: errorMessage };
|
||||
});
|
||||
store.dispatch(updateMessages(messages));
|
||||
return;
|
||||
}
|
||||
}
|
||||
store.dispatch(updateMessages([{code:'error', message: 'HTML_ERROR'}], notificationMessages));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,11 @@ export const fromWCOrder = (WCOrder) => {
|
||||
dateCreated: formatDate(WCOrder['date_created']),
|
||||
dateCompleted: formatDate(WCOrder['date_completed']),
|
||||
estimatedDeliveryDate: undefined,
|
||||
vatCode: WCOrder['vat'],
|
||||
reference: WCOrder['reference'],
|
||||
tender: WCOrder['tender'],
|
||||
companyName: WCOrder['company_name'],
|
||||
projectName: WCOrder['project_name'],
|
||||
assignedTo: 'assigned to',
|
||||
fixedPrice: WCOrder.total,
|
||||
recurringPrice: WCOrder['recurring_price'],
|
||||
|
||||
Reference in New Issue
Block a user