java时间最小单位是什么_在Woocommerce中设置变量产品的最小单位显示价格

您应该使用可变产品并为每种产品设置3种变体:

每单位

每箱

__每个托盘

1)在后端:仅对于可变产品,我们添加自定义设置字段,用于显示“最小单价” .

eaf705c9-5aeb-468b-b523-726cf6569983.png

2)在前端:仅对于可变产品,我们在商店,档案和单个产品页面中显示自定义的“最低单价” .

891bdc45-251c-416f-84ef-e939ce174319.png

代码:

// Backend: Add and display a custom field for variable products

add_action('woocommerce_product_options_general_product_data', 'add_custom_product_general_field');

function add_custom_product_general_field()

{

global $post;

echo '

';

woocommerce_wp_text_input(array(

'id' => '_min_unit_price',

'label' => __('Min Unit price', 'woocommerce') ,

'placeholder' => '',

'description' => __('Enter the minimum unit price here.', 'woocommerce'),

'desc_tip' => 'true',

));

echo '

';

}

// Backend: Save the custom field value for variable products

add_action('woocommerce_process_product_meta', 'save_custom_product_general_field');

function save_custom_product_general_field($post_id)

{

if (isset($_POST['_min_unit_price'])){

$min_unit_price = sanitize_text_field($_POST['_min_unit_price']);

// Cleaning the min unit price for float numbers in PHP

$min_unit_price = str_replace(array(',', ' '), array('.',''), $min_unit_price);

// Save

update_post_meta($post_id, '_min_unit_price', $min_unit_price);

}

}

// 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( __( 'From %1$s', 'woocommerce' ), $min_price_html );

}

return $price;

}

代码位于活动子主题(或活动主题)的function.php文件中 . 经过测试和工作 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值