Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

REST API - Cart Order API #1425

Merged
merged 29 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1b3a7c3
Checkout/order WIP schema
mikejolley Dec 20, 2019
182dec5
Add _address suffix for billing/shipping
mikejolley Jan 6, 2020
8a881a0
Rename schema, update endpoints, create tests
mikejolley Jan 6, 2020
a087357
Fix POST in test
mikejolley Jan 6, 2020
bfbcdfa
Fix test response checks
mikejolley Jan 6, 2020
9840d7b
Stock reservation and draft order status
mikejolley Jan 7, 2020
fa8c9c2
Add todo for shipping lines
mikejolley Jan 7, 2020
1804ad4
Readme
mikejolley Jan 7, 2020
8229881
Rename address fields in readme
mikejolley Jan 7, 2020
2bf5ac2
10 min timeout of stock
mikejolley Jan 7, 2020
a6fd544
Fix broken test
mikejolley Jan 8, 2020
46bbe70
Update src/RestApi/StoreApi/Controllers/CartOrder.php
mikejolley Jan 9, 2020
9cce54b
Add typehinting where possible
mikejolley Jan 9, 2020
7348af6
Remove explicit pass by reference
mikejolley Jan 9, 2020
a3fde19
Merge branch 'add/checkout-api' of https://github.com/woocommerce/woo…
mikejolley Jan 9, 2020
9548eea
Further typehinting
mikejolley Jan 9, 2020
c3f2836
Clarify todo comment
mikejolley Jan 9, 2020
0573ae8
Validate product instances
mikejolley Jan 9, 2020
b7263fb
Specific phpcs exclusion rule
mikejolley Jan 9, 2020
f2390a2
Exclusion rule
mikejolley Jan 9, 2020
ee616db
Move ReserveStock code to class
mikejolley Jan 9, 2020
a8a9379
Correct shipping-rates schema to shipping_rates
mikejolley Jan 9, 2020
ec62913
Save shipping rates and lines if included with request
mikejolley Jan 9, 2020
ad95bf3
Insert todo for shipping rate code
mikejolley Jan 9, 2020
75cd337
Calculate shipping and selected shipping from order properties, not g…
mikejolley Jan 9, 2020
aba04bf
Prevent error when shipping is not needed
mikejolley Jan 10, 2020
8e71d18
Update API readme
mikejolley Jan 10, 2020
1ed5865
Added tests for stock reserve class
mikejolley Jan 10, 2020
250a5ee
Fixes conflicts with draft statuses
mikejolley Jan 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,63 @@ class Library {
*/
public static function init() {
add_action( 'init', array( __CLASS__, 'register_blocks' ) );
add_action( 'init', array( __CLASS__, 'define_tables' ) );
add_action( 'init', array( __CLASS__, 'maybe_create_tables' ) );
add_filter( 'wc_order_statuses', array( __CLASS__, 'register_draft_order_status' ) );
add_filter( 'woocommerce_register_shop_order_post_statuses', array( __CLASS__, 'register_draft_order_post_status' ) );
}

/**
* Register custom tables within $wpdb object.
*/
public static function define_tables() {
global $wpdb;

// List of tables without prefixes.
$tables = array(
'wc_reserved_stock' => 'wc_reserved_stock',
);

foreach ( $tables as $name => $table ) {
$wpdb->$name = $wpdb->prefix . $table;
$wpdb->tables[] = $table;
}
}

/**
* Set up the database tables which the plugin needs to function.
*/
public static function maybe_create_tables() {
$db_version = get_option( 'wc_blocks_db_version', 0 );

if ( version_compare( $db_version, \Automattic\WooCommerce\Blocks\Package::get_version(), '>=' ) ) {
return;
}

global $wpdb;

require_once ABSPATH . 'wp-admin/includes/upgrade.php';

$wpdb->hide_errors();
$collate = '';

if ( $wpdb->has_cap( 'collation' ) ) {
$collate = $wpdb->get_charset_collate();
}

dbDelta(
"
CREATE TABLE {$wpdb->prefix}wc_reserved_stock (
`order_id` bigint(20) NOT NULL,
`product_id` bigint(20) NOT NULL,
`stock_quantity` double NOT NULL DEFAULT 0,
nerrad marked this conversation as resolved.
Show resolved Hide resolved
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`order_id`, `product_id`)
) $collate;
"
);

update_option( 'wc_blocks_db_version', \Automattic\WooCommerce\Blocks\Package::get_version() );
}

/**
Expand Down Expand Up @@ -59,4 +116,36 @@ public static function register_blocks() {
$instance->register_block_type();
}
}

/**
* Register custom order status for orders created via the API during checkout.
*
* Draft order status is used before payment is attempted, during checkout, when a cart is converted to an order.
*
* @param array $statuses Array of statuses.
* @return array
*/
public static function register_draft_order_status( array $statuses ) {
$statuses['wc-checkout-draft'] = _x( 'Draft', 'Order status', 'woo-gutenberg-products-block' );
return $statuses;
}

/**
* Register custom order post status for orders created via the API during checkout.
*
* @param array $statuses Array of statuses.
* @return array
*/
public static function register_draft_order_post_status( array $statuses ) {
$statuses['wc-checkout-draft'] = [
'label' => _x( 'Draft', 'Order status', 'woo-gutenberg-products-block' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => true,
/* translators: %s: number of orders */
'label_count' => _n_noop( 'Drafts <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>', 'woo-gutenberg-products-block' ),
];
return $statuses;
}
}
3 changes: 2 additions & 1 deletion src/RestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function get_routes_from_namespace( $namespace ) {
}

/**
* If we're making a cart request, we may need to load some additonal classes from WC Core so we're ready to deal with requests.
* If we're making a cart request, we may need to load some additional classes from WC Core so we're ready to deal with requests.
*
* Note: We load the session here early so guest nonces are in place.
*
Expand Down Expand Up @@ -96,6 +96,7 @@ protected static function get_controllers() {
'store-cart-items' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartItems',
'store-cart-coupons' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartCoupons',
'store-cart-shipping-rates' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartShippingRates',
'store-cart-order' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\CartOrder',
'store-customer' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\Customer',
'store-products' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\Products',
'store-product-collection-data' => __NAMESPACE__ . '\RestApi\StoreApi\Controllers\ProductCollectionData',
Expand Down
Loading