Skip to content

Instantly share code, notes, and snippets.

@ChaseWiseman
Created August 3, 2018 23:41
Show Gist options
  • Save ChaseWiseman/fe15e68b222654c5ccc6acad4c4128d0 to your computer and use it in GitHub Desktop.
Save ChaseWiseman/fe15e68b222654c5ccc6acad4c4128d0 to your computer and use it in GitHub Desktop.
WC Settings API Refactor
<?php
class WooCommerce {
public $settings;
public function init() {
// all that other fun init stuff
$this->settings = new \WooCommerce\Settings( 'woocommerce' );
}
// and the rest
}
<?php
namespace WooCommerce;
class Settings extends Settings_API {
/**
* Register every single setting that should be a part of this (WC core in this case).
*/
protected function register_settings() {
// address line 1
$this->register_setting( 'store_address', self::SETTING_TYPE_STRING, [
'name' => 'Address line 1',
'description' => 'The street address for your business location',
] );
$this->register_control( 'store_address', self::CONTROL_TYPE_TEXT ); // eventual input type
// address line 2
$this->register_setting( 'store_address_2', self::SETTING_TYPE_STRING, [
'name' => 'Address line 2',
'description' => 'An optional, additional address line for your business location',
] );
$this->register_control( 'store_address_2', self::CONTROL_TYPE_TEXT );
// selling location(s)
$this->register_setting( 'allowed_countries', self::SETTING_TYPE_STRING, [
'name' => 'Allowed Countries',
'description' => 'The countries to which you are willing to sell',
'options' => [ 'all', 'all_except', 'specific' ],
] );
$this->register_control( 'allowed_countries', self::CONTROL_TYPE_SELECT, [
'options' => [
'all' => 'Sell to all countries',
'all_except' => 'Sell to all countries, except for...',
'specific' => 'Sell to specific countries',
]
] );
// and so forth...
}
}
<?php
class \WC_Settings_General extends \WC_Settings_Page {
public function __construct() {
$this->id = 'general';
$this->label = __( 'General', 'woocommerce' );
parent::__construct();
}
// add sections, etc... as normal
/**
* Gets the broad groups of settings and their descriptions for display.
*
* Here we just define which settings should be displayed where. The Controls and the output methods in WC_Settings_Page take care of _how_ they're displayed.
*/
public function get_groups() {
return [
[
'id' => 'store-address',
'name' => 'Store Address',
'description' => 'This is where your business is located. Tax rates and shipping rates will use this address.',
'setting_ids' => [
'store_address',
'store_address_2',
],
],
[
'id' => 'general',
'name' => 'General',
'setting_ids' => [
'allowed_countries',
'specific_allowed_countries',
],
]
];
}
/** Methods responsible for rendering the controls */
protected function output_checkbox_control( $name, $value, $args = array() ) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment