In the past, I've added custom fields to my products in WooCommerce. This was only visible at the back and not directly visible on the front end.
Now I can't get it done in any way. When I work with plugins, this only works on the product page as a selection. But I would like to configure it on the product page in the back for example after the regular price field (see example).
I'm looking for the right hooks to do this. Anyone have an idea where I can find this?
add_action('woocommerce_product_options_pricing', 'add_custom_fields_to_poduct_general');
function add_custom_fields_to_poduct_general() {
global $woocommerce, $post;
woocommerce_wp_text_input(
array(
'id' => '_custom_text',
'value' => get_post_meta($post->ID, '_custom_text', true),
'label' => __('Custom text', 'woocommerce'),
)
);
}
Add more fields after price using the hook woocommerce_product_options_pricing
add_action('woocommerce_process_product_meta', 'save_custom_product_field');
function save_custom_product_field($post_id) {
$key = '_custom_text';
$value = (!empty($_POST[$key]) ? wc_clean(wp_unslash($_POST[$key])) : '' ); // phpcs:ignore WordPress.Security.NonceVerification
if ($value) {
update_post_meta($post_id, $key, $value);
} else {
delete_post_meta($post_id, $key);
}
}
The hook woocommerce_process_product_meta
is used to save the data added using the above hook
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.