dtrz17917 2018-05-24 06:30
浏览 285
已采纳

在不设置变量的情况下设置WooCommerce产品的价格范围

IN woocommerce I would like to be able to put a price range on my simple products without setting a variable product as I don't need that.

See the link below: https://www.tnbloom.com.au/product/memorial-wreath/

For example, instead of the actual price of $45, I would like to replace it by $45 - $120 (without setting variable product).

Any help would be much appreciated.

  • 写回答

1条回答 默认 最新

  • donglingyi4679 2018-05-24 07:17
    关注

    The following code will add a custom price field in the product options pricing settings, to set the max price for your displayed price range. So you will have to set that max price for each product.

    Then it will display on front end a price range, on shop, archives and single product pages, only if the max range field has a price set for it.

    The code:

    // Add a custom field for price range to product in backend
    add_action( 'woocommerce_product_options_pricing', 'add_field_product_options_pricing' );
    function add_field_product_options_pricing() {
        global $post;
    
        echo '<div class="options_group show_if_simple">';
    
        woocommerce_wp_text_input( array(
            'id'            => '_max_price_for_range',
            'label'         => __('Max price for range', 'woocommerce').' ('.get_woocommerce_currency_symbol().')',
            'placeholder'   => __('Set the max price for range', 'woocommerce'),
            'description'   => __('Set the max price for range, to activate it…', 'woocommerce'),
            'desc_tip'      => 'true',
        ));
    
        echo '</div>';
    }
    
    // Save product custom field to database when submitted in Backend
    add_action( 'woocommerce_process_product_meta', 'save_product_options_custom_fields', 30, 1 );
    function save_product_options_custom_fields( $post_id ){
        // Saving custom field value
        if( isset( $_POST['_max_price_for_range'] ) ){
            update_post_meta( $post_id, '_max_price_for_range', sanitize_text_field( $_POST['_max_price_for_range'] ) );
        }
    }
    
    // Frontend: display a price range when the max price is set for the product
    add_filter( 'woocommerce_get_price_html', 'custom_range_price_format', 10, 2 );
    function custom_range_price_format( $price, $product ) {
    
        // Only for simple product type
        if( $product->is_type('simple') ){
            // Get the max price for range
            $max_price = get_post_meta( $product->get_id(), '_max_price_for_range', true );
    
            if( empty($max_price) )
                return $price; // exit
    
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) );
    
            $price = sprintf( '%s &ndash; %s', wc_price($active_price), wc_price($max_price) );
        }
        return $price;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    In backend product option settings:

    enter image description here

    Frontend products:

    enter image description here

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

报告相同问题?

悬赏问题

  • ¥500 高有偿提问!求优化设计微信小程序
  • ¥15 matlab在安装时报错 无法找到入口 无法定位程序输入点
  • ¥15 收益高的广告联盟有哪些
  • ¥15 Android Studio webview 的使用问题, 播放器横屏全屏
  • ¥15 删掉jdk后重新下载,Java web所需要的eclipse无法使用
  • ¥15 uniapp正式环境中通过webapi将本地数据推送到设备出现的跨域问题
  • ¥15 xui建立节点,显示错误
  • ¥15 关于#单片机#的问题:开始、复位、十进制的功能可以实现,但是切换八进制的功能无法实现(按下按键也没有效果),把初始状态调成八进制,也是八进制可以实现但是切换到十进制不行(相关搜索:汇编语言|计数器)
  • ¥15 VINS-Mono或Fusion中feature_manager中estimated_depth是特征的深度还是逆深度?
  • ¥15 谷歌浏览器如何备份抖音网页数据