Hi i working in page with virtual product i need to do this

so this i want to do i want to have a checkbox with name "Personal" so when click hide right way the company name adress etc also i want to add tax 19% and when it's no click add tax zero

i have used this code down i don't know how get value for my checkbox so when it's True i add tax 19% and hide name company , adress,phone , etc and when it's False tax be Zero

also another think how i can get the selection country and check if in europe and how i get this value

EDIT: if possible to add the checkbox only when country i'ts not in europe,

please can anyone help me on this , thank you

i attach some image to understand better

When it's Check http://i.imgur.com/jwH2i2L.png

When it's Not http://i.imgur.com/zyB9elJ.png

/**
 * Add checkbox field to the checkout
 **/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

    woocommerce_form_field( 'my_checkbox', array(
        'type'          => 'checkbox',
        'class'         => array('input-checkbox'),
        'label'         => __('Personal'),
        'required'  => false,
        ), $checkout->get_value( 'my_checkbox' ));
}

/**
 * Process the checkout
 **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error.
    if (!$_POST['my_checkbox'])
    {
         $woocommerce->add_error( __('Please agree to my checkbox.') ); 
    }
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_checkbox']) update_post_meta( $order_id, 'My Checkbox', esc_attr($_POST['my_checkbox']));


/**
 * Make Tax Zero When it's my_checkbox False dosen't work again :(
 **/

add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );
function wc_diff_rate_for_user( $tax_class, $product ) {
    if (!$_POST['my_checkbox']) {
        $tax_class = 'Zero Rate';
    }
    return $tax_class;
}

/**
 * hide filters when checkbox my_checkbox it's True dosen't work again :(
 **/

add_filter( 'woocommerce_checkout_fields' , 'woo_remove_billing_checkout_fields' );
function woo_remove_billing_checkout_fields( $fields ) {

    if($_POST['my_checkbox']) {
        unset($fields['billing']['billing_company']);
        unset($fields['billing']['billing_address_1']);
        unset($fields['billing']['billing_address_2']);
        unset($fields['billing']['billing_city']);
        unset($fields['billing']['billing_postcode']);
        unset($fields['billing']['billing_country']);
        unset($fields['billing']['billing_state']);
        unset($fields['billing']['billing_phone']);
        unset($fields['order']['order_comments']);
        unset($fields['billing']['billing_address_2']);
        unset($fields['billing']['billing_postcode']);
        unset($fields['billing']['billing_company']);
        unset($fields['billing']['billing_city']);
    }

    return $fields;
}

}

closed as off-topic by Michael, Mark Kaplun, Mayeenul Islam, kaiser Apr 5 '16 at 20:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • If i understand and i guess, that you want also to solve related problems of EU Vat tax and EU Vat company number. Why first you don't use this excellent and free plugin: WooCommerce EU VAT Assistant. That will solve for you the applying tax problem you have. Otherwise it's going to be very difficult to handle the problem (is very complex) and european law very strict. Once set and done, come back to update this post. – LoicTheAztec Apr 1 '16 at 23:37
  • Hi thank you for reply, i know this plugin already . but this i need if it's the user it's company don't have tax but if it's personal have 19 % tax in europe if inside or outside , last it's only one country i have like cypros need pay alwasy tax 19% if company or personal , the problem it's the plugin you say don't have something like this , can i do this with this plugin or threre is anothe can do this ? thank you – kiriaki Apr 2 '16 at 16:49
  • You have to be more clear, when you write, even if your native language is not english (I am french personally). It's very difficult to understand what you try to ask. I have made some clarifications for you in the answer below. What you are trying to do is a bit more complicated than you expect (see below). – LoicTheAztec Apr 2 '16 at 18:45
  • Where is the javascript you use ? In which function (or where) you are defining european countries? UPDATE YOUR CODE PLEASE. Correct grammar, mistakes and try to be more clear. Like this, people will answer you – LoicTheAztec Apr 4 '16 at 16:23
up vote 2 down vote accepted

CLARIFICATION FOR ALL :

You use virtual products on your shop.

On checkout page, you have created and set an additional checkbox for european VAT purpose.

There is 3 cases related to this checkbox.

  1. Checkbox is hidden (default)
  2. Checkbox is visible when selected country is an european country:
    • Checkbox disabled (default) => set the VAT value to ‘0’ (vat is not applied).
    • Checkbox enabled:
      1. VAT is applied normally
      2. Hide (unset) all fields in billing details except email field and country selector.

WHAT IS NOT WORKING

Your php functions related to checkbox detection state, because you use $_POST['my_checkbox'] but you don't post (submit) anything, you have just an enable/disable state on a form item.

WHAT DO YOU NEED TO DO (not done yet) :

  1. Define the european countries => Visible checkbox state
  2. You need to use javascript/jQuery .select() form event for detection and Ajax in javascript and wordpress.
    Read this article: 5 TIPS FOR USING AJAX IN WORDPRESS. You can search and find a lot of related stuff to wordpress ajax and form element detection state over this forum threads.
  • Hi, thank you very much for the reply i make a java query to control the fields hide,show etc, the problem i have now it's when i change the user field to select company or personal how i connect the woocormercial to make tax zero or 19 % ? i have make already function in Jquery i have my_tax(tax) ? can write simple example on this please thank you – kiriaki Apr 4 '16 at 13:40
  • First update your question above with what you have done, the code… After you need to: 1. Use wp_localize_script() 2. Use admin-ajax.php with add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' ); add_action( 'wp_ajax_myajax-submit', 'myajax_submit' ); function myajax_submit() {… …} 3. your Jquery detect function … You can read: Using AJAX With PHP on Your WordPress Site Without a Plugin – LoicTheAztec Apr 4 '16 at 16:04
  • You have also this nice tutorial How to use Ajax with WordPress. Learn, code and try different things. Once done, come back and UDATE YOUR QUESTION (very important) with what you have done, include all your different code and try to be very conscientious and clear. – LoicTheAztec Apr 4 '16 at 16:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.