1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以將其用于簡(jiǎn)單的產(chǎn)品:
add_filter( 'woocommerce_get_price_html', 'custom_product_price_display', 10, 2 );
add_filter( 'woocommerce_cart_item_price', 'custom_product_price_display', 10, 2 );
function custom_product_price_display( $price, $product ) {
if( ! is_a( $product, 'WC_Product' ) ) {
$product = $product['data'];
}
// Price per meter prefixed
if( $product->is_type('simple') && $product->get_length() ) {
$unit_price_html = wc_price( $product->get_price() / $product->get_length() );
$price .= ' <span class="per-meter">' . __("Price per meter is: ") . $unit_price_html . '</span>';
}
return $price;
}
代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。經(jīng)過(guò)測(cè)試并有效。
要處理“每米顯示的價(jià)格”和“每單位”前綴,請(qǐng)改用以下內(nèi)容:
add_filter( 'woocommerce_get_price_html', 'custom_product_price_display', 10, 2 );
add_filter( 'woocommerce_cart_item_price', 'custom_product_price_display', 10, 2 );
function custom_product_price_display( $price, $product ) {
if( ! is_a( $product, 'WC_Product' ) ) {
$product = $product['data'];
}
// Price per meter prefixed
if( $product->is_type('simple') && $product->get_length() ) {
$unit_price_html = wc_price( $product->get_price() / $product->get_length() );
$price .= ' <span class="per-meter">' . __("Price per meter is: ") . $unit_price_html . '</span>';
}
// Prefix" per unit"
else {
$price .= ' <span class="per-unit">' . __("per unit") . $unit_price_html . '</span>';
}
return $price;
}
- 1 回答
- 0 關(guān)注
- 134 瀏覽
添加回答
舉報(bào)