Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Your own shop settings

A plugin quickly acquires parameters: whether to show the lead time, which lead time is the usual one, from what amount shipping is free. So that the shop owner can change them without a programmer, they are moved to the WooCommerce → Settings page. WooCommerce draws and saves the form itself — you only describe the fields with an array.

The code of this lesson goes into includes/admin.php.

A section on an existing tab

A section is a link under the tab name. Let us add an Acme section to the Products tab:

// wp-content/plugins/acme-shop/includes/admin.php
add_filter('woocommerce_get_sections_products', function (array $sections): array {
    $sections['acme'] = 'Acme';
 
    return $sections;
});

Open WooCommerce → Settings → Products: Acme has appeared next to the General and Inventory links. The filter name ends with the tab ID: products, shipping, account, advanced.

Section fields

// wp-content/plugins/acme-shop/includes/admin.php
add_filter('woocommerce_get_settings_products', function (array $settings, string $section): array {
    if ($section !== 'acme') {
        return $settings;
    }
 
    return [
        ['title' => 'Acme settings', 'type' => 'title', 'id' => 'acme_options'],
        [
            'title' => 'Show the lead time',
            'id' => 'acme_show_lead_time',
            'type' => 'checkbox',
            'default' => 'yes',
        ],
        [
            'title' => 'Default lead time, days',
            'id' => 'acme_default_lead_time',
            'type' => 'number',
            'default' => 3,
        ],
        ['type' => 'sectionend', 'id' => 'acme_options'],
    ];
}, 10, 2);

For other sections the filter returns the fields unchanged; for its own, a separate array. The list starts with a title element and ends with sectionend carrying the same id. Go to the Acme section: there is a checkbox and a number field, and the Save changes button already works.

Field types

typeField
text, number, email, passwordAn input line
textareaMulti-line text
checkboxA checkbox, stores yes or no
selectA list, choices in options
radioRadio buttons
single_select_pageA site page picker

The desc parameter adds an explanation under the field; desc_tip => true turns it into a question-mark tooltip.

Reading settings

Values live in the wp_options table under a key equal to the field id. Let us apply them to the lead time block:

// wp-content/plugins/acme-shop/includes/storefront.php
add_action('woocommerce_single_product_summary', function () {
    global $product;
 
    if (get_option('acme_show_lead_time', 'yes') !== 'yes') {
        return;
    }
 
    $days = (int) $product->get_meta('_acme_lead_time')
        ?: (int) get_option('acme_default_lead_time', 3);
 
    wc_get_template('single-product/lead-time.php', ['days' => $days], '', dirname(__DIR__) . '/templates/');
}, 25);

This version replaces the function from the lesson on templates. Untick the checkbox in the settings — the Made in… line disappears from product pages. A product without its own lead time gets the default one.

Your own tab

When there are many settings, they get a separate tab. The field array is moved into a function and used twice — for output and for saving:

// wp-content/plugins/acme-shop/includes/admin.php
function acme_workshop_settings(): array
{
    return [
        ['title' => 'Workshop', 'type' => 'title', 'id' => 'acme_workshop'],
        ['title' => 'Workshop email', 'id' => 'acme_workshop_email', 'type' => 'email'],
        ['type' => 'sectionend', 'id' => 'acme_workshop'],
    ];
}
 
add_filter('woocommerce_settings_tabs_array', function (array $tabs): array {
    $tabs['acme'] = 'Acme';
 
    return $tabs;
}, 50);
 
add_action('woocommerce_settings_acme', function () {
    woocommerce_admin_fields(acme_workshop_settings());
});
 
add_action('woocommerce_settings_save_acme', function () {
    woocommerce_update_options(acme_workshop_settings());
});

The filter adds the tab to the end of the list. The first action draws the fields, the second saves them when the button is pressed. An Acme tab with an email field appears on the settings page.

Common mistake

A checkbox value is tested as a boolean: if (get_option('acme_show_lead_time')). WooCommerce stores checkboxes as the strings yes and no, and the non-empty string no is truthy in PHP — the condition always passes. Compare with the string: === 'yes'.

Thanks for reading!

Donate to the site — we work hard for you.