Question:
How to hide custom checkboxes from WooCommerce?

Summary: 

Here we have to add a custom checkbox from WooCommerce checkout based on a product ID


Solution:

// 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>';

}


Explanation:

  • The is_in_cart() function checks if any of the specified product IDs are present in the WooCommerce cart. If found, it returns true; otherwise, it returns false.

  • The checkout_shipping_form_packing_addition() function is hooked to the woocommerce_after_checkout_billing_form action with a priority of 20. This means it will be executed after the billing form is rendered.

  • Inside checkout_shipping_form_packing_addition(), it first checks if a specific product ID (15163) is present in the cart using is_in_cart(). If the product is present, it exits the function, preventing further execution.

  • If the product is not in the cart, it proceeds to echo HTML to display a custom checkbox field labeled "სამაჯური" (translation: Packing) with a description and a price.

  • The checkbox field allows users to select whether they want to purchase a packing service for an additional fee.

  • The value of the checkbox field is retrieved from the WooCommerce session using WC()->session->get('chosen_packing'), which allows the checkbox to retain its state if the user navigates away from the page and returns.


Overall, this code dynamically adds a checkbox field to the WooCommerce checkout page based on the presence of a specific product in the cart, providing users with the option to purchase an additional service.


Answered by: >LoicTheAztec

Credit:> StackOverflow


Suggested blogs:

>How to manage the Text in the container in Django?

>Activating Virtual environment in visual studio code windows 11

>Fix webapp stops working issue in Django- Python webapp

>Creating a form in Django to upload a picture from website

>Sending Audio file from Django to Vue

>How to keep all query parameters intact when changing page in Django?

>Solved: TaskList View in Django


Submit
0 Answers

What Our Clients Say About Us

Client satisfaction is our ultimate goal. Here are some kind words of our precious clients they have used to express their satisfaction with our service.

Why Choose Us ?

We endow businesses with flexible engagement models based on their unique needs. Our strength lies in state-of-the-art technology and affordable consulting services. Try us for fast POCs, full-fledged applications, or technology consulting. Always available for your service.