stop wooCommerce产品出版如果满足了条件

问题描述 投票:0回答:1
我想检查我的商店经理是否可以发布产品,例如,商店不能拥有30多个简单产品。 我符合此代码:

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认为与众不同……

woocommerce product message
1个回答
0
投票
为此,您首先需要检查有条件功能中发布了多少个简单产品:

// 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内容:

add_filter( 'admin_footer', 'new_product_show_or_hide_publish_metabox_js'); function new_product_show_or_hide_publish_metabox_js() { global $pagenow, $typenow; if ( $pagenow === 'post-new.php' && $typenow === 'product' && ! is_new_simple_product_allowed() ) : ?><script> jQuery( function($) { function show_hide_publish_metabox_content( productType ){ if ( productType === 'simple' ) { $('#submitpost').hide(); $( '<div id="replace-buttons" style="padding:0 10px; color:#d63638;"><p>New simple product is not allowed.</p></div>' ).insertAfter('#submitpost'); } else { $('#submitpost').show(); $('#replace-buttons').remove(); } } var productType = $('#product-type').val(); show_hide_publish_metabox_content( productType ); $('#product-type').on('change', function(){ productType = $(this).val(); show_hide_publish_metabox_content( productType ); }); }); </script><? endif; } Displayed error notice 当不允许新的简单产品(所选产品类型很简单)时,您将获得:

如果您将产品类型更改为“可变产品”,则将撤回默认的metabox内容:

您也可以将重定向重定向到产品列表,例如此线程

Normal Metabox

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.