add_action( 'woocommerce_new_product', 'on_product_save', 10, 1 );
add_action( 'woocommerce_update_product', 'on_product_save', 10, 1 );
function on_product_save( $product_id ) {
$product = wc_get_product( $product_id );
// do something with this product
}
但我不知道如何向用户显示管理消息并在“错误”中退出保存,...任何人有任何想法吗?谢谢!!
try认为与众不同……
// Conditional function: Check if new simple product is allowed (the limit set to 30)
function is_new_simple_product_allowed( $limit = 30 ) {
$product_ids = get_posts(['post_type' => 'product', 'post_status' => 'publish', 'numberposts' => -1, 'fields' => 'ids']);
return count($product_ids) < $limit;
}
然后我们在两个以下功能中使用该条件函数:
1)。在管理员新产品页面上显示错误通知:
add_action( 'admin_notices', 'sample_admin_notice__success' );
function sample_admin_notice__success() {
global $pagenow, $typenow;
if ( $pagenow === 'post-new.php' && $typenow === 'product' && ! is_new_simple_product_allowed() ) {
printf( '<div class="%1$s"><p>%2$s</p></div>', 'error',
esc_html__( 'You are not allowed to add a new simple product as the allowed limit for simple products has already been reached.', 'woocommerce' ) );
}
}
不允许新的简单产品时displayed错误通知:
2)。仅当使用javaScript所选产品类型“简单”时,仅在管理新产品上显示或隐藏“发布” Metabox内容:
如果您将产品类型更改为“可变产品”,则将撤回默认的metabox内容:
您也可以将重定向重定向到产品列表,例如此线程
。