Created
August 14, 2019 12:38
-
-
Save jenkoian/4f8ea1bd88272c0cc5ae933e96df6d5f to your computer and use it in GitHub Desktop.
WooCommerce settings overrides.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* For overriding woocommerce settings. | |
*/ | |
declare( strict_types=1 ); | |
namespace Acme\Client\Mu\Plugins\Woocommerce\Overrides; | |
/** | |
* This class is to help override certain woocommerce settings. It allows an array of settings to override to be passed | |
* into the constructor which then can either be used to filter them out and/or to update their value. | |
*/ | |
class WC_Settings_Overrider { | |
/** | |
* An array of woocommerce settings to override. This should be in the format ['setting_name' => 'value_of_setting']. | |
* | |
* @var array | |
*/ | |
private $settings_to_override; | |
/** | |
* WC_Settings_Overrider constructor. | |
* | |
* @param array $settings_to_override Array of settings to override. | |
*/ | |
public function __construct( array $settings_to_override ) { | |
$this->settings_to_override = $settings_to_override; | |
} | |
/** | |
* Filter out any advanced settings we don't wish to display in the woocommerce settings page. | |
* | |
* @param array $settings Settings that are used for this filter. | |
* | |
* @return array | |
*/ | |
public function filter_settings( array $settings ): array { | |
$settings_to_remove = array_keys( $this->settings_to_override ); | |
return array_filter( | |
$settings, | |
function ( array $setting ) use ( $settings_to_remove ) : bool { | |
return ! \in_array( $setting['id'], $settings_to_remove, true ); | |
} | |
); | |
} | |
/** | |
* Update woocommerce options as per our hard coded config. | |
*/ | |
public function set_options(): void { | |
$options = $this->settings_to_override; | |
foreach ( $options as $name => $value ) { | |
update_option( $name, $value, 'no' ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Overriding woocommerce account settings | |
*/ | |
declare( strict_types=1 ); | |
namespace Acme\Client\Mu\Plugins\Woocommerce\Overrides; | |
const GENERAL_SETTINGS_TO_OVERRIDE = [ | |
'woocommerce_enable_coupons' => 'no', | |
]; | |
$wc_settings_overrider = new WC_Settings_Overrider( namespace\GENERAL_SETTINGS_TO_OVERRIDE ); | |
add_filter( 'woocommerce_general_settings', [ $wc_settings_overrider, 'filter_settings' ] ); | |
add_action( 'woocommerce_loaded', [ $wc_settings_overrider, 'set_options' ] ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment