Skip to content
Advertisement

Is there a way to validade if a field attribute of a product exists in Woocommerce?

I found a youtube tutorial to create a plugin in WordPress to add an input field for each product, so that the client could upload an image. You can add this field attribute to a product or not.

The code has a validation function, to check if the image was uploaded or not. If not, promps you a warning saying that you have to. The original code makes the warning active for all products, even those that don’t have the field.

I changed the code to check if the product has the field or not:

JavaScript

This is the whole code:

JavaScript

I added line 4 and && isset($title) on line 5.

**The problem: **The code works when you go to the individual product page and click to buy it. But in the general page when you click to add to cart, it does work. It let the products with the input field pass.

It should check if the product has the field attribute, if it has prompt a message to add an image, otherwise add the product to the cart, in all pages, not just the individual product page.

Advertisement

Answer

I found a solution!! Is not what I was looking for, but is a workaround. Now instead of adding to the cart, it displays a button that leads you to the individual product page, where the validation works. I found this post here and the code:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement