如果他们的购物篮中没有特定的产品类别,我想阻止任何客户前进到结账处。我还想通过错误消息告诉他们他们需要添加某个产品。我找到了一些代码,但无法正常工作。我已将它作为代码片段添加到我的 Wordpress 安装中,但可惜它不起作用,即使我打开了调试,也没有错误消息。这是我在 Github 中找到的可能需要修改才能使其工作的代码:
function sv_wc_prevent_checkout_for_category() {
// set the slug of the category for which we disallow checkout
$category = 'sibling';
// get the product category
$product_cat = get_term_by( 'slug', $category, 'product_cat' );
// sanity check to prevent fatals if the term doesn't exist
if ( is_wp_error( $product_cat ) ) {
return;
}
$category_name = '<a href="' . get_term_link( $category, 'product_cat' ) . '">' . $product_cat->name . '</a>';
// check if this category is the only thing in …Run Code Online (Sandbox Code Playgroud)