Rumah > Soal Jawab > teks badan
Saya telah cuba menukar harga jualan produk WooCommerce saya kepada "Percuma Ahli" dan bukannya 0. Saya ingin menukar harga jualan kepada teks tersuai seperti:
Saya mencari di internet dan menemui coretan kod yang melakukan perkara yang sama, tetapi masalahnya ialah ia turut mengubah harga jualan dan harga biasa.
Ini adalah kod yang saya temui di Stack Overflow:
function my_wc_custom_get_price_html( $price, $product ) { if ( $product->get_price() == 0 ) { if ( $product->is_on_sale() && $product->get_regular_price() ) { $regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) ); $price = wc_format_price_range( $regular_price, __( 'Free for members!', 'woocommerce' ) ); } else { $price = '<span class="amount">' . __( 'Free!', 'woocommerce' ) . '</span>'; } } return $price; } add_filter( 'woocommerce_get_price_html', 'my_wc_custom_get_price_html', 10, 2 );
Masalahnya ialah kod ini juga mengalih keluar coretan pada harga biasa. Inilah hasilnya apabila saya cuba menambah beberapa CSS sebaris untuk menambah coretan:
Apa yang saya ingin capai ialah menukar harga jualan seperti yang ditunjukkan dalam tangkapan skrin di bawah:
P粉4208682942024-03-23 09:05:34
Apabila produk dijual dengan harga 0 (sifar), cuba gunakan kod ringkas berikut untuk memaparkan harga biasa dengan coretan dalam teks tersuai:
add_filter( 'woocommerce_get_price_html', 'custom_formatted_sale_price_html', 10, 2 ); function custom_formatted_sale_price_html( $price_html, $product ) { if ( $product->is_on_sale() && $product->get_price() !== 0 ) { $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); $sale_price_text = $regular_price > 0 ? __( 'Free only for members!', 'woocommerce' ) : __( 'Free!', 'woocommerce' ); $style = $regular_price > 0 ? ' style="background-color:#08A04B;color:white;padding:0 5px;"' : ''; return '<del aria-hidden="true">' . wc_price( $regular_price ) . '</del> <span'.$style.'>' . $sale_price_text . '</span>'; } return $price_html; }
Ia sepatutnya berfungsi seperti yang anda jangkakan.