drn1008 2017-09-01 18:19
浏览 91
已采纳

WooCommerce变量产品:使用自定义标签仅保留“最低”价格

In the functions file I have added a filter hook to add a custom label before the variation product "min" price.

How can I get the label in the same line as the price?

See my code and the screenshot below:

add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
    $min_price = $product->get_variation_price( 'min', true );
    $price = sprintf( __( 'From%1$s', 'woocommerce' ), wc_price( $min_price ) );
    return $price;
}

  • 写回答

1条回答 默认 最新

  • doudang4857 2017-09-02 00:50
    关注

    Since WooCommerce 3, woocommerce_variable_sale_price_html hook is deprecated and not anymore useful. If you don't care about "min" sale price (when the min price is on sale), you can use this:

    add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
    function custom_min_max_variable_price_html( $price, $product ) {
        $prices = $product->get_variation_prices( true );
        $min_price = current( $prices['price'] );
    
        $min_price_html = wc_price( $min_price ) . $product->get_price_suffix();
        $price = sprintf( __( 'From %1$s', 'woocommerce' ), $min_price_html );
    
        return $price;
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works on WooCommerce 3+. You will get something like this:

    enter image description here

    If you care about "min" sale price (when the min price is on sale), and you want to display both prices, you should use this code instead:

    add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
    function custom_min_max_variable_price_html( $price, $product ) {
        $prices = $product->get_variation_prices( true );
        $min_price = current( $prices['price'] );
    
        $min_keys = current(array_keys( $prices['price'] ));
        $min_price_regular = $prices['regular_price'][$min_keys];
        $min_price_html = wc_price( $min_price ) . $product->get_price_suffix();
    
        if( $min_price_regular != $min_price ){ // When min price is on sale (Can be removed)
            $min_price_regular_html = '<del>' . wc_price( $min_price_regular ) . $product->get_price_suffix() . '</del>';
            $min_price_html = $min_price_regular_html .'<ins>' . $min_price_html . '</ins>';
        }
        $price = sprintf( __( 'From %1$s', 'woocommerce' ), $min_price_html );
    
        return $price;
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works on WooCommerce 3+. You will get something like this:

    enter image description here

    To handle when all variations prices are the same:

    WooCommerce variable products: Display the min price with a custom text for different prices

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。