Чтобы добавить кастомное поле для товара, можно использовать хук woocommerce_product_options_general_product_data
. Пример:
<?php
function add_custom_product_field() {
woocommerce_wp_text_input( array(
'id' => '_custom_field',
'label' => __( 'Custom Field', 'woocommerce' ),
'description' => __( 'Enter the custom field here', 'woocommerce' ),
'desc_tip' => 'true',
'placeholder' => 'Custom Field',
) );
}
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_product_field' );
Для сохранения значения кастомного поля для товара в БД использовать хук woocommerce_process_product_meta
:
Пример:
function save_custom_product_field( $post_id ) {
$custom_field = !empty( $_POST['_custom_field'] ) ? sanitize_text_field( $_POST['_custom_field'] ) : '';
update_post_meta( $post_id, '_custom_field', $custom_field );
}
add_action( 'woocommerce_process_product_meta', 'save_custom_product_field' );
Для вывода кастомного поля на странице checkout можно использовать хук woocommerce_checkout_create_order_line_item
:
пример:
function add_custom_field_to_order_items( $item, $cart_item_key, $values, $order ) {
$custom_field = get_post_meta( $values['product_id'], '_custom_field', true );
if ( ! empty( $custom_field ) ) {
$item->add_meta_data( __( 'Custom Field', 'woocommerce' ), $custom_field );
}
}
add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_field_to_order_items', 10, 4 );