Home > Blockchain >  Woocommerce - show checkbox at checkout for certain products and/or categories
Woocommerce - show checkbox at checkout for certain products and/or categories

Time:08-18

I want to show a custom checkbox during checkout that will only show for certain SKUs or Product categories. I already have this code that shows the checkbox on all checkout pages

add_action( 'woocommerce_review_order_before_submit', 'bt_add_checkout_checkbox', 10 );
/**
 * Add WooCommerce additional Checkbox checkout field
 */
function bt_add_checkout_checkbox() {
   
    woocommerce_form_field( 'checkout_checkbox', 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'         => 'Custom label', // Label and Link
    ));    
}

add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
/**
 * Alert if checkbox not checked
 */ 
function bt_add_checkout_checkbox_warning() {
    if ( ! (int) isset( $_POST['checkout_checkbox'] ) ) {
        wc_add_notice( __( 'Please acknowledge the Checkbox' ), 'error' );
    }
}

I don't know much about PHP but I think I must use $cart_item and $product within a foreach loop and an if statement but I'm quite lost with this part, my logic says it would be something like this:

function bt_add_checkout_checkbox() {
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
        $product = $cart_item['data'];
        $sku = WC()->cart->get_sku($product);
        if ($sku == 'SA300ARS'){
               
            woocommerce_form_field( 'checkout_checkbox', 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 acknowledge that my product can take up to 24 hours to be delivered.  <a href="/checkout-checkbox" target="_blank" rel="noopener">(Unless the description says otherwise)</a>', // Label and Link
            )); 
        }
    }
}

CodePudding user response:

You're going in the right direction. You just need to modify the code like this:

add_action( 'woocommerce_review_order_before_submit', 'bt_add_checkout_checkbox', 10 );
/**
 * Add WooCommerce additional Checkbox checkout field
 */
function bt_add_checkout_checkbox() {
    //Check if wooCommerce is activated
    if ( class_exists( 'WooCommerce' ) ) {
  
        //Define SKUs you want to check for
        $checkSKUs = ['sku1', 'sku2', 'sku3'];

        //Grab all the SKUs in cart
        $skus = array();
        foreach( WC()->cart->get_cart() as $cart_item ) {
            array_push($skus, $cart_item['data']->get_sku());
        }

        //Check if anything matches in both
        $matchingResult = array_intersect($checkSKUs,$skus);
        if (count($matchingResult) > 0) { 

            //If at least 1 SKU matches then generate checkout field
            woocommerce_form_field( 'checkout_checkbox', 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 acknowledge that my product can take up to 24 hours to be delivered.  <a href="/checkout-checkbox" target="_blank" rel="noopener">(Unless the description says otherwise)</a>', // Label and Link
            ));
        } 
    }   
}

   add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
    /**
     * Alert if checkbox not checked
     */ 
    function bt_add_checkout_checkbox_warning() {
        if ( ! (int) isset( $_POST['checkout_checkbox'] ) ) {
            wc_add_notice( __( 'Please acknowledge the Checkbox' ), 'error' );
        }
    }

I believe this should work. If not then let me know. I've added comments for your understanding.

  • Related