Home > other >  Show custom check box for all WooCommerce product except product ID 32
Show custom check box for all WooCommerce product except product ID 32

Time:11-07

Am using the snippet below to show a custom checkbox for all products but would like to hide/not show it if product with ID 32 is in cart. How do i modify this code to achieve that? Thanks in advance.

add_action( 'woocommerce_review_order_before_submit', 'bbloomer_add_checkout_privacy_policy', 9 );
    
function bbloomer_add_checkout_privacy_policy() {
   
woocommerce_form_field( 'privacy_policy', array(
   'type'          => 'checkbox',
   'class'         => array('form-row privacy'),
   '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,
   'label'         => 'I\'ve read and accept the <a href="/privacy-policy">Privacy Policy</a>',
)); 
   
}
   
// Show notice if customer does not tick
    
add_action( 'woocommerce_checkout_process', 'bbloomer_not_approved_privacy' );
   
function bbloomer_not_approved_privacy() {
    if ( ! (int) isset( $_POST['privacy_policy'] ) ) {
        wc_add_notice( __( 'Please acknowledge the Privacy Policy' ), 'error' );
    }
}

CodePudding user response:

Before you actually show the checkbox field, you will first have to go through the cart to see if the productID is NOT present.

Same for the validation, as it is a required field and would otherwise still be a required field, even though it is not present.

So you get:

// Function to check if a certain product ID is in cart
function is_product_in_cart() {
    // Check if product in cart
    // Multiple product IDs can be entered, separated by a comma
    $targeted_ids = array( 32, 1234, 5678 );
    
    // Flag no product in cart
    $flag = false;
    
    // WC Cart NOT null
    if ( ! is_null( WC()->cart ) ) {
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            // Check cart item for defined product Ids
            if ( in_array( $cart_item['product_id'], $targeted_ids ) ) {
                // Product is in cart
                $flag = true;
                
                // Break loop
                break;
            }
        }
    }

    return $flag;
}

// Add field 
function action_woocommerce_review_order_before_submit() {    
    // NOT true
    if ( ! is_product_in_cart() ) {
        // Add checkbox
        woocommerce_form_field( 'privacy_policy', array(
            'type'          => 'checkbox',
            'class'         => array( 'form-row privacy' ),
            '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,
            'label'         => 'I\'ve read and accept the <a href="/privacy-policy">Privacy Policy</a>',
        ));
    }
}
add_action( 'woocommerce_review_order_before_submit', 'action_woocommerce_review_order_before_submit', 9 );

// Validate
function action_woocommerce_checkout_process() {
    // NOT true
    if ( ! is_product_in_cart() ) {
        // NOT isset
        if ( ! isset( $_POST['privacy_policy'] ) ) {
            wc_add_notice( __( 'Please acknowledge the Privacy Policy', 'woocommerce' ), 'error' );
        }
    }
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );

// Save field
function action_woocommerce_checkout_create_order( $order, $data ) {
    // Isset    
    if ( isset( $_POST['privacy_policy'] ) ) {
        $order->update_meta_data( 'privacy_policy', sanitize_text_field( $_POST['privacy_policy'] ) );
    } 
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );
  • Related