Ecommerce Shopify WordPress Discussion

Hide custom checkbox from WooCommerce checkout based on a product ID

I have code that adds a checkbox to the checkout page. If checked, adds additional fee to the order. add_action('woocommerce_after_checkout_billing_form', 'checkout_shipping_form_packing_addition', 20); function checkout_shipping_form_packing_addition() { $domain = 'woocommerce'; echo '<tr class="packing-select"><th>' . __('სამაჯური', $domain) . '</th><td>'; $chosen = WC()->session->get('chosen_packing'); // Add a custom checkbox field woocommerce_form_field('chosen_packing', array( 'type' => 'checkbox', 'class' => array('form-row-wide packing'), 'label' => __('სამაჯურის ღირებულება 30₾', 'woocommerce'), 'description' => __('აღნიშნული სერვისით სარგებლობისთვის საჭიროა სამაჯურის შეძენა, თუ გსურთ შეძენა, მონიშნეთ ეს ველი, თუ უკვე გაქვთ სამაჯური ან ადგილზე გადაიხდით სამაჯურის ღირებულებას დატოვეთ მონიშვნის გარეშე'), 'required' => false, ), $chosen); echo '</td></tr>'; } // jQuery - Ajax script add_action('wp_footer', 'checkout_shipping_packing_script'); function checkout_shipping_packing_script() { // Only checkout page if (is_checkout() && !is_wc_endpoint_url()) : ?> <script type="text/javascript"> jQuery(function($) { $('form.checkout').on('change', 'input#chosen_packing', function() { $.ajax({ type: 'POST', url: wc_checkout_params.ajax_url, data: { 'action': 'woo_get_ajax_data', 'packing': $(this).prop('checked') ? 1 : 0, }, success: function(result) { $('body').trigger('update_checkout'); console.log('response: ' + result); // just for testing | TO BE REMOVED }, error: function(error) { console.log(error); // just for testing | TO BE REMOVED } }); }); }); </script> <?php else: WC()->session->__unset('chosen_packing'); endif; } // Php Ajax (Receiving request and saving to WC session) add_action('wp_ajax_woo_get_ajax_data', 'woo_get_ajax_data'); add_action('wp_ajax_nopriv_woo_get_ajax_data', 'woo_get_ajax_data'); function woo_get_ajax_data() { if (isset($_POST['packing'])) { $packing = sanitize_key($_POST['packing']); WC()->session->set('chosen_packing', $packing); echo json_encode($packing); } die(); // Alway at the end (to avoid server error 500) } // Add a custom dynamic packaging fee add_action('woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1); function add_packaging_fee($cart) { if (is_admin() && !defined('DOING_AJAX')) return; if( WC()->session->get('chosen_packing') ){ $label = __('სამაჯური', 'woocommerce'); $cost = 30; $cart->add_fee($label, $cost); } } I tried to use the following code to hide the checkbox when a specific product is in the cart. But it doesn't work. This feature works for other billing fields but does not hide this particular checkbox. add_filter( 'woocommerce_checkout_fields' , 'disabling' ); function disabling($fields){ $product_id = array(15163); //product id which would trigger $in_cart = false; foreach( WC()->cart->get_cart() as $cart_item ) { $product_in_cart = $cart_item['product_id']; if ( $product_in_cart === $product_id ) $in_cart = true; // checks if the product is in cart } if ( $in_cart ){ unset($fields['billing']['chosen_packing']); unset($fields['shipping']['chosen_packing']); return $fields; } else { return $fields; } }
Replace your first function with the following, to hide the checkbox if one of the specific product(s) is in cart: // Conditional function: Checks if one of the products is in cart function is_in_cart( $products_ids ) { foreach( WC()->cart->get_cart() as $item ) { if ( in_array($item['product_id'], $products_ids) ) return true; // Exit } return false; } add_action('woocommerce_after_checkout_billing_form', 'checkout_shipping_form_packing_addition', 20); function checkout_shipping_form_packing_addition() { // Defined products that hide the checkout field if ( is_in_cart( array(15163) ) ) return; echo '<tr class="packing-select"><th>' . __('სამაჯური', 'woocommerce') . '</th><td>'; // Add a custom checkbox field woocommerce_form_field('chosen_packing', array( 'type' => 'checkbox', 'class' => array('form-row-wide packing'), 'label' => __('სამაჯურის ღირებულება 30₾', 'woocommerce'), 'description' => __('აღნიშნული სერვისით სარგებლობისთვის საჭიროა სამაჯურის შეძენა, თუ გსურთ შეძენა, მონიშნეთ ეს ველი, თუ უკვე გაქვთ სამაჯური ან ადგილზე გადაიხდით სამაჯურის ღირებულებას დატოვეთ მონიშვნის გარეშე', 'woocommerce'), 'required' => false, ), WC()->session->get('chosen_packing') ); echo '</td></tr>'; } It should work.

February 17, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.