gpt4 book ai didi

php - WooCommerce 产品 : Allow backorders only for specific user roles

转载 作者:行者123 更新时间:2023-12-04 14:59:04 31 4
gpt4 key购买 nike

我想允许特定客户角色的延期交货。我写的代码是:

// BackOrder Allow Part #1
add_filter( 'woocommerce_product_backorders_allowed', 'woocommerce_product_backorders_allowed', 10, 3 );
function woocommerce_product_backorders_allowed( $backorder_allowed, $product_id, $product ){
if ( current_user_can('administrator') ) {
$backorder_allowed = true;
} else {
$backorder_allowed = false;
}

return $backorder_allowed;
}

// BackOrder Allow Part #2
add_filter( 'woocommerce_product_get_stock_status', 'woocommerce_product_get_stock_status', 10, 2 );
function woocommerce_product_get_stock_status( $stock_status, $product ) {
if ( current_user_can('administrator') ) {
return 'onbackorder';
}
else {
return 'outofstock';
}

}

这个给出的结果似乎只适用于简单的产品。我如何为其他产品类型工作。我需要为简单、可变和 yith-bundle 工作。任何帮助将不胜感激!谢谢!

最佳答案

您的代码中存在一些错误和缺失。请改用以下内容:

// Custom conditional function targeting specific user roles
function is_allowed_user_role() {
$targeted_roles = array('administrator', 'shop_manager'); // Here define your targeted user roles
return (bool) array_intersect( wp_get_current_user()->roles, $targeted_roles );
}

add_filter( 'woocommerce_product_get_stock_status', 'filter_product_stock_status' );
add_filter( 'woocommerce_product_variation_get_stock_status', 'filter_product_stock_status' );
function filter_product_stock_status( $stock_status ) {
if ( ! is_allowed_user_role() && 'onbackorder' === $stock_status ) {
$stock_status = 'outofstock';
}
return $stock_status;
}

add_filter( 'woocommerce_product_get_backorders', 'filter_product_get_backorders' );
add_filter( 'woocommerce_product_variation_get_backorders', 'filter_product_get_backorders' );
function filter_product_get_backorders( $backorders ){
return is_allowed_user_role() ? $backorders : 'no';
}

add_filter( 'woocommerce_product_backorders_allowed', 'filter_product_backorders_allowed', 10, 3 );
function filter_product_backorders_allowed( $allowed, $product_id, $product ){
return is_allowed_user_role() ? $allowed : false;
}

代码进入事件子主题(或事件主题)的 functions.php 文件。经过测试并有效。

关于php - WooCommerce 产品 : Allow backorders only for specific user roles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67319823/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com