S
S
SP2020-05-07 21:14:48
WooCommerce
SP, 2020-05-07 21:14:48

How to not display empty value or less than 0 in Woo custom field?

add_action( 'woocommerce_product_options_pricing', 'wc_rrp_product_field' );
function wc_rrp_product_field() {
woocommerce_wp_text_input( array( 'id' => 'rrp_price', 'class' => 'wc_input_price short', 'label' => __( 'Цена в долларах', 'woocommerce' ) . ' ($)' ) );
}
add_action( 'save_post', 'wc_rrp_save_product' );
function wc_rrp_save_product( $product_id ) {
// Если это автосохранение, то ничего не делаем, сохраняем данные только при нажатии на кнопку Обновить
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST['rrp_price'] ) ) {
if ( is_numeric( $_POST['rrp_price'] ) )
update_post_meta( $product_id, 'rrp_price', $_POST['rrp_price'] );
} else delete_post_meta( $product_id, 'rrp_price' );
}
add_action( 'woocommerce_single_product_summary', 'wc_rrp_show', 5 );
function wc_rrp_show() {
global $product;
// Ничего не предпринимаем для вариативных товаров
if ( $product->product_type <> 'variable' ) {
$rrp = get_post_meta( $product->id, 'rrp_price', true );
echo '<div class="woocommerce_msrp">';
_e( '', 'woocommerce' );
echo '<span class="woocommerce-rrp-price">' . woocommerce_price( $rrp ) . '</span>';
echo '</div>';
}
}
 
// Дополнительно: Для вывода на страницах архивов (в товарных категориях, например)
add_action( 'woocommerce_after_shop_loop_item_title', 'wc_rrp_show' );


The code is displayed in the price of the product, regardless of whether there is a value in it or not.
By default, it displays 0.
How can I remove the output of an empty field or if its value is less than 0?
Through js, jq, php?

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