wordpress 如何通过指定价格来显示woocommerce产品中变量的最低价格

osh3o9ms  于 2022-12-03  发布在  WordPress
关注(0)|答案(1)|浏览(196)

我有一个WordPress的woo-commerce网站,我的工作上有可变的产品1 pack,5 pack,10 pack & 30 pack。价格为1 pack如果你选择30 pack变量是最便宜的,这是我希望显示给谁访问该网站的人的价格。我试图有一个地方在网站的后端,这个价格可以指定和显示,因为每种产品的计算都不同。
我修改了这里的代码Display prefixed price and additional unit price in Woocommerce 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 = ' '. __('pack', 'woocommerce');
    $unit_suffix = ' '. __('(per pack)', '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; 
}

但代码只将原来的简单乘积除以6。

x6yk4ghg

x6yk4ghg1#

这应该会达到您所期望的效果:

/** Variable Product Price Range: "From: $$$min_price" */
add_filter( 'woocommerce_variable_price_html', ' 
bbloomer_variation_price_format_min', 9999, 2 );
function bbloomer_variation_price_format_min( $price, $product ) {
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );
$price = sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $min_price ) );
return $price;
}

相关问题