在Woocommerce结帐时有条件地禁用特定产品类别的增值税 [英] Disable VAT conditionally for specific product categories on Woocommerce checkout

查看:54
本文介绍了在Woocommerce结帐时有条件地禁用特定产品类别的增值税的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce中,我正在使用 Toologia for WooCommerce 插件,该插件可以检查如果用户需要发票或普通收据(在希腊是必需的).因此,此插件在结帐页面中添加了一个选择字段,并以否" (默认值)或是"作为选项.

In woocommerce I am using Timologia for WooCommerce plugin, which checks if user wants invoice or plain receipt (this is required in Greece). So this plugin add a select field in checkout page with "No" (default) or "Yes" as options.

如果客户选择是",则零增值税"适用于特定类别的产品,并且在结帐页面上一切正常.

If customer select "Yes" then "zero vat" is applied for products in specific categories and everything works fine in the checkout page.

但是,当客户下订单时,在收到的订单页面和后端(订单编辑页面)上,没有增值税的商品价格就不再存在.

But when customer place the order, on the order received page and on backend (order edit page) the specific item prices are not anymore without VAT.

这是我的代码:

add_action( 'woocommerce_checkout_update_order_review', 'tfwc_taxexempt_checkout_based_invoice_choice' ); 

function tfwc_taxexempt_checkout_based_invoice_choice( $post_data ) {
    global $woocommerce;
    $woocommerce->customer->set_is_vat_exempt( false );
    parse_str($post_data);
    if ( $billing_timologio == 'Y' ) 

    add_filter( 'woocommerce_product_get_tax_class', 'wc_diff_rate_for_user', 1, 2 );

    function wc_diff_rate_for_user( $tax_class, $product ) {

    // let's get all the product category for this product...

    $terms = get_the_terms( $product->id, 'product_cat' );
    foreach ( $terms as $term ) { // checking each category 

    // if it's one of the category we'er looking for
    if(in_array($term->term_id, array(228,231,222))) {
        $tax_class = 'Zero Rate';
        // found it... no need to check other $term
        break;
    }
}

    return $tax_class;
  }
}

此代码在WC 2.X版本上工作正常,在3+版本中停止工作

This code was working fine on WC version 2.X, and stopped working fine in version 3+

感谢您的帮助.

推荐答案

更新3

该插件和您的代码已过时,存在一些错误和错误.在下面的代码中,我使用了ajax和WC_Session的另一种方式.

The plugin and your code are outdated, with some mistakes and errors. In the code bellow I am using another way with ajax and WC_Session.

税率"输入的含税产品的行为.

要使任何有效的税率代码有效,需要输入不含税的产品价格.如果没有,则需要附加代码来获取不含税的产品价格作为自定义购物车商品数据,使零费率"税收有效.

The "Tax rate" behavior on products entered including taxes.

To make any code that manipulate tax rates effective, the products prices need to be entered excluding taxes. If not, additional code is required to grab the product price excluding taxes as custom cart item data, to make the "Zero rate" tax effective.

我添加了一个自定义功能,该功能从插件代码中部分复制,其中添加了一个签出选择字段billing_timologio,以使代码可测试.

I have added a custom function partially copied from the plugin code, that adds a checkout select field billing_timologio, to make the code testable.

当我们将不含税的产品价格添加为自定义购物车项目数据时,添加到购物车ajax按钮也将替换为链接到已定义产品类别的单个产品页面的按钮.

Also the add to cart ajax button is replaced by a button linked to single product pages for the defined product categories, as we add the product price excluding taxes as custom cart item data.

代码(仅适用于Woocommerce 3.3+版本):

The code (only for Woocommerce version 3.3+):

// Your product categories settings
function get_my_terms(){
    return array( 'clothing' );
    return array( 222, 228, 231 );
}

// Change add to cart ajax button to a button linked to single product pages for specific product categories
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_replace_add_to_cart', 10, 2 );
function conditionally_replace_add_to_cart( $html, $product ) {
    if ( has_term( get_my_terms(), 'product_cat', $product_id )  && $product->is_type('simple') ) {
        // Set HERE your button link
        $link = get_permalink($product_id);
        $text = __("Read More", "woocommerce");
        $html = '<a href="' . $link . '" class="button alt add_to_cart_button">' . $text . '</a>';
    }
    return $html;
}

// Adding a custom checkout select field
add_filter( 'woocommerce_billing_fields', 'custom_billing_fields', 20, 1 );
function custom_billing_fields($billing_fields) {

    $billing_fields['billing_timologio'] = array(
        'type'        => 'select',
        'label'       => __('INVOICE'),
        'placeholder' => _x('INVOICE', 'placeholder'),
        'required'    => false,
        'class'       => array('form-row-wide'),
        'clear'       => true,
        'options'     => array(
            'N' => __('No'),
            'Y' => __('Yes'),
        ),
        'value' => '',
    );
    return $billing_fields;
}

// The jQuery Ajax script
add_action( 'wp_footer', 'custom_checkout_script' );
function custom_checkout_script() {
    // Only checkout page
    if( is_checkout() && ! is_wc_endpoint_url() ):

    // Removing WC_Session "
    if( WC()->session->__isset('billing_timologio') ){
        WC()->session->__unset('billing_timologio');
    }
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined')
            return false;

        // On load reset value to 'N'
        $('#billing_timologio_field select').val('N');

        // On change (Ajax)
        $('#billing_timologio_field select').change( function () {
            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'get_ajax_billing_timologio',
                    'b_timologio': $(this).val() == 'Y' ? 1 : 0,
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                }
            });
        });
    });
    </script>
    <?php
    endif;
}

// The Wordpress Ajax receiver
add_action( 'wp_ajax_get_ajax_billing_timologio', 'get_ajax_billing_timologio' );
add_action( 'wp_ajax_nopriv_get_ajax_billing_timologio', 'get_ajax_billing_timologio' );
function get_ajax_billing_timologio() {
    if ( $_POST['b_timologio'] == '1' ){
        WC()->session->set('billing_timologio', '1');
    } else {
        WC()->session->set('billing_timologio', '0');
    }
    die();
}

// Save the product price excluding tax as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_price_excl_vat_as_custom_cart_item_data', 20, 3);
function add_price_excl_vat_as_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
    if ( ! wc_prices_include_tax() )
        return $cart_item_data;

    $_product_id = $variation_id > 0 ? $variation_id : $product_id;
    $product = wc_get_product($_product_id); // The WC_Product Object

    // Save the price excluding tax as custom cart item data
    if( has_term( get_my_terms(), 'product_cat', $product_id ) ) {
        $cart_item_data['price_excl_tax'] = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_price() ) );
    }

    return $cart_item_data;
}

// Changing conditionally specific items tax rate
add_action( 'woocommerce_before_calculate_totals', 'zero_tax_items_based_on_invoice_choice', 30, 1 );
function zero_tax_items_based_on_invoice_choice( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Check for product categories and enabled "billing_timologio" field
        if( has_term( get_my_terms(), 'product_cat', $cart_item['product_id'] ) && WC()->session->get('billing_timologio') ){
            // Set the item tax rate to "Zero rate"
            $cart_item['data']->set_tax_class('Zero Rate');

            // Set price excluding taxes
            if( isset($cart_item['price_excl_tax']) ){
                $cart_item['data']->set_price($cart_item['price_excl_tax']);
            }
        }
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

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

在收到订单,订单视图和(管理)订单编辑页面上,当客户选择是"时,特定商品的税费将设置为零费率" .用于帐单" (ΤΙΜΟΛΟΓΙΟ).

On order received, order view and (admin) order edit pages, the tax will be set to "Zero Rate" for the specific items when customer has selected "Yes" for "BILLING INVOICE" (ΤΙΜΟΛΟΓΙΟ).

定义的产品类别中剩余的相关产品价格这次将具有不含税的正确价格

The related product price remaining to the defined product categories will have this time the correct price excluding taxes


原始答案.

我似乎有2个错误:

  • 第一个是$product->id,将替换为$product->get_id()
  • 第二,您需要将内部函数放在外部
  • The first is $product->id to be replaced by $product->get_id()
  • The second, you need to put the internal function outside

还可以简化代码.

没有任何保证,因为您的代码无法真正测试,请尝试以下方法:

Without any guaranty as your code can't be tested for real, try this instead:

add_action( 'woocommerce_checkout_update_order_review', 'tfwc_taxexempt_checkout_based_invoice_choice' ); 
function tfwc_taxexempt_checkout_based_invoice_choice( $post_data ) {
    WC()->customer->set_is_vat_exempt( false );
    
    parse_str($post_data);
    
    if ( $billing_timologio == 'Y' ) 
        add_filter( 'woocommerce_product_get_tax_class', 'wc_diff_rate_for_user', 1, 2 );
    }
}

function wc_diff_rate_for_user( $tax_class, $product ) {
    // Checking for specific product categories
    if( has_term( array(228,231,222), 'product_cat', $product->get_id() ) ) {
        return 'Zero Rate';
    }
    return $tax_class;
}

我希望它能起作用.

这篇关于在Woocommerce结帐时有条件地禁用特定产品类别的增值税的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆