基于用户角色和产品类别的货到付款 (COD)
我需要根据用户角色和产品类别在 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);
我肯定在语法上有问题,但我希望逻辑概念是正确的?有什么建议吗?
I need to customize cash on delivery (COD) in WooCommerce based on user role and product category.
Requirements:
- By default COD is hidden
- COD is visible when a product in cart, belongs to a certain category (category 1)
- Users who have the role "x" must always see COD, even if they don't have category 1 products in the cart.
This is my code attempt to meet the above requirements:
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);
I definitely got something wrong in the syntax, but I hope the logical concept is correct? Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码包含一些小错误:
is_product_category()
是一个条件标记,在查看产品类别存档时返回 true。请改用 has_term() 。所以你得到:
Your code contains some minor mistakes:
is_product_category()
is a conditional tag which returns true when viewing a product category archive. Use has_term() instead.So you get: