简体   繁体   中英

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

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
            )); 
        }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM