Custom Field no Woocommerce returns result

Asked

Viewed 203 times

2

I’m having a problem with Woocommerce. I added a field that does not have in the standard of Woocommerce that would add the amount of products that go inside the master box of the product.

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 displayed value. Does anyone know what I might be missing?

1 answer

3


Has two problems with woo_add_custom_general_fields_save().

The first with ‘_embalageminput’, there is no , must be ' or ".
The second is that there is no variable $post_id, then you don’t know where to save.

The right is this way:

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 the $post_id for you, then just receive it by the function as I did.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.