V
V
Valentina2020-01-03 15:29:01
WordPress
Valentina, 2020-01-03 15:29:01

How to get value in php form in Woocommerce product page?

I'm trying to make my calculator on a product page. I made a calculator and a script that calculates. But now all initial values ​​are static. This calculator should work with the individual values ​​of each product. I have a code that I have added to the product page as a shortcode.

function woo_calculator_content() {
  // Калькулятор
   echo ('
<section>
<script src="http://door.vel-wild.pro/wp-content/themes/mrDoor/raschet.js"></script>
 <form action="" method="post">
  <div class="vel_sum input-group">   
      <span>Сколько нужно м<sup>2</sup></span>  
      <input type="button" value="-" class="button-minus" data-field="quantity">
      <input class="vel_sum_meter" id="skolkometrov" name="QTY" type="number" step="1" min="1" value="1" oncclick="multiply(this)">
      <input type="button" value="+" class="button-plus" data-field="quantity">
      <input name="PPRICE" value="1.67" style="display: none;">
      <div> <span>Колличество упаковок <input name="TOTAL" readonly class="vel_sum_upakovka"></span>
       <span><input class="vel_sum_upakovkametrov" name="TOTALMETERS" readonly>м<sup>2</sup></span></div>
    </div>
  </form>
</section>');
} 
add_shortcode( '4cards', 'woo_calculator_content' );


But now I need to get the value from my custom field and add it to the input name = "PPRICE". in the value=""

Code field of my custom file:

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', __NAMESPACE__.'\woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
    global $woocommerce, $post;
    echo '<div class="options_group">';
    // Number Field
    woocommerce_wp_text_input(
        array(
            'id'                => '_number_field',
            'label'             => __( 'м<sup>2</sup> в упаковке', 'woocommerce' ),
            'placeholder'       => '',
            'description'       => __( 'Enter the custom value here.', 'woocommerce' ),
            'type'              => 'number',
            'custom_attributes' => array(
                'step'  => 'any',
                'min'   => '0'
            )
        )
    );
?>
<?php
    echo '</div>';

}

// Save Fields
add_action( 'woocommerce_process_product_meta', __NAMESPACE__.'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){

    // Number Field
    $woocommerce_number_field = $_POST['_number_field'];
    if( !empty( $woocommerce_number_field ) )
        update_post_meta( $post_id, '_number_field', esc_attr( $woocommerce_number_field ) );
}


    // Вывод описания сколько в упаковке
add_action( 'woocommerce_before_add_to_cart_form', 'production_time', 11 ); 
function production_time() {
    global $product;
    $woocommerce_number_field =$product->get_meta('_number_field');
    if( has_term( ['laminat'], 'product_cat' ) )  {
        echo '<div class="vel_costpack_hide"><p class="ri ri-clock">' . sprintf( __( ' Товар продается упаковками. В упаковке: %s', 'woocommerce' ), $woocommerce_number_field, __( 'м<sup>2</sup>', 'woocommerce' )) . '</p></div>';
    }
}

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