Skip to main content

Getting Started - Configuration

The framework provides a unified interface for managing application settings and configurations using the Option facade. This ensures that all settings are properly namespaced within the WordPress options table.

Option Management​

The Option service allows you to store, retrieve, and delete configuration values.

Set Option​

Store a configuration value in the database.

Description:

The Option::set() method saves a value to the WordPress options table. It automatically handles key namespacing (prefixing with droip_ecommerce_) to prevent conflicts with other plugins or the core WordPress installation. You can store any serializable data type (strings, arrays, objects).

Method Signature:

Option::set(string $name, mixed $value): bool

Parameters:

  • $name - The configuration key (automatically prefixed).
  • $value - The value to store.

Examples:

use Ecommerce\Core\Facades\Option;

// Set a simple setting
Option::set('currency', 'USD');

// Set a configuration array
Option::set('payment_gateways', [
'stripe' => ['enabled' => true],
'paypal' => ['enabled' => false]
]);

// Update an existing setting
Option::set('currency', 'EUR');

SQL Output:

-- Initial insert
INSERT INTO `wp_options` (`option_name`, `option_value`, `autoload`)
VALUES ('droip_ecommerce_currency', 'USD', 'yes');

-- Update existing
UPDATE `wp_options`
SET `option_value` = 'EUR'
WHERE `option_name` = 'droip_ecommerce_currency';

Get Option​

Retrieve a configuration value.

Description:

The Option::get() method retrieves a value from the database. If the option does not exist, you can provide a default value to be returned instead. This method is cached by WordPress, ensuring high performance for frequently accessed settings.

Method Signature:

Option::get(string $name, mixed $default = null): mixed

Parameters:

  • $name - The configuration key to retrieve.
  • $default - (Optional) The default value to return if the option is missing.

Examples:

use Ecommerce\Core\Facades\Option;

// Get a value (returns null if missing)
$currency = Option::get('currency');

// Get with a default value
$currency = Option::get('currency', 'USD');

// Get nested configuration (requires logic handling)
$gateways = Option::get('payment_gateways', []);
if ($gateways['stripe']['enabled']) {
// ...
}

SQL Output:

-- Standard WordPress select (cached on subsequent calls)
SELECT `option_value` FROM `wp_options`
WHERE `option_name` = 'droip_ecommerce_currency'
LIMIT 1;

Delete Option​

Remove a configuration setting.

Description:

The Option::delete() method removes a configuration option from the database. It is useful for cleaning up settings when a module is disabled or during uninstallation.

Method Signature:

Option::delete(string $name): bool

Parameters:

  • $name - The configuration key to delete.

Examples:

use Ecommerce\Core\Facades\Option;

// Delete a setting
Option::delete('currency');

// Clean up feature flags
if ($featureDisabled) {
Option::delete('feature_flag_experimental_ui');
}

SQL Output:

DELETE FROM `wp_options`
WHERE `option_name` = 'droip_ecommerce_currency';