• Resolved Wagner

    (@wagnerdp)


    I have a code that exempt the user from VAT in all products based on a checkout form, it is working fine as i am using set_is_vat_exempt() which works based on the user section. So all products will be set_is_vat_exempt(true/false) in my order review.

    Now i have a specific product which can not be exempt from VAT, it must be always taxable. I have tried to update my code to use tax_class instead but couldn’t go through due to woocommerce_checkout_update_order_review doesn’t accept tax class args.
    Any help?

    <?php
    
    /**
     * Plugin Name:       Vat Exempt
     * Plugin URI:        https://www.uccellodesigns.co.uk/
     * Description:       Based in a Zero tax WooCommerce by default, the plugin add VAT/TAX in checkout page deppending on the user option.
     * Version:           1.0
     * Requires at least: 5.8
     * Requires PHP:      7.2
     * Author:            Wagner Dantas Pereira
     * Text Domain:       Vat_Exempt
     */ 
     
    function enqueue_style() {
        
        if (is_checkout()) {
            wp_enqueue_style('default-style', plugin_dir_url( __FILE__ ) . '/css/default.css');
        }
    }
    
    add_action('wp_enqueue_scripts', 'enqueue_style');
    
    function uk_vat_exempt( $checkout ) {
        
        //Set the radio to be Vat exempt on checkout
        $checked = $checkout->get_value( 'shipping_method_tax_exempt' ) ? $checkout->get_value( 'shipping_method_tax_exempt' ) : 'true' ; 
        //Include date
        $date = date('d/m/Y');
    
        echo '<div class="header_vat_exempt">
              <h2>'.__('Are you eligible for VAT Exemption?').'</h2>';
        
        woocommerce_form_field( 'shipping_method_tax_exempt', array(
          'type'          => 'radio',
          'class'         => array('form-row-wide', 'disability_vat_exempt'),
          'options' => array('true' => 'Disability VAT Exemption','false' => 'No',),    
          'required'  => true,
          ), $checked);
    
      echo '</div>';
        
      echo '<div id="content_vat_exempt" style="display:block">
                <div class="description_vat_exempt">
                <h3>VAT Exemptions</h3>
                <p>You should complete this declaration if you are ‘chronically sick or disabled’ and the goods or services are for your own personal or domestic use. A family member or carer can complete this on your behalf if you wish. You can find out more from the Helpsheets on the <a href="https://www.gov.uk/financial-help-disabled/vat-relief" target="_blank" title="Opens in a new window" rel="noopener">GOV.UK website</a>. HMRC staff cannot advise whether or not an individual is chronically sick or disabled. A person is ‘chronically sick or disabled’ if he or she is a person:- with a physical or mental impairment which has a long term and substantial adverse effect upon his or her ability to carry out everyday activities- with a condition which the medical profession treats as a chronic sickness It does not include an elderly person who is not disabled or chronically sick or any person who is only temporarily disabled or incapacitated, such as with a broken limb. If you are unsure, you should seek guidance from your GP or other medical professional.
                </p>
                </div>';
                
                    echo '<div class="form_vat_exempt">
                            <h3>Fill out the form below</h3>';
        
                    woocommerce_form_field( 'vat_exempt_person_name', array(
                        'type'        => 'text',
                        'required'    => true,
                        'class'         => array('validate-required'),
                        'label'       => 'Name of person to which VAT exemption applies',
                    ), $checkout->get_value( 'vat_exempt_person_name' ) );
                    
                    woocommerce_form_field( 'vat_exempt_full_address', array(
                        'type'        => 'text',
                        'required'    => false,
                        'class'         => array(''),
                        'label'       => 'Full address',
                    ), $checkout->get_value( 'vat_exempt_full_address' ) );
                    
                    woocommerce_form_field( 'vat_exempt_reason', array(
                        'type'        => 'text',
                        'required'    => true,
                        'class'         => array('validate-required'),
                        'label'       => 'Reason for VAT exemption',
                    ), $checkout->get_value( 'vat_exempt_reason' ) );
                    
                    woocommerce_form_field( 'vat_exempt_signature', array(
                        'type'        => 'text',
                        'required'    => false ,
                        'class'       => array(''),
                        'label'       => 'Signed',
                    ), $checkout->get_value( 'vat_exempt_signature' ) );
                    
                    woocommerce_form_field( 'vat_exempt_date', array(
                        'type'        => 'text',
                        'required'    => false,
                        'class'         => array('vat_exempt-date-field'),
                        'label'       => 'Date',
                        'default'     => $date,
                        'custom_attributes'    => array('readonly' => true ),
                    ), $checkout->get_value( 'vat_exempt_date' ) );
                    
                    echo '</div>';
                    
        echo '</div>';
    }
    
    add_action( 'woocommerce_after_checkout_billing_form', 'uk_vat_exempt', 10, 1);
    
    /* Save vat exempt fields */
    
    function uk_vat_exempt_create_order( $order, $data ) {
        //check if $_POST has our custom fields and accordingly update meta for this order
        
        
        
            if ( isset($_POST['shipping_method_tax_exempt']) && ! empty($_POST['shipping_method_tax_exempt']) ) {
                $order->update_meta_data( 'shipping_method_tax_exempt', sanitize_text_field( $_POST['shipping_method_tax_exempt'] ) );
            }
    
            if ( isset($_POST['vat_exempt_person_name']) && ! empty($_POST['vat_exempt_person_name']) ) {
                $order->update_meta_data( 'vat_exempt_person_name', sanitize_text_field( $_POST['vat_exempt_person_name'] ) );
            } 
    
            if ( isset($_POST['vat_exempt_full_address']) && ! empty($_POST['vat_exempt_full_address']) ) {
                $order->update_meta_data( 'vat_exempt_full_address', sanitize_text_field( $_POST['vat_exempt_full_address'] ) );
            } 
    
            if ( isset($_POST['vat_exempt_reason']) && ! empty($_POST['vat_exempt_reason']) ) {
                $order->update_meta_data( 'vat_exempt_reason', sanitize_text_field( $_POST['vat_exempt_reason'] ) );
            } 
    
            if ( isset($_POST['vat_exempt_signature']) && ! empty($_POST['vat_exempt_signature']) ) {
                $order->update_meta_data( 'vat_exempt_signature', sanitize_text_field( $_POST['vat_exempt_signature'] ) );
            } 
    
            if ( isset($_POST['vat_exempt_date']) && ! empty($_POST['vat_exempt_date']) ) {
                $order->update_meta_data( 'vat_exempt_date', sanitize_text_field( $_POST['vat_exempt_date'] ) );
            } 
            
    
    }
    add_action( 'woocommerce_checkout_create_order', 'uk_vat_exempt_create_order', 10, 2 );
    
    add_action( 'woocommerce_checkout_update_order_review', 'taxexempt_checkout_update_order_review');
    function taxexempt_checkout_update_order_review( $post_data ) {
      global $woocommerce;
        
        
      //$woocommerce->customer->set_is_vat_exempt(true);
    
      parse_str($post_data);
    
        if ( isset($shipping_method_tax_exempt) && $shipping_method_tax_exempt == 'true')
            $woocommerce->customer->set_is_vat_exempt(true);
        
        
        if ( isset($shipping_method_tax_exempt) && $shipping_method_tax_exempt == 'false')
            $woocommerce->customer->set_is_vat_exempt(false);   
            
     
    }
    
    /* Function to VAT exemp Radio selection */
    add_action('wp_footer', 'custom_checkout_js_script');
    function custom_checkout_js_script() {
        
        //Include date
        $date = date('d/m/Y');
            
        if( is_checkout() && ! is_wc_endpoint_url() ) :
        ?>
        <script language="javascript">
        jQuery( function($){
            var a = 'input[name="shipping_method_tax_exempt"]:checked',
                b = 'input[name="shipping_method_tax_exempt"]';
    
            // Custom function that show hide specific field, based on radio input value
            function showHideField( value, field ){
                if ( value === 'false' ) {
                    $('#content_vat_exempt').slideUp('slow');
                    
                        $('#vat_exempt_person_name').val('');
                        $('#vat_exempt_full_address').val('');
                        $('#vat_exempt_reason').val('');
                        $('#vat_exempt_signature').val('');
                        $('#vat_exempt_date').val('');
                    
                    
                } else {
                    $('#content_vat_exempt').slideDown('slow');
                    
                    $('#vat_exempt_date').val('<?php echo $date ?>');
                }
            }
    
            // On start after DOM is loaded
            showHideField( $(a).val(), b );
    
            // On radio button change live event
            $('form.woocommerce-checkout').on('change', a, function() {
                showHideField( $(this).val(), b );
            });
        });
        </script>
        <?php
        endif;
    }
    
    /**
     * Add the Vat Exempt to order emails
     **/
    function uk_vat_exempt_meta_fields( $fields, $sent_to_admin, $order  ) {
    
        if( ! $order->get_meta( 'shipping_method_tax_exempt' ))
            
                   
            return $fields;
    
        
            if( $order->get_meta( 'shipping_method_tax_exempt' ) == 'true'){
                echo '<h4 style="color:#7ac143">'.__( 'VAT Exempt', 'woocommerce' ).'</h2>';
            }
        
            
            $fields[] = array(
                'label' => __( 'Name of person exempt from VAT', 'woocommerce' ),
                'value' => $order->get_meta( 'vat_exempt_person_name' )
            );
    
            $fields[] = array(
                'label' => __( 'Full Address', 'woocommerce' ),
                'value' => $order->get_meta( 'vat_exempt_full_address' )
            );
    
            $fields[] = array(
                'label' => __( 'Reason for VAT exemption', 'woocommerce' ),
                'value' => $order->get_meta( 'vat_exempt_reason' )
            );
    
            $fields[] = array(
                'label' => __( 'Signature', 'woocommerce' ),
                'value' => $order->get_meta( 'vat_exempt_signature' )
            );
    
            $fields[] = array(
                'label' => __( 'Date', 'woocommerce' ),
                'value' => $order->get_meta( 'vat_exempt_date' )
            );
    
        return $fields;
    }
    add_action( 'woocommerce_email_order_meta_fields', 'uk_vat_exempt_meta_fields', 20, 3 );
    
    ?>

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘VAT Exempt except for a specific product’ is closed to new replies.