Skip to content

Instantly share code, notes, and snippets.

@hereswhatidid
Created March 21, 2019 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hereswhatidid/1172309a783a8f22991bef5f9d212eab to your computer and use it in GitHub Desktop.
Save hereswhatidid/1172309a783a8f22991bef5f9d212eab to your computer and use it in GitHub Desktop.
WooCommerce custom settings page
<?php
// Initialize the settings page by including it via the `woocommerce_get_settings_pages` filter
add_filter( 'woocommerce_get_settings_pages', 'hwid_settings_page' );
function hwid_settings_page( $settings ) {
$settings[] = include( 'Settings.php' );
return $settings;
}
<?php
namespace RMG\QuoteBuilder;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( class_exists( 'RMG\QuoteBuilder\Settings_Quotes', false ) ) {
return new Settings_Quotes();
}
/**
* Settings_Quotes.
*/
class Settings_Quotes extends \WC_Settings_Page {
/**
* Constructor.
*/
public function __construct() {
$this->id = 'quotes';
$this->label = __( 'Quotes', 'rmgqb' );
parent::__construct();
}
/**
* Get sections.
*
* @return array
*/
public function get_sections() {
$sections = array(
'' => __( 'General', 'rmgqb' ),
'pages' => __( 'Pages', 'rmgqb' ),
'display' => __( 'Display Options', 'rmgqb' ),
);
return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
}
/**
* Output the settings.
*/
public function output() {
global $current_section;
$settings = $this->get_settings( $current_section );
\WC_Admin_Settings::output_fields( $settings );
}
/**
* Save settings.
*/
public function save() {
global $current_section;
$settings = $this->get_settings( $current_section );
\WC_Admin_Settings::save_fields( $settings );
if ( $current_section ) {
do_action( 'woocommerce_update_options_' . $this->id . '_' . $current_section );
}
}
/**
* Get settings array.
*
* @param string $current_section Current section name.
* @return array
*/
public function get_settings( $current_section = '' ) {
if ( 'pages' === $current_section ) {
$settings = apply_filters(
'woocommerce_quotes_pages_settings', [
'section_title' => [
'name' => __( 'Quote Pages', 'rmgqb' ),
'type' => 'title',
'desc' => '',
'id' => 'quote_options',
],
'quote_detail_page' => [
'title' => __( 'Quote details page', 'rmgqb' ),
'desc' => __( 'This the page that the [quotedetails] shortcode exists on.', 'rmgqb' ),
'id' => 'rmgqb_detail_page',
'type' => 'single_select_page',
'default' => '',
'class' => 'wc-enhanced-select-nostd',
'css' => 'min-width:300px;',
'desc_tip' => TRUE,
],
'section_end' => [
'type' => 'sectionend',
'id' => 'quote_options',
],
]
);
} elseif ( 'display' === $current_section ) {
$settings = apply_filters(
'woocommerce_quotes_display_settings', [
'disp_section_title' => [
'name' => __( 'Display Options', 'rmgqb' ),
'type' => 'title',
'desc' => '',
'id' => 'quote_options',
],
'quote_show_button' => [
'title' => __( 'View Quote Button', 'rmgqb' ),
'desc' => __( 'Display the `View Quote` button on the side of the site globally.', 'rmgqb' ),
'id' => 'rmgqb_show_quote_button',
'default' => 'no',
'type' => 'checkbox',
],
'disp_section_end' => [
'type' => 'sectionend',
'id' => 'quote_options',
],
]
);
} else {
$settings = apply_filters(
'woocommerce_quotes_settings', apply_filters(
'woocommerce_quotes_general_settings', [
[
'title' => __( 'General Settings', 'rmgqb' ),
'type' => 'title',
'desc' => '',
'id' => 'quote_options',
],
'quote_submit_form' => [
'name' => __( 'Quote Recipient', 'rmgqb' ),
'type' => 'text',
'desc_tip' => 'Defaults to the main site contact if left blank. Multiple recipients can be added by separating their addresses with a comma.',
'id' => 'rmg_quote_recipient',
],
'form_section_end' => [
'type' => 'sectionend',
'id' => 'wc_settings_tab_quotesforms_section_end',
],
[
'type' => 'sectionend',
'id' => 'quote_options',
],
]
)
);
}
return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, $current_section );
}
}
return new Settings_Quotes();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment