自定义格式的最低价格,包括特定 WooCommerce 变量产品的税

Custom formatted min price including tax for specific WooCommerce Variable Product

我正在尝试更改特定产品的价格,使其显示含增值税的价格(与显示价格不含增值税的其他产品相反)

我已经设法让它与可变产品本身一起使用,方法是使用 https://tomjesch.com/display-woocommerce-products-with-and-without-tax/

中的以下代码
function edit_selected_variation_price( $data, $product, $variation ) {
    if(is_singular('product') && $product->get_id() == 68719 ) {
    $price = $variation->price;
    $price_incl_tax = $price + round($price * ( 20 / 100 ), 2);  
    $price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
    $price = number_format($price, 2, ",", ".");
        $display_price = '<span class="price">';
        $display_price .= '<span class="amount">£ ' . $price_incl_tax .'<small class="woocommerce-price-suffix"> incl VAT</small></span>';
        $display_price .= '</span>';
    $data['price_html'] = $display_price;
    }
    return $data;
}
add_filter( 'woocommerce_available_variation', 'edit_selected_variation_price', 10, 3);

这在选择一个选项时有效。但是,在选择一个选项之前,有一个价格显示 FROM:£xxx 我现在也想更改为“FROM:£xxx inc VAT”

但是,我似乎无能为力。所以我添加了以下内容来设置 html 的价格:

function cw_change_product_html( $price_html, $product ) {
 if ( $product->get_id() == 68719 ) {
     $price_incl_tax = $product->price + round($price * ( 20 / 100 ), 2);  
    $price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
 $price_html = '<span class="amount">From ' . $price_incl_tax . 'incl VAT</span>';
 }
 echo $price_html;
}

然后我尝试使用这三种不同的钩子。

add_filter( 'woocommerce_get_price_html_from_to', 'cw_change_product_html', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );
add_filter('woocommerce_variable_price_html', 'cw_change_product_html', 10, 2);

只有第二个似乎触发了代码,但随后它输出了所有不同变体的所有价格。

我需要使用不同的钩子还是有什么方法可以 运行 上面的代码一次?

您调用了正确的钩子 (woocommerce_get_price_html),但是您的代码中存在一些缺陷。

  1. 让我们解决您的 运行 代码仅用于顶部显示价格的问题。确保您正在检查的 ID 是父 ID。

  2. 不要直接访问产品对象的值。 WooCommerce 为大量数据提供 getter 功能。所以改用 $product->get_price().

  3. 您有一个未定义的变量$price,它会使您的代码崩溃。

  4. 您可以检索父产品的税率,而不是将其硬编码到函数的计算中。

  5. 您可以使用wc_price()函数输出格式化价格。

最终代码应如下所示:

add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );
function cw_change_product_html( $price_html, $product ) {

     if ( $product->get_id() == 68719 ) {
        $tax_rates = WC_Tax::get_rates( $product->get_tax_class() );

        //Check the product tax rate of parent
        if ( !empty( $tax_rates ) ) {
            $tax_rate = reset($tax_rates);
            $price_incl_tax =  $product->get_price() + ( $product->get_price() * $tax_rate['rate'] / 100 );
            $price_html = sprintf( 'From %s incl VAT', wc_price( $price_incl_tax, array( 'currency' => get_woocommerce_currency() ) ) );
        }
     
    }
    
    return $price_html;
}

您的代码中有一些错误。 woocommerce_variable_price_html 是最好的钩子。此外,您可以使用 WC_Tax 方法动态获取税额,而不是使用自定义税收计算。最后 wc_price() 是要在 WooCommerce 中使用的格式化价格函数。

代码:

add_filter( 'woocommerce_variable_price_html', 'filter_wc_variable_price_html', 10, 2 );
function filter_wc_variable_price_html( $price_html, $product ) {
    // only for variable product 68719
    if( $product->get_id() != 68719 )
        return $price_html;

    $min_price = $product->get_variation_price( 'min' );
    $tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
    $taxes     = WC_Tax::calc_tax( $min_price, $tax_rates, false );

    return sprintf(
        __( 'From %s %s', 'woocommerce' ),
        wc_price( $min_price + array_sum( $taxes ) ),
        '<small class="woocommerce-price-suffix">' . __( 'incl VAT', 'woocommerce' ) . '</small>'
    );
}

由于您的其他代码有点过时且复杂,您可以尝试以下方法:

add_filter( 'woocommerce_available_variation', 'filter_wc_available_variation_price_html', 10, 3);
function filter_wc_available_variation_price_html( $data, $product, $variation ) {
    // only for variable product 68719
    if( $product->get_id() != 68719 )
        return $data;

    $price     = $data['display_price'];
    $tax_rates = WC_Tax::get_rates( $variation->get_tax_class() );
    $taxes     = WC_Tax::calc_tax( $price, $tax_rates, false );

    $data['price_html'] = sprintf( '%s %s',
        wc_price( $price + array_sum( $taxes ) ),
        '<small class="woocommerce-price-suffix">' . __( 'incl VAT', 'woocommerce' ) . '</small>'
    );

    return $data;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。