javascript - How display a product custom field value in WooCommerce?

I try to make custom calculator on product page. I made a calculator and script which calculated. But all start values is static now. This calculator should work with custom values each product. I have a section which I added on product page like 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 get value from my custom fild and add this oninput name="PPRICE"
value.
Something like this
The code my custom fild is:
// 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
Solution:
To get a product custom field from a product Id (so the product Id is required) use Wordpressget_post_meta()
with the right meta key.
Simply replace in your first function the line:
<input name="PPRICE" value="1.67" style="display: none;">
with this code line for example:
<input name="PPRICE" value="1.67 (' . get_post_meta( get_the_id(), '_number_field', true) . ' м<sup>2</sup> в упаковке)" style="display: none;">
The WooCommerce way (Since WooCommerce 3)
You can also use TheWC_Data
methodget_meta()
on the WC_Product Object instance like:
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_id() );
}
$value_number_field = $product->get_meta('_number_field'); // Get custom field value
echo $value_number_field; // Display it
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.