Automattic\WooCommerce\Database\Migrations\CustomOrderTable
CLIRunner::enable
Set custom order tables (HPOS) to authoritative if: 1). HPOS and posts tables are in sync, or, 2). This is a new shop (in this case also create tables). Additionally, all installed WC plugins should be compatible.
OPTIONS
- [--for-new-shop]
- Enable only if this is a new shop, irrespective of whether tables are in sync.
--- default: false
--- - [--with-sync]
- Also enables sync (if it's currently not enabled).
--- default: false
--- - [--ignore-plugin-compatibility]
- Enable even if there are active plugins that are incompatible with HPOS.
EXAMPLES
# Enable HPOS on new shops. wp wc hpos enable --for-new-shop
Method of the class: CLIRunner{}
No Hooks.
Returns
void. Nothing (null).
Usage
$CLIRunner = new CLIRunner(); $CLIRunner->enable( $args, $assoc_args );
- $args(array)
- Positional arguments passed to the command.
Default:array() - $assoc_args(array)
- Associative arguments (options) passed to the command.
Default:array()
CLIRunner::enable() CLIRunner::enable code WC 11.2.2
public function enable( array $args = array(), array $assoc_args = array() ) {
$assoc_args = wp_parse_args(
$assoc_args,
array(
'for-new-shop' => false,
'with-sync' => false,
'ignore-plugin-compatibility' => false,
)
);
$enable_hpos = true;
WP_CLI::log( __( 'Running pre-enable checks...', 'woocommerce' ) );
$is_new_shop = \WC_Install::is_new_install();
if ( $assoc_args['for-new-shop'] && ! $is_new_shop ) {
WP_CLI::error( __( '[Failed] This is not a new shop, but --for-new-shop flag was passed.', 'woocommerce' ) );
}
$container = wc_get_container();
/** Feature controller instance @var FeaturesController $feature_controller */
$feature_controller = $container->get( FeaturesController::class );
if ( ! $assoc_args['ignore-plugin-compatibility'] ) {
$compatibility_info = $feature_controller->get_compatible_plugins_for_feature( 'custom_order_tables', true );
/** Plugin util instance @var PluginUtil $plugin_util */
$plugin_util = $container->get( PluginUtil::class );
$incompatibles = $plugin_util->get_items_considered_incompatible( 'custom_order_tables', $compatibility_info );
if ( count( $incompatibles ) > 0 ) {
WP_CLI::warning( __( '[Failed] Some installed plugins are incompatible. Please review the plugins by going to WooCommerce > Settings > Advanced > Features and see the "Order data storage" section.', 'woocommerce' ) );
$enable_hpos = false;
}
}
/** DataSynchronizer instance @var DataSynchronizer $data_synchronizer */
$data_synchronizer = wc_get_container()->get( DataSynchronizer::class );
$pending_orders = $data_synchronizer->get_total_pending_count();
$table_exists = $data_synchronizer->check_orders_table_exists();
if ( ! $table_exists ) {
WP_CLI::warning( __( 'Orders table does not exist. Creating...', 'woocommerce' ) );
if ( $is_new_shop || 0 === $pending_orders ) {
$data_synchronizer->create_database_tables();
if ( $data_synchronizer->check_orders_table_exists() ) {
WP_CLI::log( __( 'Orders table created.', 'woocommerce' ) );
$table_exists = true;
} else {
WP_CLI::warning( __( '[Failed] Orders table could not be created.', 'woocommerce' ) );
$enable_hpos = false;
}
} else {
WP_CLI::warning( __( '[Failed] The orders table does not exist and this is not a new shop. Please create the table by going to WooCommerce > Settings > Advanced > Features and enabling sync.', 'woocommerce' ) );
$enable_hpos = false;
}
}
if ( $pending_orders > 0 ) {
WP_CLI::warning(
sprintf(
// translators: %s is the command to run (wp wc cot sync).
__( '[Failed] There are orders pending sync. Please run `%s` to sync pending orders.', 'woocommerce' ),
'wp wc hpos sync',
)
);
$enable_hpos = false;
}
if ( $assoc_args['with-sync'] && $table_exists ) {
$this->toggle_compat_mode( true );
}
if ( ! $enable_hpos ) {
WP_CLI::error( __( 'HPOS pre-checks failed, please see the errors above', 'woocommerce' ) );
return;
}
/** CustomOrdersTableController instance @var CustomOrdersTableController $cot_status */
$cot_status = wc_get_container()->get( CustomOrdersTableController::class );
if ( $cot_status->custom_orders_table_usage_is_enabled() ) {
WP_CLI::warning( __( 'HPOS is already enabled.', 'woocommerce' ) );
} else {
$feature_controller->change_feature_enable( 'custom_order_tables', true );
if ( $cot_status->custom_orders_table_usage_is_enabled() ) {
WP_CLI::success( __( 'HPOS enabled.', 'woocommerce' ) );
} else {
WP_CLI::error( __( 'HPOS could not be enabled.', 'woocommerce' ) );
}
}
}