为了账号安全,请及时绑定邮箱和手机立即绑定

在 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 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信