diff --git a/assets/js/admin/enhanced-settings-sync.js b/assets/js/admin/enhanced-settings-sync.js index 56adb5332..6644253bd 100644 --- a/assets/js/admin/enhanced-settings-sync.js +++ b/assets/js/admin/enhanced-settings-sync.js @@ -73,4 +73,37 @@ jQuery(document).ready(function($) { button.prop('disabled', false); }); }); + + /** + * Handle the sync shipping profiles button click event + * + * @since 3.5.0 + * + * @param {object} event + */ + $('#wc-facebook-enhanced-settings-sync-shipping-profiles').click(function(event) { + event.preventDefault(); + var button = $(this); + + button.html('Syncing...'); + button.prop('disabled', true); + + var data = { + action: "wc_facebook_sync_shipping_profiles", + nonce: wc_facebook_enhanced_settings_sync.sync_shipping_profiles_nonce + }; + + $.post(wc_facebook_enhanced_settings_sync.ajax_url, data, function(response) { + if (response.success) { + button.html('Sync completed'); + button.prop('disabled', false); + } else { + button.html('Sync failed'); + button.prop('disabled', false); + } + }).fail(function() { + button.html('Sync failed'); + button.prop('disabled', false); + }); + }); }); diff --git a/includes/AJAX.php b/includes/AJAX.php index 74c82cf5c..405987f5f 100644 --- a/includes/AJAX.php +++ b/includes/AJAX.php @@ -45,6 +45,9 @@ public function __construct() { // sync all coupons via AJAX add_action( 'wp_ajax_wc_facebook_sync_coupons', array( $this, 'sync_coupons' ) ); + // sync all shipping profiles via AJAX + add_action( 'wp_ajax_wc_facebook_sync_shipping_profiles', array( $this, 'sync_shipping_profiles' ) ); + // get the current sync status add_action( 'wp_ajax_wc_facebook_get_sync_status', array( $this, 'get_sync_status' ) ); @@ -117,13 +120,13 @@ public function sync_products() { } } - /** - * Syncs all coupons via AJAX. - * - * @internal - * - * @since 3.5.0 - */ + /** + * Syncs all coupons via AJAX. + * + * @internal + * + * @since 3.5.0 + */ public function sync_coupons() { check_admin_referer( Shops::ACTION_SYNC_COUPONS, 'nonce' ); @@ -135,6 +138,24 @@ public function sync_coupons() { } } + /** + * Syncs all shipping profiles via AJAX. + * + * @internal + * + * @since 3.5.0 + */ + public function sync_shipping_profiles() { + check_admin_referer( Shops::ACTION_SYNC_SHIPPING_PROFILES, 'nonce' ); + + try { + facebook_for_woocommerce()->feed_manager->get_feed_instance( 'shipping_profiles' )->regenerate_feed(); + wp_send_json_success(); + } catch ( \Exception $exception ) { + wp_send_json_error( $exception->getMessage() ); + } + } + /** * Gets the current sync status. diff --git a/includes/Admin/Settings_Screens/Shops.php b/includes/Admin/Settings_Screens/Shops.php index e26ce8950..4440b23ab 100644 --- a/includes/Admin/Settings_Screens/Shops.php +++ b/includes/Admin/Settings_Screens/Shops.php @@ -31,6 +31,9 @@ class Shops extends Abstract_Settings_Screen { /** @var string */ const ACTION_SYNC_COUPONS = 'wc_facebook_sync_coupons'; + /** @var string */ + const ACTION_SYNC_SHIPPING_PROFILES = 'wc_facebook_sync_shipping_profiles'; + /** * Shops constructor. * @@ -126,9 +129,10 @@ public function enqueue_assets() { 'wc-facebook-enhanced-settings-sync', 'wc_facebook_enhanced_settings_sync', array( - 'ajax_url' => admin_url( 'admin-ajax.php' ), - 'sync_products_nonce' => wp_create_nonce( self::ACTION_SYNC_PRODUCTS ), - 'sync_coupons_nonce' => wp_create_nonce( self::ACTION_SYNC_COUPONS ), + 'ajax_url' => admin_url( 'admin-ajax.php' ), + 'sync_products_nonce' => wp_create_nonce( self::ACTION_SYNC_PRODUCTS ), + 'sync_coupons_nonce' => wp_create_nonce( self::ACTION_SYNC_COUPONS ), + 'sync_shipping_profiles_nonce' => wp_create_nonce( self::ACTION_SYNC_SHIPPING_PROFILES ), ) ); } @@ -234,6 +238,22 @@ class="button"
++ Manually sync your shipping profiles from WooCommerce to your shop. It may take a couple of minutes for the changes to populate. +
+