[英]Display prefixed price and additional unit price in Woocommerce simple products
[英]Set a min unit displayed price for simple products too in Woocommerce
在Woocommerce中,我想在目录前面显示简单产品和可变产品的最低价格。
基于“ 在Woocommerce中为可变商品设置最低显示单位价格 ”的答案代码,在其中我对最后一个功能进行了如下更改:
// 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;
}
我也想使此解决方案适用于简单的产品。 任何帮助表示赞赏。
在产品附加价格设置字段(请参见下面的屏幕截图)中 , 最低单价替换了Woocommerce商店,档案库和单个产品页面中显示的产品价格。
基于“ 设置Woocommerce中可变产品的最低单位显示价格 ”答案代码,以下内容适用于Woocommerce 3及更高版本中使用的简单可变产品 。
代码(完全重新访问和增强):
// 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;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。 经过测试和工作。
在后端,对于简单和可变产品:
在前端,用于简单和可变产品(在商店,档案馆和单个产品页面中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.