Woocommerce: Добавление кастомного поля в вариативный продукт

if(!function_exists('wc_custom_product_data_fields')){

    // Display Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

    function woo_add_custom_general_fields() {
        global $woocommerce, $post;
        echo '<div class="options_group">';

        woocommerce_wp_text_input(
            array(
                'id'          => '_custom_field',
                'label'       => __( '_custom_field', 'woocommerce' ),
                'placeholder' => __( '', 'woocommerce' ),
                'class'       => 'wc_input_price short',
                'desc_tip'    => 'true',
                'description' => __( '_custom_field', 'woocommerce' ),
                //'type'              => 'number',
                'required' => 'required',
                'custom_attributes' => array()
            )
        );
        echo '</div>';
    }

    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
    function woo_add_custom_general_fields_save( $post_id ){
        $woocommerce_text_lpreis = $_POST['_custom_field']; //number_format($_POST['_custom_field'],2,'.','');
        if( !empty( $woocommerce_text_lpreis ) ){
            update_post_meta($post_id, '_custom_field', esc_attr( $woocommerce_text_lpreis) );
        }
    }

}



add_action( 'woocommerce_product_after_variable_attributes', 'add_to_variations_metabox', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'save_product_variation', 20, 2 );



function add_to_variations_metabox( $loop, $variation_data, $variation ){

    $custom = get_post_meta( $variation->ID, '_custom_field', true ); ?>

    <div class="variable_custom_field">
        <p class="form-row form-row-first">
            <label>_custom_field</label>
            <input type="text" name="variation_custom_data[<?php echo $loop; ?>]" value="<?php echo esc_attr( $custom ); ?>" />
        </p>
    </div>

    <?php

}


function save_product_variation( $variation_id, $i ){

    // save custom data
    if ( isset( $_POST['variation_custom_data'][$i] ) ) {
        // sanitize data in way that makes sense for your data type
        $custom_data = ( trim( $_POST['variation_custom_data'][$i]  ) === '' ) ? '' : $_POST['variation_custom_data'][$i];
        update_post_meta( $variation_id, '_custom_field', $custom_data );
    }

}

Добавить поле в категории Woocommerce. Custom field Woocommerce

Добавление дополнительного поля в категории плагина woocommerce. Поле с визуальным редактором от WordPress.

<?php
add_action( 'product_cat_add_form_fields', 'taxonomy_add_meta_field', 10, 2 );
// A callback function to add a custom field to our "presenters" taxonomy
function taxonomy_add_meta_field($tag) {
    // Check for existing taxonomy meta for the term you're editing
    ?>
    <div class="form-field">
        <label for="term_meta[description_bottom]"><?php _e( 'Description after Products', 'project_wpml' ); ?></label>
        <textarea type="text" class="wp-editor-area" name="term_meta[description_bottom]" id="term_meta[description_bottom]" value=""></textarea>
        <p class="description"><?php _e( 'Enter a value for this field','project_wpml' ); ?></p>
    </div>
    <?php
}

add_action( 'product_cat_edit_form_fields', 'taxonomy_edit_meta_field', 10, 2 );
// Edit term page
function taxonomy_edit_meta_field($term) {
    // put the term ID into a variable
    $t_id = $term->term_id;

    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_option( "taxonomy_$t_id" );
    $settings = array('textarea_name' =>  "term_meta[description_bottom]" );
    ?>

    <tr class="form-field">
        <th scope="row" valign="top"><label for="term_meta[description_bottom]"><?php _e( 'Description after Products', 'project_wpml' ); ?></label></th>
        <td>
            <?php wp_editor( $term_meta['description_bottom'],  'description_bottom', $settings ); ?>
            <!--<textarea name="term_meta[description_bottom]" id="term_meta[description_bottom]" value="<?php echo esc_attr( $term_meta['description_bottom'] ) ? esc_attr( $term_meta['description_bottom'] ) : ''; ?>"></textarea>-->
            <p class="description"><?php _e( 'Enter a value for this field','project_wpml' ); ?></p>
        </td>
    </tr>
    <?php
}

function save_taxonomy_custom_meta( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
    }
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
?>