mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/79545194/add-a-custom-field-to-woocommerce-checkout-blocks-and-get-the-value/79545713
24 lines
728 B
Text
24 lines
728 B
Text
// Add a custom checkout field (checkout blocks)
|
|
add_action(
|
|
'woocommerce_init',
|
|
function() {
|
|
woocommerce_register_additional_checkout_field( array(
|
|
'id' => 'namespace/newsletter-opt-in',
|
|
'label' => 'Subscribe to newsletter?',
|
|
'location' => 'order',
|
|
'type' => 'checkbox',
|
|
) );
|
|
}
|
|
);
|
|
|
|
// Save the custom checkout field value (when the checkbox ix checked)
|
|
add_action(
|
|
'woocommerce_set_additional_field_value',
|
|
function ( $key, $value, $group, $wc_object ) {
|
|
if ( 'namespace/newsletter-opt-in' === $key ) {
|
|
$wc_object->update_meta_data( 'newsletter_opt_in', $value );
|
|
}
|
|
},
|
|
10,
|
|
4
|
|
);
|