Files
piratepoet/mailpoet/tests/_support/woo_cot_helper_plugin.php
2023-12-13 12:41:06 +01:00

56 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php declare(strict_types = 1);
/*
Plugin Name: MailPoet Woo COT Helper
Description: Adds functionality for testing WooCommerce COT feature
Author: MailPoet
Version: 1.0
*/
use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
use Automattic\WooCommerce\Internal\Features\FeaturesController;
/**
* Enable WooCommerce COT tables
* @see https://github.com/Automattic/woocommerce/wiki/COT-Upgrade-Recipe-Book#easy-way
*/
function mailpoet_enable_cot(): void {
if (!function_exists('wc_get_container')) {
return;
}
/** @var FeaturesController $featuresController */
$featuresController = wc_get_container()->get(FeaturesController::class);
if ($featuresController instanceof FeaturesController) {
$featuresController->change_feature_enable('custom_order_tables', true);
}
}
add_action( 'init', 'mailpoet_enable_cot', 99 );
/**
* Add wp create_cot WP CLI command for creating Custom Order Tables from command line
*/
function mailpoet_create_cot() {
if (!function_exists('wc_get_container')) {
WP_CLI::error('Cant create COT. WooCommerce is not active!');
}
try {
/** @var DataSynchronizer $dataSynchronizer */
$dataSynchronizer = wc_get_container()->get(DataSynchronizer::class);
} catch (\Exception $e) {
WP_CLI::error('DataSynchronizer for COT not found. Does installed WooCommerce version support COT? ' . $e->getMessage());
}
$dataSynchronizer->create_database_tables();
WP_CLI::success('Database tables for COT feature created.');
}
if (class_exists(WP_CLI::class)) {
WP_CLI::add_command('create_cot', 'mailpoet_create_cot');
}
// Related PR in WooCommerce: https://github.com/woocommerce/woocommerce/pull/39988
// Sometimes during tests can happen that orders are out of sync. This state can trigger and exception
// The following filter avoids this state
add_filter('wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true');