根据 WooCommerce 结账中的分类术语限制支付网关

2024-03-15

在我的 WooCommerce 商店中,仅当产品具有类别 ID“266”的特定产品类别时,我想限制并显示支付网关(支票)。现在我有了这个代码片段,但它的作用相反 - 它在结账时禁用了特定产品类别的网关:

add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_unset_gateway_by_category' );
  
function bbloomer_unset_gateway_by_category( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( ! is_checkout() ) return $available_gateways;
    $unset = false;
    $category_ids = array( 8, 37 );
    foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
        $terms = get_the_terms( $values['product_id'], 'product_cat' );    
        foreach ( $terms as $term ) {        
            if ( in_array( $term->term_id, $category_ids ) ) {
                $unset = true;
                break;
            }
        }
    }
    if ( $unset == true ) unset( $available_gateways['cheque'] );
    return $available_gateways;
}

Using has_term() https://codex.wordpress.org/Function_Reference/has_termWordPress 条件函数将简化代码,使其更加有效,如下所示:

add_filter( 'woocommerce_available_payment_gateways', 'filter_available_payment_gateways' );
function filter_available_payment_gateways( $available_gateways ) {
    // Only on checkout page
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        // Here define your product categories
        $product_categories = array( 't-shirts', 'posters' ); // Can be term names, term slugs or term ids
        $taxonomy = 'product_cat'; // For WooCommerce product category terms (or 'product_tag' for WooCommerce product tag terms)

        $payment_method     = 'cheque'; // Here define your payment method id to be removed
        $hide_payment       = false;

        // Loop through cart items
        foreach ( WC()->cart->get_cart_contents() as $item ) {
            if ( ! has_term( $product_categories, $taxonomy, $item['product_id'] ) ) {
                $hide_payment = true;
            }
        }
        
        if ( $hide_payment ) {
            unset( $available_gateways[$payment_method] );
        }
    }
    return $available_gateways;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。


改为处理产品标签

只需在代码中替换分类法'product_cat' by 'product_tag'.


也定位父产品类别

如果您需要瞄准父产品类别同样,你也必须使用它:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

add_filter( 'woocommerce_available_payment_gateways', 'filter_available_payment_gateways' );
function filter_available_payment_gateways( $available_gateways ) {
    // Only on checkout page
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        // Here define your product categories
        $product_categories = array( 't-shirts', 'posters' ); // Can be term names, term slugs or term ids
        $taxonomy = 'product_cat'; // For WooCommerce product category terms (or 'product_tag' for WooCommerce product tag terms)

        $payment_method     = 'cheque'; // Here define your payment method id to be removed
        $hide_payment       = false;

        // Loop through cart items
        foreach ( WC()->cart->get_cart_contents() as $item ) {
            if ( ! has_product_categories( $product_categories, $item['product_id'] ) ) {
                $hide_payment = true;
            }
        }
        
        if ( $hide_payment ) {
            unset( $available_gateways[$payment_method] );
        }
    }
    return $available_gateways;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据 WooCommerce 结账中的分类术语限制支付网关 的相关文章

随机推荐

  • 在 pandas 中使用 .loc 会减慢计算速度

    我有以下数据框 我想将底部 1 的值分配给新列 当我使用 loc 通知进行此计算时 使用 loc 分配大约需要 10 秒 而替代解决方案只需 2 秒 df temp pd DataFrame np random randn 10000000
  • 图像“包含”resizeMode 在本机反应中不起作用

    我正在真实的 Android 设备上使用 React Native 当创建一个非常简单的应用程序时 在主应用程序组件上仅使用以下渲染函数 render
  • 当“breaks”已经定义时,序列 x 轴标签(R,ggplot)

    我在数据上使用了scale 函数 以避免在进行混合模型时出现高相关性 现在我希望原始值出现在我的图中 所以我用以下方法反转了缩放比例x attr x scaled scale attr x scaled center 并将这些值放入我用来绘
  • 文本字段上的颤动光标位置

    我想在光标位置后附加文本 例如 当用户将光标移动到文本字段中间时 我想获取文本上的光标位置 然后在光标位置后添加文本 我尝试使用文本编辑控制器 但无法到达光标位置 如何检测文本字段上的光标位置 我用这个解决了我的问题 Get cursor
  • ggplot2中的上标和下标轴标签[重复]

    这个问题在这里已经有答案了 我需要 ggplot2 中的一个轴标签 其内容为 同化 mol CO2 m 2 s 1 其中 CO2 的 2 作为下标 2 和 1 作为上标 谢谢 你可以尝试 library ggplot2 qplot upta
  • C# Web API POST 参数 FromBody 始终为 null

    我已经在网络上搜索了几个小时 并尝试了 StackOverflow 上描述的许多不同的解决方案 我知道以前曾有人问过类似的问题 但没有一个答案或评论对我有用 问题 我有一个 NET Web API 它有一个带有一些参数的 Post 方法 其
  • Extjs - 带有子菜单的工具栏按钮菜单下拉列表。这是可能的?

    我已经完成了一个带有带有下拉菜单的按钮的工具栏 但我需要更多的子菜单级别 可以这样做吗 例子 工具栏按钮 gt 菜单 1 级 1 菜单 2 LV 1 menu 3 lv 1 gt 子菜单 1 lv 2 子菜单 2 lv 2 菜单 4 LV
  • 版本控制和测试驱动开发

    测试驱动开发的标准流程似乎是添加测试 查看它失败 编写生产代码 查看测试通过 重构 并将其全部检查到源代码管理中 是否有任何东西可以让您检查测试代码的修订版 x 和生产代码的修订版 x 1 并查看您在修订版 x 中编写的测试是否失败 我对任
  • 如何在 Visual Studio 2010 中的 AnkhSVN 中更改 SVN 身份验证详细信息?

    我想在 Visual Studio 2010 AnkhSVN 插件中更改 SVN 的用户名和密码 我怎样才能做到这一点 找到了 要清除缓存的用户名 密码 您可以访问 Tools gt Options gt Source Control gt
  • Bash 中声明、排版和局部变量之间的区别

    在 Bash 中输入变量时 有什么区别declare and typeset 当在函数内部使用时 有什么区别declare and typeset and local 我遇到的唯一区别是排版可以移植到 ksh 脚本 除此之外 还有什么理由可
  • 浮点图 - 外部选择条形图

    我正在使用浮点http code google com p flot http code google com p flot 并希望当用户将鼠标悬停在链接上时突出显示系列中的特定栏 有谁知道该怎么做 Cheers Tim 你正在寻找的是hi
  • 将视觉块发送到外部命令

    如何将视觉块发送到外部命令 我使用 Ctrl q 选择我的块 然后按 program name 但 Vim 发送整行而不是选定的文本块 我在 Windows 10 上使用 gVim Ex 命令是基于行的 而块视觉模式是一个 Vim 扩展 这
  • Kentico UserInfoProvider 在控制台应用程序中未按预期工作

    此代码在 Kentico 网站中运行良好 var users UserInfoProvider GetUsers for int x 0 x lt users Count x UserInfo currentUser users Eleme
  • Tailwind CSS,某些自定义颜色不起作用

    我正在尝试通过编写一些主题在我的项目中使用 Tailwind 自定义颜色tailwind config js extend module exports content src js jsx ts tsx public index html
  • 错误错误:未捕获(承诺):NullInjectorError:R3InjectorError

    我有一条错误消息 ERROR Error Uncaught in promise NullInjectorError R3InjectorError MarketModule IndiceService gt IndiceService g
  • 仅在一个WebLogic集群节点上运行@Scheduled任务?

    我们正在集群 WebLogic 10 3 4 环境中运行一个 Spring 3 0 x Web 应用程序 war 其中包含夜间 Scheduled 作业 但是 当应用程序部署到每个节点时 使用 AdminServer 的 Web 控制台中的
  • 超时后中止 Rust 中的评估

    我有一个 Rust 函数 不是我写的 它要么以毫秒为单位返回 要么在失败前等待约 10 分钟 我想将对这个函数的调用包装在返回一个Option这是None如果运行时间超过 10 秒 则包含结果 如果运行时间较短 然而 我还没有找到任何方法来
  • Kotlin 中的记忆功能

    我有一个带有实例方法 buildHierarchyUncached 的现有类 其签名可以在下面找到 private fun buildHierarchyUncached date LocalDate Node 我想向公众提供function
  • 语音回声问题

    我正在尝试使用 Adob e Flex 构建一个视频聊天程序 但回声存在一个巨大的问题 如果参与者没有使用耳机 他们所说的一切都会产生回声 更糟糕的是 它们实际上可以创建回声的正反馈循环 直到麦克风静音为止该循环不会结束 有没有人在 Fle
  • 根据 WooCommerce 结账中的分类术语限制支付网关

    在我的 WooCommerce 商店中 仅当产品具有类别 ID 266 的特定产品类别时 我想限制并显示支付网关 支票 现在我有了这个代码片段 但它的作用相反 它在结账时禁用了特定产品类别的网关 add filter woocommerce