Skip to content

Instantly share code, notes, and snippets.

@jenkoian
Created August 14, 2019 12:38
Show Gist options
  • Select an option

  • Save jenkoian/4f8ea1bd88272c0cc5ae933e96df6d5f to your computer and use it in GitHub Desktop.

Select an option

Save jenkoian/4f8ea1bd88272c0cc5ae933e96df6d5f to your computer and use it in GitHub Desktop.

Revisions

  1. jenkoian revised this gist Aug 14, 2019. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions usage.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <?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' ] );
  2. jenkoian created this gist Aug 14, 2019.
    60 changes: 60 additions & 0 deletions class-wc-settings-overrider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    <?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' );
    }
    }
    }