Hide VAT in woocommerce checkout page based on location and radio button selection b2c & b2b - woocommerce

** I am missing how to add the conditional radio button to enable and disable the VAT message in the total cart **
function extempt_vat_for_non_eu_firms( $post_data ) {
WC()->customer->set_is_vat_exempt( false );
parse_str( $post_data, $output );
$countries_without_vat = array('DE','ES','FR');
if(in_array($output['billing_country'], $countries_without_vat)) {
WC()->customer->set_is_vat_exempt( true );
}
}
add_action( 'woocommerce_checkout_update_order_review', extempt_vat_for_non_eu_firms' );

Related

if 'woocommerce_add_to_cart_validation' filter hook return false, there is redirection, how prevent redirection?

I use following filter hook to prevent add new product to cart if number of items in cart is 2. i.e. prevent add more than two items to cart.
add_filter( 'woocommerce_add_to_cart_validation', 'call_back_fn', 1, 3 );
function call_back_fn( $passed, $product_id, $quantity ) {
if( count(WC()->cart->get_cart()) >= 2 ){
wc_add_notice( __( "cart item limitation", "woocommerce" ), "error" );
$passed = false;
}
return $passed;
}
but when condition is true, when click 'add to cart' in shop page it redirects to that product single page and show notification. How prevent this and show notice in shop page?
Thanks

Hide a checkout custom required checkbox field based on chosen country in WooCommerce

I have the following scenario:
If a customer is outside Canada, I need to show a checkbox that verifying he is out of the country for tax purposes, and to hide this checkbox and remove its requirement when the customer has selected Canada as a country.
Looked for many solutions, I found this one useful and working, if I select Canada, the check box gets hidden, however the requirement still there, and the customer can't proceed further.
Also, if he is a returning customer, and the system pulled Canada automatically, the checkbox shows unless he changes the country and goes back to Canada.
**What's missing from the code? **
Remove checkbox requirement when Canada is selected
Hide the checkbox on page load and only show after country selection if it was not Canada.
May you please help with this?
(If other country is selected)
(If Canada is selected)
add_action( 'woocommerce_review_order_before_submit', 'bt_add_tax_verification', 10 );
/**
* Add WooCommerce additional Checkbox checkout field
*/
function bt_add_tax_verification() {
woocommerce_form_field( 'tax_verification', array( // CSS ID
'type' => 'checkbox',
'class' => array('form-row mycheckbox'), // CSS Class
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true, // Mandatory or Optional
'label' => 'I certify that I am not resident in Canada for purposes of the Excise Tax Act and I am not registered under that Act. Where applicable, I agree to advise RxCourse Institute Inc, 838 Silverthorn Mill Ave, Mississauga ON L5W 1B1 in the event there is any change to my residence status or should I become registered for the purposes of the Excise Tax Act', // Label and Link
));
}
add_action( 'woocommerce_checkout_process', 'bt_add_tax_verification_warning' );
/**
* Alert if checkbox not checked
*/
function bt_add_tax_verification_warning() {
if ( ! (int) isset( $_POST['tax_verification'] ) ) {
wc_add_notice( __( 'Please verify that you are not a Canadian resident by selecting the checkbox' ), 'error' );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'bt_checkout_field_order_meta_db' );
/**
* Add custom field as order meta with field value to database
*/
function bt_checkout_field_order_meta_db( $order_id ) {
if ( ! empty( $_POST['tax_verification'] ) ) {
update_post_meta( $order_id, 'tax_verification', sanitize_text_field( $_POST['tax_verification'] ) );
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'bt_checkout_field_display_admin_order_meta', 10, 1 );
/**
* Display field value on the backend WooCOmmerce order
*/
function bt_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Checkout Checkbox Label').':</strong> ' . get_post_meta( $order->get_id(), 'tax_verification', true ) . '<p>';
}
add_filter('woocommerce_email_order_meta_keys', 'bt_custom_order_meta_email');
function bt_custom_order_meta_email( $keys ) {
$keys[] = 'tax_verification'; // This will look for a custom field called 'tax_verification' and add it to WooCommerce emails
return $keys;
}
add_filter( 'woocommerce_email_order_meta_fields', 'bt_woocommerce_email_order_meta_fields', 10, 3 );
/**
* Show hide base on country selection
*/
add_action( 'woocommerce_after_checkout_form', 'woo_conditionally_hide_show_checkout_field', 9999 );
function woo_conditionally_hide_show_checkout_field() {
wc_enqueue_js( "
// On page load
jQuery(document).ready(function() {
// var country_code = jQuery(this).val();
var country_name = jQuery('#billing_country option:selected').text().toLowerCase();
show_hide_fields(country_name);
});
// On change country dropdown
jQuery(document).on('change','#billing_country',function() {
var country_name = jQuery('option:selected',this).text().toLowerCase();
show_hide_fields(country_name);
});
function show_hide_fields(country){
if (country == 'canada') {
jQuery('#tax_verification_field').hide();
} else {
jQuery('#tax_verification_field').show();
}
}
");
}
add_action( 'woocommerce_checkout_process', 'bt_add_tax_verification_warning' );
/**
* Alert if checkbox not checked
*/
function bt_add_tax_verification_warning() {
if( isset($_POST['billing_country']) && 'CA' !== $_POST['billing_country'] ){
if ( ! (int) isset( $_POST['tax_verification'] ) ) {
wc_add_notice( __( 'Please verify that you are not a Canadian resident by selecting the checkbox' ), 'error' );
}
}
}
Modify the validation function to check if Country is CA

Hide order review based on cart items total in WooCommerce

how Hide order review based on cart items total in WooCommerce ?
i found this code, but i need hide review based on cart items
add_filter( 'woocommerce_cart_needs_shipping', 'show_hide_shipping_methods' );
function show_hide_shipping_methods( $needs_shipping ) {
$cart_items_total = WC()->cart->get_cart_contents_total();
if ( $cart_items_total < 40 ) {
$needs_shipping = false;
// Optional: Enable shipping address form
add_filter('woocommerce_cart_needs_shipping_address', '__return_true' );
}
return $needs_shipping;
}
Do you mean this?
// Remove on checkout page
function remove_checkout_totals() {
$cart_items_total = WC()->cart->get_cart_contents_total();
if ( $cart_items_total < 40 ) {
// Remove cart totals block
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
}
}
add_action( 'woocommerce_checkout_order_review', 'remove_checkout_totals', 1 );

Remove selected checkout fields with woocommerce depending on shipping method

Hello so I'm trying to figure out how to remove some billing fields using woocommerce checkout depending on the shipping method selected. So with this code I'm trying to unset the billing address, billing city, billing state and billing postcode when the customer selects local shipping but this code isn't working. Any help would be appreciated.
add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');
function xa_remove_billing_checkout_fields($fields) {
$shipping_method ='local_pickup:1'; // Set the desired shipping method to hide the checkout field(s).
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == $shipping_method) {
unset($fields['billing']['billing_address_1']); // Add/change filed name to be hide
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_state']);
unset($fields['billing']['billing_postcode']);
}
return $fields;
}
Here's how I would go about solving this problem.
This will involve php, css, and javascript (jQuery).
PHP
add_filter( 'woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields' );
function xa_remove_billing_checkout_fields( $fields ) {
// change below for the method
$shipping_method ='local_pickup:1';
// change below for the list of fields
$hide_fields = array( 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_postcode' );
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
// uncomment below line and reload checkout page to check current $chosen_methods
// print_r($chosen_methods);
$chosen_shipping = $chosen_methods[0];
foreach($hide_fields as $field ) {
if ($chosen_shipping == $shipping_method) {
$fields['billing'][$field]['required'] = false;
$fields['billing'][$field]['class'][] = 'hide';
}
$fields['billing'][$field]['class'][] = 'billing-dynamic';
}
return $fields;
}
Instead of unsetting the fields, we will just alter it's requiredness.
That means, if the chosen method is the one we want to check, we will not make it required. Then we will add a hide class. With this, we can hide these fields using css. And woocommerce will not throw an error that it is required. Using jQuery, we can show/hide these fields. So if we unset it on the first run, there's nothing to show because the fields are not there in the first place and with that the page needs to reload.
So here's the javascript and the css part.
add_action( 'wp_footer', 'cart_update_script', 999 );
function cart_update_script() {
if (is_checkout()) :
?>
<style>
.hide {display: none!important;}
</style>
<script>
jQuery( function( $ ) {
// woocommerce_params is required to continue, ensure the object exists
if ( typeof woocommerce_params === 'undefined' ) {
return false;
}
$(document).on( 'change', '#shipping_method input[type="radio"]', function() {
// change local_pickup:1 accordingly
$('.billing-dynamic').toggleClass('hide', this.value == 'local_pickup:1');
});
});
</script>
<?php
endif;
}

Adding a hook to "Place order" button in woocommerce

When the user gets to the checkout, there is a button , the "Place order" button at the bottom of the form. I have been trying to add a hook to this button in woocommerce, but I don't seem to find the correct one, I have tried woocommerce_checkout_place_order... but it doesnt do anything.
function my_function() {
//write function
}
add_action( "woocommerce_order_status_pending", "my_function");
Thanks in advance!
You need this hook woocommerce_review_order_after_submit. It will execute any function you hook to it just after the submit area. with this hook you can add some html on the checkout page after the submit button.
But if you need to call a function after the user have pressed the "Place order" button - use woocommerce_checkout_order_processed. This one will hook you just after the order was created so you can use the freshly generated order details:
add_action( 'woocommerce_checkout_order_processed', 'is_express_delivery', 1, 1 );
function is_express_delivery( $order_id ){
$order = new WC_Order( $order_id );
//something else
}
You may check this site for some more hooks you might use on the checkout page.
## I USED THIS CODE FOR ADDING DELIVERY CHARGES DEPENDING UPON THE CART SUBTOTAL AND SOME POST FIELDS ##
function action_woocommerce_checkout_process($wccs_custom_checkout_field_pro_process )
{
global $woocommerce;
//Add Fuel Surcharge & CAF
function woo_add_cart_fee() {
global $woocommerce;
if ( WC()->cart->cart_contents_total < 1500 &&
$_POST['delivery_type']=='Pick Up') {
$fuel_surchargeandCAF = get_option( 'fuel_surchargeandCAF',
70 );
WC()->cart->add_fee( __('Delivery Charges', 'woocommerce'),
$fuel_surchargeandCAF, TRUE, '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
};
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10,
1 );

Resources