admin管理员组文章数量:1124409
In the context of WooCommerce products does anyone know of a way to prevent a user from
Saving Draft
Updating an existing post
Publishing
Submitting for review
IF specific WooCommerce product data fields have not been filled in or any other field for that matter?
It's easy to create MetaBox.io custom fields and set them to be required, but I want to get control over the native Woo and FooEvents fields among other fields such as Post Title, Post Content, Featured Image, etc.
I’ve been playing with a “Save_Post” action hook but having some trouble with it preventing a user from doing any of the 4 actions above. I managed to get this much to show up but because of the "redirect" issue related to the save_post hook, the admin notices and the css show up when I "add a new product" which is annoying. Still, I want to be able to prevent the save until these items are filled. Once I figure this out, I think I might be able to take it further with the Woo and FooEvents fields.
In the context of WooCommerce products does anyone know of a way to prevent a user from
Saving Draft
Updating an existing post
Publishing
Submitting for review
IF specific WooCommerce product data fields have not been filled in or any other field for that matter?
It's easy to create MetaBox.io custom fields and set them to be required, but I want to get control over the native Woo and FooEvents fields among other fields such as Post Title, Post Content, Featured Image, etc.
I’ve been playing with a “Save_Post” action hook but having some trouble with it preventing a user from doing any of the 4 actions above. I managed to get this much to show up but because of the "redirect" issue related to the save_post hook, the admin notices and the css show up when I "add a new product" which is annoying. Still, I want to be able to prevent the save until these items are filled. Once I figure this out, I think I might be able to take it further with the Woo and FooEvents fields.
Share Improve this question edited Mar 4, 2024 at 22:42 Zakir Faizal asked Mar 4, 2024 at 22:39 Zakir FaizalZakir Faizal 12 bronze badges1 Answer
Reset to default 0To set required fields in woocommerce when adding a product, you will need to use hooks to enforce validation before the product is saved
// Function to validate required fields before saving product
function custom_validate_required_fields( $product_id, $product ) {
$errors = array();
// Check if Post Title is empty
if ( empty( $product->get_title() ) ) {
$errors[] = __( 'Product title is required.', 'your-text-domain' );
}
// Check if Post Content is empty
if ( empty( $product->get_description() ) ) {
$errors[] = __( 'Product content is required.', 'your-text-domain' );
}
// Check if Featured Image is empty
if ( ! has_post_thumbnail( $product_id ) ) {
$errors[] = __( 'Featured image is required.', 'your-text-domain' );
}
// If errors found, display error messages and prevent saving product
if ( ! empty( $errors ) ) {
foreach ( $errors as $error ) {
wc_add_notice( $error, 'error' );
}
return false; // Prevent saving product
}
return true; // Proceed with saving product
}
add_action( 'woocommerce_process_product_meta', 'custom_validate_required_fields', 10, 2 );
woocommerce_process_product_meta
hook fired when a product is being saved. It checks if the required fields Post Title, Post Content, and Featured Image are empty
本文标签: woocommerce offtopicMake Admin Side Fields MandatoryRequired
版权声明:本文标题:woocommerce offtopic - Make Admin Side Fields MandatoryRequired 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736629025a1945735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论