{Name (First):7.3} requested the following code from my WordPress AI Code Generator.
add a checkbox on the woocommerce checkout page for "solar tax exemption"
If the "solar tax exemption" box is checked during checkout, change the tax class of items in the category "solar" to "zero tax"
All items that are not in the category "solar" should remain in the default tax class.
The robot generated the following code:
“`php
function nm_add_solar_tax_checkbox( $checkout ) {
echo '<div id="solar_tax_checkbox"><h3>'.__('Solar Tax Exemption').'</h3>';
woocommerce_form_field('solar_tax_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('I am eligible for solar tax exemption'),
), $checkout->get_value('solar_tax_checkbox'));
echo '</div>';
}
add_action('woocommerce_after_order_notes', 'nm_add_solar_tax_checkbox');
function nm_add_solar_tax_checkbox_update_order_meta( $order_id ) {
if ($_POST['solar_tax_checkbox']) update_post_meta($order_id, 'Solar Tax Exemption', esc_attr($_POST['solar_tax_checkbox']));
}
add_action('woocommerce_checkout_update_order_meta', 'nm_add_solar_tax_checkbox_update_order_meta');
function nm_set_zero_tax_for_solar_item($cart_object) {
if (isset($_POST['solar_tax_checkbox'])) {
foreach (WC()->cart->cart_contents as $key => $value) {
$product_categories = wp_get_post_terms($value['product_id'],'product_cat');
foreach ($product_categories as $category) {
if ($category->slug == 'solar') {
$value['data']->set_tax_class('zero-rate');
}
}
}
}
}
add_action('woocommerce_before_calculate_totals', 'nm_set_zero_tax_for_solar_item', 1);
“`
This might not be the perfect code because the WooCommerce tax system might work a bit differently than products rate in general, but you are free to modify this according to your needs.
This snippet adds a checkbox to the WooCommerce checkout for a solar tax exemption. If the checkbox is enabled, it changes the tax class of all products in the 'solar' category to 'zero tax'. Any product not in that category stays at the default tax rate.