WooCommerce 产品:仅允许特定用户角色的延期交货
WooCommerce products: Allow backorders only for specific user roles
我想允许特定客户角色的延期交货。
我写的代码是:
// 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 文件。已测试并有效。
我确实改变了第二个功能。如果缺货,比如 qty = 0 或 stockstatus 缺货,允许缺货
// Custom conditional fuction to target 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() && 'outofstock' === $stock_status ) {
$stock_status = 'onbackorder';
}
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;
}
我想允许特定客户角色的延期交货。 我写的代码是:
// 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 文件。已测试并有效。
我确实改变了第二个功能。如果缺货,比如 qty = 0 或 stockstatus 缺货,允许缺货
// Custom conditional fuction to target 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() && 'outofstock' === $stock_status ) {
$stock_status = 'onbackorder';
}
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;
}