在Woocommerce中也为简单产品设置最低显示价格 [英] Set a min unit displayed price for simple products too in Woocommerce

查看:79
本文介绍了在Woocommerce中也为简单产品设置最低显示价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我想在目录前面显示简单产品和可变产品的最低价格.

基于"

在前端,用于简单和可变产品(在商店,档案馆和单个产品页面中)

In Woocommerce I would like to show a minimum price for a simple product and variable products in front of the catalog.

Based on "Set a min unit displayed price for variable products in Woocommerce" answer code, where I have made light changes to the last function as follow:

// Frontend: Display the min price with "From" prefix label for variable products
add_filter( 'woocommerce_variable_price_html', 'custom_min_unit_variable_price_html', 30, 2 );
function custom_min_unit_variable_price_html( $price, $product ) {
    $min_unit_price = get_post_meta( $product->get_id(), '_min_unit_price', true );

    if( $min_unit_price > 0 ){
        $min_price_html = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
        $price = sprintf( __( '%1$s <span class="amount"><div  style="font-size:11px;color: #666;text-align: center;">Bulk purchasing</div></span>', 'woocommerce' ), $min_price_html );
    }

    return $price;
}

I would like to adapt this solution to simple products as well. Any help is appreciated.

解决方案

From product additional price setting field (see the screenshots below), a minimal unit price replace the displayed product price in Woocommerce shop, archives and single product pages.

Based on "Set a min unit displayed price for variable products in Woocommerce" answer code, the following will work for simple and variable products to be used with Woocommerce 3 and above.

The code (completely revisited and enhanced):

// Backend: Add and display a custom field for simple and variable products
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_price_field_to_general_product_data' );
function add_custom_price_field_to_general_product_data() {
    global $product_object;

    echo '<div class="options_group hide_if_external">';

    woocommerce_wp_text_input(array(
        'id'          => '_min_unit_price',
        'label'       => __('Min Unit price', 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')',
        'description' => __('Enter the minimum unit price here.', 'woocommerce'),
        'desc_tip'    => 'true',
        'value'       => str_replace('.', ',', $product_object->get_meta('_min_unit_price') ),
        'data_type'   => 'price'
    ));

    echo '</div>';
}

// Backend: Save the custom field value for simple and variable products
add_action( 'woocommerce_admin_process_product_object', 'save_product_custom_price_field', 10, 1 );
function save_product_custom_price_field( $product ) {
    if ( isset($_POST['_min_unit_price']) ) {
        $product->update_meta_data( '_min_unit_price', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_min_unit_price'] ) ) ) );
    }
}

// Frontend variable products: Display the min price with "From" prefix label
add_filter( 'woocommerce_variable_price_html', 'custom_min_unit_variable_price_html', 10, 2 );
function custom_min_unit_variable_price_html( $price, $product ) {
    if( $min_unit_price = $product->get_meta('_min_unit_price') ){
        $price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
        $price .= ' <span class="prefix" style="font-size:11px;color: #666;text-align: center;">'.__('Bulk purchasing').'</span>';
    }
    return $price;
}

// Frontend simple products: Display the min price with "From" prefix label
add_filter( 'woocommerce_get_price_html', 'custom_min_unit_product_price_html', 10, 2 );
function custom_min_unit_product_price_html( $price, $product ) {
    if( $product->is_type('simple') && $min_unit_price = $product->get_meta('_min_unit_price') ){
        $price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
        $price .= ' <span class="prefix" style="font-size:11px;color: #666;text-align: center;">'.__('Bulk purchasing').'</span>';
    }
    return $price;
}

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


In backend, for simple and variables products:

In frontend, for simple and variables products (in shop, archives and single product pages)

这篇关于在Woocommerce中也为简单产品设置最低显示价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆