N
N
neytan2018-11-08 20:04:10
WooCommerce
neytan, 2018-11-08 20:04:10

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.
5be46c6fd9cfd399847835.png
Is it possible to do this or just add through attributes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex Vasilyev, 2018-11-08
@neytan

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 );
}

Output in a template
$price_index = '_custom_price';

    echo wc_price( get_post_meta( get_the_ID(), $price_index, true  ) );

N
neytan, 2018-11-08
@neytan

There was another question. You can display this parameter for batch editing in the admin panel when selecting several products and selecting the "Edit" > "Apply" item.
646d00cc59.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question