Skip to content

Instantly share code, notes, and snippets.

@michaelbragg
Created January 21, 2022 15:46
Show Gist options
  • Save michaelbragg/e4ee40c31359bad484c0311493df3a05 to your computer and use it in GitHub Desktop.
Save michaelbragg/e4ee40c31359bad484c0311493df3a05 to your computer and use it in GitHub Desktop.
Example plugin to test proposed `woocommerce_form_field_value` filter
<?php
/**
* Plugin Name: WooCommerce Form Field Value Example
* Author: Michael Bragg <email@michaelbragg.com>
* Version: 1.0.0
*/
function custom_checkout_field( $checkout ) {
echo '<section id="example-field">';
// Echo the checkbox field with label text.
woocommerce_form_field( 'example_-field', array(
'type' => 'checkbox',
'class' => array( 'input-checkbox' ),
'label' => esc_html( 'Example of checkbox field.' ),
'required' => false,
), $checkout->get_value( 'example_field' ) );
echo '</section>';
}
add_action(
'woocommerce_after_order_notes',
'custom_checkout_field'
);
/**
* Test the filter is working.
*/
function custom_checkout_field_checked( $value, $key, $args ) {
if ( 'example_-field' !== $key ) {
return $value;
}
$value = 1;
return $value;
}
add_filter(
'woocommerce_form_field_value',
'custom_checkout_field_checked',
10,
3
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment