Display prefixed price and additional unit price in Woocommerce simple products

核能气质少年 提交于 2021-01-28 12:26:00

问题


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 / box

$20 / bottle

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.

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.


回答1:


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;
}

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


Related:

  • Display on shop pages the unit price and the wholesale price on product pages
  • Show a price suffix only on all WooCommerce single products



回答2:


There are several ways to achieve this. The one you posted above might work once you append the new price to the original string by changing these two lines

$price = $group_price_sale . '<br>' . $unit_price_sale;
$price = $group_price . '<br>' . $unit_price;

to

$price .= $group_price_sale . '<br>' . $unit_price_sale;
$price .= $group_price . '<br>' . $unit_price;

Another way would be to hook into the price suffix function:

function so1664798_add_bottle_price_suffix( $html, $product, $price, $qty ){
  $bottleprice = $price / 6;
  $html .= ' ('.$bottleprice .' per bottle)';
  return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'so1664798_add_bottle_price_suffix', 99, 4 );

Or, if you want the price per bottle to show for example on the single product page, you can hook in there with this code (I use it for price per litre information):

function so1664798_add_bottle_price() {
    global $product;
    $price = $product->get_price();
    $price_per_bottle = $price / 6;
    echo '<span class="price-unit smaller">'.number_format((float)$price_per_bottle, 2, wc_get_price_decimal_separator(), '').get_woocommerce_currency_symbol().' / '.__('Bottle','txtdomain').',&nbsp;</span>';
}
add_action('woocommerce_single_product_summary','so1664798_add_bottle_price_summary',10);

The last option will be the safest one if you want to avoid your bottle price to be displayed at several other places (checkout, cart, ...) but only want it at the product page.



来源:https://stackoverflow.com/questions/63054379/display-prefixed-price-and-additional-unit-price-in-woocommerce-simple-products

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!