lundi 23 août 2021

custom required checkbox to show up when product is in cart (woocommerce checkout)

On my wordpress/woocommerce site I am adding a new product which is a subscription item. When this specific product is in the cart, the checkout page needs to show an additional required checkbox which must be validated before customer can place his order.

Below is the code I have sofar. It makes the checkbox show up correctly when the product is in the cart, BUT it is not required to be checked before you can click on the PAY NOW button.

How can I make the checkbox required?

I have seen this: European GDPR additional checkout validation checkbox in Woocommerce and tried to put that code inside my code (after if ($in_cart) ) but I could not make it work.

Not sure how to proceed to make my below code function:

add_action( 'woocommerce_checkout_terms_and_conditions', 'custom_subscription_checkout_button' );

function custom_subscription_checkout_button() {
   $product_id = 123; // the ONE product where this checkbox needs to show up for
   $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;
   }
  
   if ( $in_cart ) {
        echo '<p class="form-row validate-required">
                <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
                <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="subscr_terms" />
                    <span class="woocommerce-terms-and-conditions-checkbox-text">I have read and agree to the <a href="#">subscription terms</a></span>&nbsp;<span class="required">*</span>
                </label>
                <input type="hidden" name="terms-field" value="1" />
            </p>';
   } 
}



Aucun commentaire:

Enregistrer un commentaire