Custom Field in Woocommerce does not return the result

2

I'm having a problem with WooCommerce. I added a field that does not have the default WooCommerce that would be to add the amount of products that go into the product's master box.

The code I'm using in my function.php is as follows:

    <?php
    //ADICIONANDO UM NOVO CAMPO NA AREA DE EDICAO DO PRODUTO
    /// Display Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

    /**Fields Container
    **/
    function woo_add_custom_general_fields() {

        global $woocommerce, $post;

        echo '<div class="options_group">';

        // Textarea
        woocommerce_wp_text_input(
            array(
                'id' => '_embalageminput',
                'label' => __( 'Embalagem', 'woocommerce' ),
                'placeholder' => '',
                'description' => __( 'Quantidade de Peças por Caixa.', 'woocommerce' )
            )
        );

        // END Textarea

        echo '</div>';

    }

    function woo_add_custom_general_fields_save(){
        // Textarea
        $woocommerce_embalagem = $_POST['_embalageminput'];
        if( !empty( $woocommerce_embalagem ) )
            update_post_meta( $post_id, ‘_embalageminput’, esc_attr( $woocommerce_embalagem) );

        echo get_post_meta( get_the_ID(), '_embalageminput', true );
    }

    ?>

When I update the product and come back it does not have the field with the value displayed. Does anyone know what I might be missing?

    
asked by anonymous 13.08.2014 / 21:43

1 answer

3

You have two problems with woo_add_custom_general_fields_save() .

The first with ‘_embalageminput’ , there is no , it must be ' or " . The second is that there is no variable $post_id , so you do not know where to save.

The correct way is this:

function woo_add_custom_general_fields_save( $post_id ) {
    if ( isset( $_POST['_embalageminput'] ) ) {
        update_post_meta( $post_id, '_embalageminput', esc_attr( $_POST['_embalageminput'] ) );
    }
}

The action woocommerce_process_product_meta passes $post_id to you, so just get it by function as I did.

    
13.08.2014 / 23:41