If you got on this page, you most likely you’ve watched the video on youtube and you need the following code:
add_action('woocommerce_after_add_to_cart_button', 'tuskcode_custom_question_product', 10 );
add_action('woocommerce_add_to_cart_validation', 'tuskcode_add_to_cart_validate', 20, 5 );
function tuskcode_custom_question_product(){
global $product;
$product_id = $product->get_id();
//see if raffle product or not.
$is_raffle_product = get_post_meta( $product_id, '_rpwoo_ckb', true ); //yes - no or empty
if( $is_raffle_product !== 'yes') return;
//if( $product_id != 77 ) return;
echo "<p> What's the capital city of England? ";
echo "<select name='question_raffle_product' style='font-size: 1rem; color: red;' class='custom-question'>
<option value='' selected > Select </option>
<option value='paris' > Paris </option>
<option value='madrid' > Madrid </option>
<option value='london' > London </option>
</select>";
echo "</p>";
}
function tuskcode_add_to_cart_validate( $validate, $product_id, $quantity, $variation_id = 0, $variation = null ){
if( isset( $_POST['question_raffle_product'] ) ){
$value = trim( sanitize_text_field( wp_unslash( $_POST['question_raffle_product'])) );
//no answer selected, force to select one
if( $validate && $value == '' ){
$validate = false;
wc_add_notice( 'Hey, select an answer to the question, please. Thank you.', 'error' );
}
//answer selected, but wrong one is picked.
if( $validate && $value !== 'london' ){
$validate = false;
wc_add_notice( 'Hey, work on your general knowledge', 'error' );
}
}else{
//nothing to do, this product does not have a question to answer
}
return $validate;
}