您应该使用可变产品并为每种产品设置3种变体:
每单位
每箱
__每个托盘
1)在后端:仅对于可变产品,我们添加自定义设置字段,用于显示“最小单价” .
2)在前端:仅对于可变产品,我们在商店,档案和单个产品页面中显示自定义的“最低单价” .
代码:
// 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 '
';}
// 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文件中 . 经过测试和工作 .