M
M
makboriska2022-02-17 21:35:47
WordPress
makboriska, 2022-02-17 21:35:47

Woocommerce add custom field to cart?

I want to display in the cart (namely in cart.php) the amount of the additional fee (it costs on each product), but when I try to do this, nothing is displayed as standard.

Here is my custom field:

add_action( 'woocommerce_product_options_pricing', 'demolink_woo_add_custom_fields_2' );
function demolink_woo_add_custom_fields_2() {
  echo '<div class="options_group">';// Группировка полей
  
    // цифровое поле
    woocommerce_wp_text_input( array(
       'id'                => '_number_field',
       'label'             => __( 'Доп. сборы (%)', 'woocommerce' ),
       'placeholder'       => 'Ввод чисел',
       'description'       => __( 'Вводится в %', 'woocommerce' ),
       'type'              => 'number',
       'custom_attributes' => array(
        'step' => 'any',
        'min'  => '0',
       ),
    ) );
  
  echo '</div>';
  
}

// Сохраняем метаданные заказа со значением поля
add_action( 'woocommerce_checkout_update_order_meta', 'art_woo_custom_fields_save_2' );
function art_woo_custom_fields_save_2( $post_id ) {

  // Сохранение текстового поля.
  $woocommerce_number_field = $_POST['_number_field'];
  if ( ! empty( $woocommerce_number_field ) ) {
    update_post_meta( $post_id, '_number_field', esc_attr( $woocommerce_number_field ) );
  }
}

And this is how I'm trying to get it out

global $post, $product;
  $num_field      = get_post_meta( $post->ID, '_number_field', true );
      echo $num_field;

But nothing comes out, the value is not displayed, tell me pliz what's wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question