【问题标题】:Replace WooCommerce variable products price range with 'Up to' and the max price将 WooCommerce 可变产品价格范围替换为“最高”和最高价格
【发布时间】:2019-05-29 05:28:27
【问题描述】:

我找到了以下代码(来自 here),它使我能够在 WooCommerce 上的可变价格产品上显示:'From: £10'(在 £10 - £50 的产品上) .我想有效地扭转这种情况并显示“最多:50 英镑”。

我试图调整下面的代码,但就是想不通:

function custom_variable_price_range( $price_html, $product ) {
    $prefix = sprintf('%s: ', __('From', 'woocommerce') );

    $min_regular_price = $product->get_variation_regular_price( 'min', true );
    $min_sale_price    = $product->get_variation_sale_price( 'min', true );
    $max_price         = $product->get_variation_price( 'max', true );
    $min_price         = $product->get_variation_price( 'min', true );

    $price_html = ( $min_sale_price == $min_regular_price ) ? wc_price( $min_regular_price ) :
    '<del>' . wc_price( $min_regular_price ) . '</del>' . '<ins>' . wc_price( $min_sale_price ) . '</ins>';

    return ( $min_price == $max_price ) ? $price_html : sprintf( '%s%s', $prefix, $price_html );
}
add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price_range', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_variable_price_range', 10, 2 );

这就是我想要的样子:

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    以下代码将给出后缀为“Up to:”的“max”可变格式价格并处理销售价格范围:

    add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price_range', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'custom_variable_price_range', 10, 2 );
    function custom_variable_price_range( $price_html, $product ) {
    
        $prefix = __('Up to', 'woocommerce');
    
        $max_regular_price = $product->get_variation_regular_price( 'max', true );
        $max_sale_price    = $product->get_variation_sale_price( 'max', true );
        $max_active_price  = $product->get_variation_price( 'max', true );
        $min_active_price  = $product->get_variation_price( 'min', true );
    
        $price_html = ( $max_sale_price == $max_regular_price ) ? wc_price( $max_active_price ) :
        '<del>' . wc_price( $max_regular_price ) . '</del> <ins>' . wc_price( $max_sale_price ) . '</ins>';
    
        return $min_active_price == $max_active_price ? $price_html : sprintf('%s %s', $prefix, $price_html);
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    如果您不想处理促销价格范围,您将使用:

    add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price_range', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'custom_variable_price_range', 10, 2 );
    function custom_variable_price_range( $price_html, $product ) {
    
        $prefix = __('Up to', 'woocommerce');
    
        $max_active_price  = $product->get_variation_price( 'max', true );
        $min_active_price  = $product->get_variation_price( 'min', true );
    
        $price_html = wc_price( $max_active_price );
    
        return $min_active_price == $max_active_price ? $price_html : sprintf('%s %s', $prefix, $price_html );
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 谢谢@LoicTheAztec,这非常有帮助。是否也可以删除“:”所以“最高 50 英镑”?感谢您的宝贵时间。
    【解决方案2】:

    添加这个

        add_filter( 'woocommerce_variable_sale_price_html', 
       'lw_variable_product_price', 10, 2 );
        add_filter( 'woocommerce_variable_price_html', 
        'lw_variable_product_price', 10, 2 );
    
         function lw_variable_product_price( $v_price, $v_product ) {
    
        // Product Price
         $prod_prices = array( $v_product->get_variation_price( 'min', true ), 
         $v_product->get_variation_price( 'max', true ) );
        $prod_price = $prod_prices[0]!==$prod_prices[1] ? sprintf(__('Up to: %1$s', 
     'woocommerce'), 
       wc_price( $prod_prices[0] ) ) : wc_price( $prod_prices[0] );
    
      // Regular Price
      $regular_prices = array( $v_product->get_variation_regular_price( 'min', true ), 
      $v_product->get_variation_regular_price( 'max', true ) );
      sort( $regular_prices );
       $regular_price = $regular_prices[0]!==$regular_prices[1] ? sprintf(__('Up to: 
      %1$s','woocommerce')
      , wc_price( $regular_prices[0] ) ) : wc_price( $regular_prices[0] );
    
        if ( $prod_price !== $regular_price ) {
       $prod_price = '<del>'.$regular_price.$v_product->get_price_suffix() . '</del> 
        <ins>' . 
        $prod_price . $v_product->get_price_suffix() . '</ins>';
        }
       return $prod_price;
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 2019-05-09
      • 2019-12-20
      • 2018-05-25
      • 1970-01-01
      相关资源
      最近更新 更多