在 Woocommerce 简单产品中显示前缀价格和附加单价 [英] Display prefixed price and additional unit price in Woocommerce simple products
问题描述
我使用 Woocommerce 每盒销售 6 瓶葡萄酒.所以我很自然地在设置我的产品时输入每盒的价格.但是,我确实希望用户也能看到每瓶的价格.
Using Woocommerce I sell wine per box with 6 bottles in a box. So naturally I enter a price per box when setting up my product. I do however want the user to see the price per bottle as well.
$120/盒
$20/瓶
我尝试使用 在商店页面上显示产品页面上的单价和批发价答案代码.它完全符合我的要求,除了它删除了原始(每盒)价格.
I tried using Display on shop pages the unit price and the wholesale price on product pages answer code. It does exactly what I want, except, it removes the original ( per box ) price.
下面经过编辑的片段显示原始 $price 和单位 $price 以及组/单位指示符(对于简单产品):
The edited snippet below display both the original $price and the unit $price as well as the group/unit indicator (for simple products):
add_filter( 'woocommerce_get_price_html', 'unit_product_price_on_archives', 10, 2 );
function unit_product_price_on_archives( $price, $product ) {
if ( is_product() || is_product_category() || is_product_tag() ) {
$unit_divider = 6;
$group_suffix = ' '. __('(per box)', 'woocommerce');
$unit_suffix = ' '. __('(per bottle)', 'woocommerce');
if( $product->is_on_sale() )
{
$regular_price_unit = $product->get_regular_price() / $unit_divider;
$regular_price_unit = wc_get_price_to_display( $product, array( 'price' => $regular_price_unit ) );
$regular_price_group = $product->get_regular_price();
$regular_price_group = wc_get_price_to_display( $product, array( 'price' => $regular_price_group ) );
$group_price_sale = $product->get_sale_price();
$group_price_sale = wc_get_price_to_display( $product, array( 'price' => $group_price_sale ) );
$group_price_sale = wc_format_sale_price( $regular_price_group, $group_price_sale ) . $group_suffix;
$unit_price_sale = $product->get_sale_price() / $unit_divider;
$unit_price_sale = wc_get_price_to_display( $product, array( 'price' => $unit_price_sale ) );
$unit_price_sale = wc_format_sale_price( $regular_price_unit, $unit_price_sale ) . $unit_suffix;
$price = $group_price_sale . '<br>' . $unit_price_sale;
}
else
{
$group_price = $price;
$group_price = $group_price . $group_suffix;
$unit_price = $product->get_price() / $unit_divider;
$unit_price = wc_get_price_to_display( $product, array( 'price' => $unit_price ) );
$unit_price = $price = wc_price($unit_price) . $unit_suffix;
$price = $group_price . '<br>' . $unit_price;
}
}
return $price;
}
然而,我只提供了正常或促销价的标准产品.
I did however only cater for standard product with regular or sale price.
我没有将此扩展到分组或可变产品.
I did not extend this to grouped or variable products.
推荐答案
我重新审视了代码并简化了它(仅针对单个产品):
I have revisited code and simplified it (for single products only):
add_filter( 'woocommerce_get_price_html', 'custom_product_price_html', 10, 2 );
function custom_product_price_html( $price, $product ) {
// For simple products only
if ( $product->is_type('simple') ) {
$unit_divider = 6;
$suffix_box = ' '. __('(per box)', 'woocommerce');
$suffix_unit = ' '. __('(per bottle)', 'woocommerce');
$active_price_unit = wc_get_price_to_display( $product, array( 'price' => ( $product->get_price() / $unit_divider ) ) );
if($product->is_on_sale() )
{
$regular_price_unit = wc_get_price_to_display( $product, array( 'price' => ( $product->get_regular_price() / $unit_divider ) ) );
$formatted_price_unit = wc_format_sale_price( $regular_price_unit, $active_price_unit );
$price .= $suffix_box . '<br><span class="unit-price">' . $formatted_price_unit . $suffix_unit . '</span>';
}
else
{
$price .= $suffix_box . '<br><span class="unit-price">' . wc_price($active_price_unit) . $suffix_unit . '</span>';
}
}
return $price;
}
代码位于活动子主题(或主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of your active child theme (or theme). Tested and works.
相关:
这篇关于在 Woocommerce 简单产品中显示前缀价格和附加单价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!