亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 WooCommerce 產品上顯示自定義前綴以及每米價格

在 WooCommerce 產品上顯示自定義前綴以及每米價格

PHP
慕森王 2023-08-26 17:51:05
我使用此代碼在我的產品價格下方添加文本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();產品價格除以產品長度,無法在不導致網站錯誤的情況下使其正常工作。另外,有什么方法可以使此代碼僅適用于某些產品嗎?因為有很多單位出售
查看完整描述

1 回答

?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

您可以將其用于簡單的產品:


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;

}


查看完整回答
反對 回復 2023-08-26
  • 1 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號