Answer the question
In order to leave comments, you need to log in
How to add another price field in woocommerce?
Good afternoon. There is a wp + woocommerce site. You need to add another field with the price and display this field on the product page.
Is it possible to do this or just add through attributes?
Answer the question
In order to leave comments, you need to log in
Adding a field in the admin
add_action( 'woocommerce_product_options_pricing', 'custom_woocommerce_product_options_pricing' );
function custom_woocommerce_product_options_pricing() {
$price_index = '_custom_price';
woocommerce_wp_text_input(
array(
'id' => $price_index,
'value' => get_post_meta( get_the_ID(), $price_index, true ),
'data_type' => 'price',
'label' => __( 'Custom price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')'
)
);
}
add_action( 'woocommerce_admin_process_product_object', 'custom_woocommerce_admin_process_product_object', 10, 1 );
function custom_woocommerce_admin_process_product_object( $product ) {
$price_index = '_custom_price';
$price = wc_clean( wp_unslash( $_POST[ $price_index ] ) );
update_post_meta( $product->get_id(), $price_index, $price );
}
$price_index = '_custom_price';
echo wc_price( get_post_meta( get_the_ID(), $price_index, true ) );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question