Updating Woocommerce Settings API when WordPress Settings API saved and vise versa

Working on a woocommerce gateway and i am trying to make it in such a way that when users saved changes on the woocommerce payment gateway settings page, some values are updated in the admin setting page and if User saves the Admin settings page, it should update the woocommerce payment gateways settings.

I have been able to do it one-way but its not working the other way round, if i save the changes from woocommerce settings API page for the gateways, it updates on the Admin Settings API page. But if i try to save setting from the Admin Settings API page, it doesn’t save as the options from the woocommerce setting page will always override the new settings.

INSTANCE:
I saved the following options on WooCommerce Payment settings page;

‘Test Mode’ = ‘Yes’;
‘secret Key’ = ‘theSecretKey’;
‘public Key’ = ‘thePublicKey’;

When i check the Admin Settings page, the necessary fields are auto-populated with the options;

‘Test Mode’ = ‘Yes’;
‘secret Key’ = ‘theSecretKey’;
‘public Key’ = ‘thePublicKey’;

Now when i try to save a new values which from the Admin settings Page (which is to reflect on the woocommerce gateway page) with the following options;

‘Test Mode’ = ‘No’;
‘secret Key’ = ‘theSecretKeyNEW’;
‘public Key’ = ‘thePublicKeyNEW’;

And i hit save changes, it doesn’t save or update the woocommerce options; Instead it will reset to what we set from the woocommerce payment settings page;

‘Test Mode’ = ‘Yes’;
‘secret Key’ = ‘theSecretKey’;
‘public Key’ = ‘thePublicKey’;

MY CODE

if ( get_option(‘woocommerce_gateway_options’)[‘test_mode’] == ‘yes’ && get_option(‘admin_setting_options’)[‘test_mode’] == ‘no’ ) {
$update_admin_options = get_option(‘admin_setting_options’);
$update_admin_options[‘test_mode’] = ‘yes’;
update_option( ‘admin_setting_options’, $update_admin_options );
}elseif(get_option(‘woocommerce_gateway_options’)[‘test_mode’] == ‘no’ && get_option(‘admin_setting_options’)[‘test_mode’] == ‘yes’) {
$update_admin_options = get_option(‘aiops_settings_options’);
$update_admin_options[‘test_mode’] = ‘active’;
update_option( ‘admin_setting_options’, $update_admin_options );
}

if ( isset(get_option(‘woocommerce_gateway_options’)[‘secret_key’]) || isset(get_option(‘woocommerce_gateway_options’)[‘public_test’]) ) {
$update_admin_options = get_option(‘admin_setting_options’);

$update_admin_options[‘secret_key’] = get_option(‘woocommerce_gateway_options’)[‘secret_key’];
$update_admin_options[‘public_test’] = get_option(‘woocommerce_gateway_options’)[‘public_test’];

update_option( ‘admin_setting_options’, $update_admin_options );

}

/* The Above Code works and updates the Admin settings
* when the payment option page is updated.
* NOW, when i add the below code to vise versa the saving, it doesn’t work
*/

if ( isset(get_option(‘admin_setting_options’)[‘secret_key’]) || isset(get_option(‘admin_setting_options’)[‘public_test’]) ) {
$update_woocommerce_payment_options = get_option(‘woocommerce_gateway_options’);

$update_woocommerce_payment_options[‘secret_key’] = get_option(‘admin_setting_options’)[‘secret_key’];
$update_woocommerce_payment_options[‘public_test’] = get_option(‘admin_setting_options’)[‘public_test’];

update_option( ‘woocommerce_gateway_options’, $update_woocommerce_payment_options );

}

Not sure exactly what i’m doing wrong, as i’m not sure why it isn’t working. Thanks in advance for your help.

Read more here:: Updating Woocommerce Settings API when WordPress Settings API saved and vise versa

Leave a Reply

Your email address will not be published. Required fields are marked *