Files
WooBC/woo-business-central/uninstall.php
Malin 2c36344932 feat: WooCommerce-first sync + location code filter
Reversed the product sync direction: instead of pulling all 60k+ items
from BC and matching against WooCommerce (600+ paginated API calls that
timeout), now iterates the ~100 WooCommerce products and queries BC for
each one by GTIN/item number (1-2 API calls per product).

Added Location Code setting (e.g. "ICP") to filter stock by BC location.
Uses Item Ledger Entries endpoint to sum per-location stock. Falls back
to total inventory if the endpoint is unavailable.

Also registered wbc_location_code in sync settings group and uninstall.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 10:26:02 +01:00

77 lines
2.2 KiB
PHP

<?php
/**
* Uninstall WooCommerce Business Central Integration
*
* @package WooBusinessCentral
*/
// Exit if uninstall not called from WordPress
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
// Check if user wants to delete data (optional, could be a setting)
$delete_data = get_option( 'wbc_delete_data_on_uninstall', 'no' );
// Always delete these options
$options_to_delete = array(
'wbc_tenant_id',
'wbc_client_id',
'wbc_client_secret',
'wbc_environment',
'wbc_company_id',
'wbc_sync_frequency',
'wbc_enable_stock_sync',
'wbc_enable_price_sync',
'wbc_enable_order_sync',
'wbc_default_payment_terms_id',
'wbc_default_shipment_method_id',
'wbc_shipping_item_number',
'wbc_location_code',
'wbc_last_sync_time',
'wbc_encryption_key',
'wbc_delete_data_on_uninstall',
);
foreach ( $options_to_delete as $option ) {
delete_option( $option );
}
// Delete transients
delete_transient( 'wbc_access_token' );
// Clear scheduled events
$sync_timestamp = wp_next_scheduled( 'wbc_product_sync_event' );
if ( $sync_timestamp ) {
wp_unschedule_event( $sync_timestamp, 'wbc_product_sync_event' );
}
$cleanup_timestamp = wp_next_scheduled( 'wbc_log_cleanup_event' );
if ( $cleanup_timestamp ) {
wp_unschedule_event( $cleanup_timestamp, 'wbc_log_cleanup_event' );
}
// Drop logs table
global $wpdb;
$table_name = $wpdb->prefix . 'wbc_logs';
$wpdb->query( "DROP TABLE IF EXISTS $table_name" );
// Delete user meta (BC customer IDs)
$wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key IN ('_wbc_bc_customer_id', '_wbc_bc_customer_number')" );
// Delete post meta (BC item info, order sync info)
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_wbc_%'" );
// Delete order meta for HPOS (if using custom order tables)
if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) ) {
if ( Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) {
$orders_table = $wpdb->prefix . 'wc_orders_meta';
if ( $wpdb->get_var( "SHOW TABLES LIKE '$orders_table'" ) === $orders_table ) {
$wpdb->query( "DELETE FROM $orders_table WHERE meta_key LIKE '_wbc_%'" );
}
}
}
// Clear any cached data
wp_cache_flush();