Custom WooCommerce Settings Page

Custom WooCommerce Settings Page

Getting your Trinity Audio player ready...
Step 2: Register Settings

To handle and save the settings, you need to register a settings group and fields.

add_action('admin_init', 'register_woocommerce_bulk_discount_settings');

function register_woocommerce_bulk_discount_settings() {
    // Register a setting for bulk discount values
    register_setting('wc_bulk_discount_settings_group', 'wc_bulk_discount_threshold');
    register_setting('wc_bulk_discount_settings_group', 'wc_bulk_discount_percentage');
    
    // Add a settings section
    add_settings_section(
        'wc_bulk_discount_settings_section', 
        'Bulk Discount Settings', 
        'bulk_discount_settings_section_callback', 
        'wc_bulk_discount_settings'
    );
    
    // Add settings fields
    add_settings_field(
        'wc_bulk_discount_threshold', 
        'Minimum Quantity for Discount', 
        'bulk_discount_threshold_callback', 
        'wc_bulk_discount_settings', 
        'wc_bulk_discount_settings_section'
    );
    
    add_settings_field(
        'wc_bulk_discount_percentage', 
        'Discount Percentage', 
        'bulk_discount_percentage_callback', 
        'wc_bulk_discount_settings', 
        'wc_bulk_discount_settings_section'
    );
}

// Callback for section description (optional)
function bulk_discount_settings_section_callback() {
    echo 'Set the minimum quantity and discount percentage for bulk purchases.';
}

// Callback for input field (Minimum Quantity)
function bulk_discount_threshold_callback() {
    $value = get_option('wc_bulk_discount_threshold', 10);
    echo '<input type="number" name="wc_bulk_discount_threshold" value="' . esc_attr($value) . '" />';
}

// Callback for input field (Discount Percentage)
function bulk_discount_percentage_callback() {
    $value = get_option('wc_bulk_discount_percentage', 10);
    echo '<input type="number" name="wc_bulk_discount_percentage" value="' . esc_attr($value) . '" />';
}

Similar Posts