为某些产品的 WooCommerce 税率添加新税费

问题描述 投票:0回答:1

我对 WooCommerce 税有疑问。 我需要在总价之前对某些产品添加新的 8% 税,仅针对特定角色“ different_user”。

其他产品已经包含其他税费,这些税费显示在最终价格之前,然后添加到其中。

重要的是,该税不会影响 WooCommerce 中已添加的任何其他税或折扣。

<tr class="tax-rate tax-rate-impuesto-1">
<th>Impuesto</th>
<td data-title="Impuesto">
<span class="woocommerce-Price-amount amount">2,20
<span class="woocommerce-Price-currencySymbol">€</span>
 </span>
 </td>
</tr>

我已经设法通过他们的“ID”使用 add_action 对我需要的产品添加税,并且它有效。

但它显示在不同的行上:

<tr class="fee">
<th>Ajuste de impuestos adicionales</th>
<td data-title="Ajuste de impuestos adicionales">
<span class="woocommerce-Price-amount amount">
<bdi>0,38<span class="woocommerce-Price-currencySymbol">€</span>
</bdi>
</span>
</td>
</tr>

我应该如何将此税添加到我已经从 WooCommerce 获得的税中,而不是显示在新行上?

我已经尝试过几次,但我对此感到非常困惑,我无法理解 WooCommerce 如何处理税收。

我可以直接在 WooCommerce 中添加新的税率,但由于它仅适用于某些产品,所以我无法这样做,并且被阻止了

这是结果:

enter image description here

// Hook to modify taxes based on products and user roles
add_action('woocommerce_cart_calculate_fees', 'add_custom_tax_to_selected_products', 20, 1);

function add_custom_tax_to_selected_products($cart) {
    // Check if it is admin or AJAX request, and avoid executing the code in that case
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    // Check if the user has the role 'different_user'
    if (!current_user_can('usuario_diferente')) {
        return;
    }

    // IDs of products that should receive the 8% tax
    $product_ids_with_tax = array(355, 625); 

    // Variable to store the total additional tax
    $tax_amount = 0;

    //Iterate over the products in the cart
    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        $product_id = $cart_item['product_id'];

        // Check if the product is on the list of products with additional tax
        if (in_array($product_id, $product_ids_with_tax)) {
            //Calculate 8% of the subtotal of this product
            $item_total = $cart_item['line_total']; // Total price of the product in the cart without taxes
            $tax_amount += ($item_total * 0.08);    // Add 8% of the total price of that product
        }
    }

     // If there is a tax amount to add, we add it to the total taxes
    if ($tax_amount > 0) {
         
        $cart->add_fee( __('Ajuste de impuestos adicionales', 'woocommerce'), $tax_amount, true, '' );
    } 
}
 
php wordpress woocommerce hook-woocommerce user-roles
1个回答
0
投票

您可以选择另一种更简单有效的方式,使用税率并为这 2 个产品和特定用户角色分配特定税率。

首先,您需要在 WooCommerce 设置中定义并设置新的税率,该税率与所需位置的所需税率百分比相匹配。

然后您将使用以下代码(用新定义的税率替换函数“零税率”)

add_filter('woocommerce_product_get_tax_status','set_custom_tax_rate_for_specific_user_role_and_products', 10, 2 );
add_filter( 'woocommerce_product_variation_get_tax_status', 'set_custom_tax_rate_for_specific_user_role_and_products', 10, 2 );
function set_custom_tax_rate_for_specific_user_role_and_products( $tax_class, $product ) {
    global $current_user;

    // Only for specific user role and specific products IDs
    if ( in_array( 'usuario_diferente', $current_user->roles ) && in_array( $product->get_id(), array(355, 625) ) ) {
        return 'zero-rate'; // <== Replace with the correct tax rate slug
    }
    return $tax_class;
}

应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.