Code to Update Prices and add them for Woocommerce Custom Product Variations Loop


I am actually struggling with some custom functions. Actually My aim is the following: Create 2 custom fields in a woocommerce variable product backend ✔ Show these custom fields text and prices in frontend Make able to add the product in the cart with these prices summed up.

I don’t know which hook I am using wrong and why the theme ajax doesn’t work. There is someone that can assist?

"Working" (I hope) Code

// Aggiunte campi asseverazione, legalizzazione Variazioni nel backend add_action( 'woocommerce_variation_options_pricing', 'add_variation_options_pricing_assev', 10, 3 ); function add_variation_options_pricing_assev( $  loop, $  variation_data, $  variation ){      woocommerce_wp_text_input( array(         'id' => '_assev_'.$  loop,         'wrapper_class' => 'form-row form-row-first',         'class' => 'short wc_input_price',         'label' => __( 'Asseverazione', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',         'value' => wc_format_localized_price( get_post_meta( $  variation->ID, '_assev', true ) ),         'data_type' => 'price',     ) );     woocommerce_wp_text_input( array(         'id' => '_legal_'.$  loop,         'wrapper_class' => 'form-row form-row-last',         'class' => 'short wc_input_price',         'label' => __( 'Legalizzazione', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',         'value' => wc_format_localized_price( get_post_meta( $  variation->ID, '_legal', true ) ),         'data_type' => 'price',     ) );      }  // Salvataggio in backend add_action( 'woocommerce_save_product_variation','save_variation_options_pricing_assev', 10, 2 ); function save_variation_options_pricing_assev( $  variation_id, $  loop ){     if( isset($  _POST['_assev_'.$  loop]))         update_post_meta( $  variation_id, '_assev', wc_clean( wp_unslash( str_replace( ',', '.', $  _POST['_assev_'.$  loop] ) ) ) );     if( isset($  _POST['_legal_'.$  loop]))         update_post_meta( $  variation_id, '_legal', wc_clean( wp_unslash( str_replace( ',', '.', $  _POST['_legal_'.$  loop] ) ) ) ); }  // Frontend (mi manca di aggiungere la legalizzazione) add_filter( 'woocommerce_available_variation', 'display_variation_assev', 10, 3 );  function display_variation_assev( $  data, $  product, $  variation ) {      if( $  assev = $  variation->get_meta('_assev') ) {         $  data['price_html'] = '<div class="woocommerce_assev">' . __( 'Asseverazione: ', 'woocommerce' ) .         '<span class="asseverazione-prezzo">' . wc_price( $  assev ) . '</span></div>' . $  data['price_html'];     }      return $  data; } 

Code Struggle:

/*add_filter( 'woocommerce_add_cart_item', 'aggiorna_prezzi_nuova', 10, 2 ); function aggiorna_prezzi_nuova( $  cart_item_data, $  cart_item_key, $  variation) {                if( isset( $  _POST['_assev_'][$  variation_id] ) )         $  cart_item_data['_assev'][$  variation_id] = $  _POST['_assev'][$  variation_id];     if(isset( $  _POST['_legal'] ) )        $  cart_item_data['_legal'] = $  _POST['_legal'];      return $  cart_item_data; }   add_action( 'woocommerce_before_calculate_totals', 'fissa_valore_var_carrello', 20, 1 ); function fissa_valore_var_carrello( $  cart ) {     if ( is_admin() && ! defined( 'DOING_AJAX' ) )         return;      if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )         return;      // First loop to check if product 11 is in cart     foreach ( $  cart->get_cart() as $  cart_item ){         if( isset($  cart_item['_assev']) && isset($  cart_item['_legal'])          && ! empty($  cart_item['_assev']) && ! empty($  cart_item['_legal']) )             $  cart_item['data']->set_price( '30' );     } }*/ 

Other non functional code:

function aggiorna_prezzo( $  price, $  product ) {         $  shortcost_first = get_post_meta( $  variation->id, '_assev_0', true );    $  shortcost_mid = get_post_meta( $  product->id, '_assev_0', true );   $  shortcost_minimum = get_post_meta( $  product->id, 'assev_0', true );        $  unit_price = $  assev + $  shortcost_mid + $  shortcost_first + $  shortcost_minimum; //     +($  shortcost_mid * ($  shortcost_minimum - 1));    return $  price + $  unit_price;       } add_filter('woocommerce_get_price', 'aggiorna_prezzo', 10, 3); add_filter('woocommerce_get_regular_price','aggiorna_prezzo', 10, 3); add_filter('woocommerce_get_sale_price','aggiorna_prezzo', 10, 3); 

Thank you!