【问题标题】:WooCommerce variable products: keep only "min" price with a custom labelWooCommerce 可变产品:仅保留带有自定义标签的“最低”价格
【发布时间】:2017-09-01 18:19:13
【问题描述】:

在函数文件中,我添加了一个过滤器挂钩,以便在变体产品“最低”价格之前添加一个自定义标签。

如何让标签与价格在同一行?

查看我的代码和下面的屏幕截图:

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;
}

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    自 WooCommerce 3 以来,woocommerce_variable_sale_price_html 钩子已被弃用,不再有用。如果你不关心“最低”售价(最低售价时),你可以使用这个:

    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;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    在 WooCommerce 3+ 上测试并运行。你会得到这样的东西:

    如果您关心“最低”销售价格(当最低价格打折时),并且您想同时显示这两个价格,您应该使用以下代码:

    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;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    在 WooCommerce 3+ 上测试并运行。你会得到这样的东西:

    在所有变化价格相同时处理:

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

    【讨论】:

      猜你喜欢
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多