[英]Display custom prefix with price per meter on WooCommerce products
我正在使用此代碼在我的產品價格下方添加文本
function cw_change_product_price_display( $price ) {
$price .= ' <span class="unidad">por unidad</span>';
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
但我需要顯示以下文本:每米價格是:和這個公式:
$product->get_price()/$product->get_length() . get_woocommerce_currency_symbol();
產品價格除以產品長度,無法在網站上正常運行而不會導致錯誤。
另外,有沒有辦法讓這個代碼只適用於某些產品? 因為每單位出售的數量很多
您可以以這種方式將其用於您的簡單產品:
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;
}
代碼進入您的活動子主題(或活動主題)的functions.php 文件。 測試和工作。
要同時處理“顯示的每米價格”和前綴“每單位”,請改用以下內容:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.