问题描述
在 WooCommerce 中,我使用 WooCommerce 订阅 插件.我主要有可变订阅产品和一些简单的订阅产品.
With WooCommerce I am using WooCommerce Subscriptions plugin. I have mainly Variable Subscription products and some few simple Subscription products.
我正在使用 woocommerce_dropdown_variation_attribute_options_args
过滤器钩子来更新我的可变订阅产品的下拉属性值.
I am using woocommerce_dropdown_variation_attribute_options_args
filter hook, to update dropdown attribute values on my Variable Subscription products.
对于简单订阅产品,我想添加一些条件来允许或拒绝访问产品页面.
For Simple Subscriptions products I would like to add some conditions to allow or deny access to the product page.
所以我的问题是:我可以使用哪个挂钩来检查产品是否是简单订阅,以允许或拒绝对该产品的访问?
So my question is: Which hook could I use to check if a product is a simple subscription, to allow or deny access to the product?
任何帮助/建议将不胜感激.
Any help/suggestion will be highly appreciated.
推荐答案
您可以在 WC_Product
对象上检查产品类型以进行简单订阅,例如:
You can check product type on the WC_Product
object for simple subscription like:
if( $product->get_type() === 'subscription' ) {
// Do something
}
或
if( $product->is_type('subscription') ) {
// Do something
}
以下是避免访问简单订阅产品页面、将客户重定向到主商店页面并显示错误通知的示例用法:
And here below is an example usage that will avoid access to simple subscription product pages, redirecting customer to main shop page and displaying an error notice:
add_action('template_redirect', 'conditional_single_product_page_access');
function conditional_single_product_page_access(){
// Targeting single product pages
if ( is_product() ) {
$product = wc_get_product( get_the_ID() ); // Get the WC_Product Object
// Targeting simple subscription products
if( $product->get_type() === 'subscription' ) {
wc_add_notice( __("You are not allowed to access this product"), 'error' ); // Notice
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) ); // Redirection
exit();
}
}
}
代码位于活动子主题(或活动主题)的functions.php 文件中.经测试有效.
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
注意事项:
要定位可变订阅产品类型,请使用 slug
variable-subscription
.
To target variable subscription product type use the slug
variable-subscription
.
要定位变体订阅,产品类型标号为:subscription_variation
.
To target a variation subscription, the product type slug is: subscription_variation
.
这篇关于WooCommerce 订阅:检查产品类型是否为简单订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!