admin管理员组文章数量:1122832
I have the following code that set a global download limit and expiry, for all WooCommerce downloadable products:
//FOR VARIATION DOWNLOADABLE PRODUCTS
// set 3 downloads for all variation downloadable products
add_filter('woocommerce_product_variation_get_download_limit', function ($val, $obj) {
return $val <= 0 ? 3 : $val;
}, 30, 2);
// set 3 days expiration for all variation downloadable products
add_filter('woocommerce_product_variation_get_download_expiry', function ($val, $obj) {
return $val <= 0 ? 3 : $val;
}, 30, 2);
//FOR SIMPLE DOWNLOADABLE PRODUCTS
// set 3 downloads for all simple downloadable products
add_filter('woocommerce_product_get_download_limit', function ($val, $obj) {
return $val <= 0 ? 3 : $val;
}, 30, 2);
// set 3 days expiration for all simple downloadable products
add_filter('woocommerce_product_get_download_expiry', function ($val, $obj) {
return $val <= 0 ? 3 : $val;
}, 30, 2);
MY PROBLEM: I don't want to set the download limit and expiry globally (for all downloadable products), but only for specific product categories instead.
Any help is appreciated.
I have the following code that set a global download limit and expiry, for all WooCommerce downloadable products:
//FOR VARIATION DOWNLOADABLE PRODUCTS
// set 3 downloads for all variation downloadable products
add_filter('woocommerce_product_variation_get_download_limit', function ($val, $obj) {
return $val <= 0 ? 3 : $val;
}, 30, 2);
// set 3 days expiration for all variation downloadable products
add_filter('woocommerce_product_variation_get_download_expiry', function ($val, $obj) {
return $val <= 0 ? 3 : $val;
}, 30, 2);
//FOR SIMPLE DOWNLOADABLE PRODUCTS
// set 3 downloads for all simple downloadable products
add_filter('woocommerce_product_get_download_limit', function ($val, $obj) {
return $val <= 0 ? 3 : $val;
}, 30, 2);
// set 3 days expiration for all simple downloadable products
add_filter('woocommerce_product_get_download_expiry', function ($val, $obj) {
return $val <= 0 ? 3 : $val;
}, 30, 2);
MY PROBLEM: I don't want to set the download limit and expiry globally (for all downloadable products), but only for specific product categories instead.
Any help is appreciated.
Share Improve this question edited Sep 28, 2019 at 13:43 LoicTheAztec 3,38117 silver badges24 bronze badges asked Sep 28, 2019 at 9:42 Joe TitusJoe Titus 391 silver badge6 bronze badges 1- What if you want to set the download limit based on the user type? – Paris Hall Commented Sep 24, 2020 at 15:44
1 Answer
Reset to default 2You can use WordPress conditional function has_term()
to target specific product categories this way:
// Set a different Download limit for specific downloadable products
add_filter('woocommerce_product_get_download_limit', 'product_get_download_limit_filter_callback', 30, 2 ); // Simple
add_filter('woocommerce_product_variation_get_download_limit', 'product_get_download_limit_filter_callback', 30, 2 ); // Variation
function product_get_download_limit_filter_callback( $value, $product ) {
$categories = array( 'action', 'adventure' ); // <== HERE define your product categories (terms ids, slugs or names)
if( has_term( $categories, 'product_cat', $product->get_id() ) && $value <= 0 ) {
$value = 3;
}
return $value;
}
// Set a different Download expiration for specific downloadable products
add_filter('woocommerce_product_get_download_expiry', 'product_get_download_expiry_filter_callback', 30, 2 ); // Simple
add_filter('woocommerce_product_variation_get_download_expiry', 'product_get_download_expiry_filter_callback', 30, 2 ); // Variation
function product_get_download_expiry_filter_callback( $value, $product ) {
$categories = array( 'action', 'adventure' ); // <== HERE define your product categories (terms ids, slugs or names)
if( has_term( $categories, 'product_cat', $product->get_id() ) && $value <= 0 ) {
$value = 3;
}
return $value;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.
版权声明:本文标题:custom taxonomy - Set download limit and expiry for WooCommerce downloadable products in specific categories 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302214a1931454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论