基于用户角色和产品类别的货到付款 (COD)
Cash on delivery (COD) based on user role and product category
我需要根据用户角色和产品类别在 WooCommerce 中自定义货到付款 (COD)。
要求:
- 默认隐藏 COD
- 当购物车中的产品属于特定类别(类别 1)时,COD 可见
- 具有角色“x”的用户必须始终看到 COD,即使他们的购物车中没有类别 1 产品。
这是我的代码尝试满足上述要求:
function disable_cod($available_gateways)
{
if (is_admin()) return $available_gateways;
$role = false;
//check whether the avaiable payment gateways have Cash on delivery and user is not logged in or he is a user with role customer
if (isset($available_gateways['cod']) && (current_user_can('customer') || !is_user_logged_in()))
{
$role = true;
}
foreach (WC()
->cart
->get_cart() as $cart_item_key => $cart_item)
{
$prod_simple = true;
// Get the WC_Product object
$product = wc_get_product($cart_item['product_id']);
// Get the product types in cart (example)
if ($product->is_product_category('X')) $prod_simple = false;
}
if ($prod_simple = $role = true) unset($available_gateways['cod']); // unset 'cod'
return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'disable_cod', 99, 1);
我的语法肯定有问题,但我希望逻辑概念是正确的?有什么建议吗?
您的代码包含一些小错误:
is_product_category()
是条件标签,在查看产品类别存档时 returns 为真。请改用 has_term()。
- 仅当未履行用户角色时才需要循环购物车。
所以你得到:
function filter_woocommerce_available_payment_gateways( $payment_gateways ) {
// Not on admin
if ( is_admin() ) return $payment_gateways;
// Initialize: flag - default true
$flag = true;
// Has certain user role
if ( current_user_can( 'certain-user-role' ) ) {
// False
$flag = false;
} else {
// Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
$categories = array( 63, 15, 'categorie-1' );
// Isset
if ( WC()->cart ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Has term (product category)
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
// False, break loop
$flag = false;
break;
}
}
}
}
// True
if ( $flag ) {
// Cod
if ( isset( $payment_gateways['cod'] ) ) {
// Remove
unset( $payment_gateways['cod'] );
}
}
return $payment_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
我需要根据用户角色和产品类别在 WooCommerce 中自定义货到付款 (COD)。
要求:
- 默认隐藏 COD
- 当购物车中的产品属于特定类别(类别 1)时,COD 可见
- 具有角色“x”的用户必须始终看到 COD,即使他们的购物车中没有类别 1 产品。
这是我的代码尝试满足上述要求:
function disable_cod($available_gateways)
{
if (is_admin()) return $available_gateways;
$role = false;
//check whether the avaiable payment gateways have Cash on delivery and user is not logged in or he is a user with role customer
if (isset($available_gateways['cod']) && (current_user_can('customer') || !is_user_logged_in()))
{
$role = true;
}
foreach (WC()
->cart
->get_cart() as $cart_item_key => $cart_item)
{
$prod_simple = true;
// Get the WC_Product object
$product = wc_get_product($cart_item['product_id']);
// Get the product types in cart (example)
if ($product->is_product_category('X')) $prod_simple = false;
}
if ($prod_simple = $role = true) unset($available_gateways['cod']); // unset 'cod'
return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'disable_cod', 99, 1);
我的语法肯定有问题,但我希望逻辑概念是正确的?有什么建议吗?
您的代码包含一些小错误:
is_product_category()
是条件标签,在查看产品类别存档时 returns 为真。请改用 has_term()。- 仅当未履行用户角色时才需要循环购物车。
所以你得到:
function filter_woocommerce_available_payment_gateways( $payment_gateways ) {
// Not on admin
if ( is_admin() ) return $payment_gateways;
// Initialize: flag - default true
$flag = true;
// Has certain user role
if ( current_user_can( 'certain-user-role' ) ) {
// False
$flag = false;
} else {
// Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
$categories = array( 63, 15, 'categorie-1' );
// Isset
if ( WC()->cart ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Has term (product category)
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
// False, break loop
$flag = false;
break;
}
}
}
}
// True
if ( $flag ) {
// Cod
if ( isset( $payment_gateways['cod'] ) ) {
// Remove
unset( $payment_gateways['cod'] );
}
}
return $payment_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );