I'm creating a WooCommerce plugin setting page but I failed to get the product title as dropdown list, also the data is not saved anymore (I have tried to use an input field before).
Here is my code:
public function exclutips_create_license_page() {
// Set class property
$this->options = get_option('activation-codes-settings');
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Woocommerce License Manager Settings Page</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields('exclutips_option_group');
do_settings_sections('activation-codes-settings-admin');
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function exclutips_page_init() {
register_setting(
'exclutips_option_group', // Option group
'activation-codes-settings', // Option name
array($this, 'sanitize') // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'Register Product Settings', // Title
array($this, 'print_section_info'), // Callback
'activation-codes-settings-admin' // Page
);
add_settings_field(
'ex_product_ids', 'Product Id For License System', array($this, 'ex_product_ids_callback'), 'activation-codes-settings-admin', 'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*/
public function sanitize($input) {
$new_input = array();
if (isset($input['ex_product_ids']))
$new_input['ex_product_ids'] = absint($input['ex_product_ids']);
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info() {
print 'Please select the product ID for License Code:';
}
/**
* Get the settings option array and print one of its values
*/
public function ex_product_ids_callback() {
?>
<select name="ex_product_ids">
<option selected="selected" disabled="disabled" value=""><?php echo esc_attr( __( 'Select a Product' ) ); ?></option>
<?php
global $woocommerce;
$selected_product = get_option( 'ex_product_ids' );
$products = wc_get_product();
foreach ( $products as $product ) {
$option = '<option value="' . $product->id . '" ';
$option .= ( $product->id == $selected_product ) ? 'selected="selected"' : '';
$option .= '>';
$option .= $product->the_title();
$option .= '</option>';
echo $option;
}
?>
</select>
<?php
}