Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello,

    With custom code you could add an input field on the single product page via the ‘woocommerce_before_add_to_cart_button hook’ and validate the input via the ‘woocommerce_add_to_cart_validation hook’.

    The question is whether you want to add the validation to which you need to register via code or whether you also want an additional input field in the backend (for example when creating the product) so that you can already enter the code that the product must comply with.

    This of course requires the necessary coding skills, I don’t know what your experience is with this?

    Of course you can also look for a suitable (free) plugin, maybe this one?

    https://pluginrepublic.com/wordpress-plugins/woocommerce-product-add-ons-ultimate/

    Although it is paying, therefore my proposal to add it yourself via custom code.

    Regards

    Luminus Alabi

    (@luminus)

    Automattic Happiness Engineer

    Hi @zmavocet,

    The site you’ve referenced literally has no validation other than making sure that something is entered into that field.

    If you enter any series of characters whatsoever into that field, you’ll be able to add the product to the cart.

    If that’s all you require, you can go with @crslz’s suggested plugin or the WooCommerce Product Add-Ons extension.

    If you want to validate the entered code against a database, you’ll definitely need to write custom code to make that happen. I’d recommend speaking to your developer about building something for your use case.

    If you don’t have a go-to developer, we highly recommend contacting one of the services on our customizations page: https://woocommerce.com/customizations/.

    Cheers.

    Thread Starter zmavocet

    (@zmavocet)

    Hello,

    Thank you for the replies.

    I think my coding level is at a good understanding. However, I only want that input text and validation on certain single product pages of my choice. Is their any code you can help me with?

    Hello,

    With the example below you can get started to adjust this further

    /**
     * Display custom field on the front end
     */
    function my_before_add_to_cart_button() {
    	global $product;
    	$product_id = $product->get_id();
    	
    	echo $product_id;
    	
    	// Only show on page with this specific product id
    	if ( $product_id == 1 ) {
    		echo '<input type="text" name="my-field">';
    	}
    }
    add_action( 'woocommerce_before_add_to_cart_button', 'my_before_add_to_cart_button' );
    
    /**
     * Validate the text field
     */
    function my_add_to_cart_validation( $passed, $product_id, $quantity ) {
    	if( empty( $_POST['my-field'] ) ) {
    		// Fails validation
    		$passed = false;
    		wc_add_notice( __( 'Please enter a value into the text field', 'woocommerce' ), 'error' );
    	}
    	
    	return $passed;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'my_add_to_cart_validation', 10, 3 );
    Thread Starter zmavocet

    (@zmavocet)

    Hi,

    Unfortunately, I haven’t been able to get it how I want it to like. Is their some thing else you can help with maybe a plugin that supports this kind of feature?

    Luminus Alabi

    (@luminus)

    Automattic Happiness Engineer

    Hi @zmavocet,

    Both myself and @crslz have suggested plugins that you can use to achieve what you’re looking for.

    If between those plugins and the custom code that @crslz has graciously provided for you, you’re not able to get things to work, it might be time to speak to your developer about building something specific to your needs.

    If you don’t have a go-to developer, we highly recommend contacting one of the services on our customizations page: https://woocommerce.com/customizations/.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Type in a code to purchase a product’ is closed to new replies.