【问题标题】:Disable specific WooCommerce payment method for all subcategories of a given top category为给定顶级类别的所有子类别禁用特定的 WooCommerce 付款方式
【发布时间】:2021-03-30 18:03:38
【问题描述】:

我正在尝试通过 WooCommerce 的给定类别 slug 为所有子类别停用 PayPal。目前,我的以下代码只是停用了一个类别的付款方式。是否可以为所有子类别停用?

<?php
/**
 * Disable payment gateway based on category.
 */
function ace_disable_payment_gateway_category( $gateways ) {
    // Categories that'll disable the payment gateway 
    $category_slugs = array( 'tobacco' );
    $category_ids = get_terms( array( 'taxonomy' => 'product_cat', 'slug' => $category_slugs, 'fields' => 'ids' ) );

    // Check each cart item for given category
    foreach ( WC()->cart->get_cart() as $item ) {
        $product = $item['data'];

        if ( $product && array_intersect( $category_ids, $product->get_category_ids() ) ) {
            unset( $gateways['ppec_paypal'] );
            break;
        }
    }

    return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'ace_disable_payment_gateway_category' );

【问题讨论】:

    标签: php wordpress woocommerce payment-gateway taxonomy-terms


    【解决方案1】:

    更新 2

    要为顶级产品类别的子类别禁用特定支付网关,请使用以下命令:

    add_filter( 'woocommerce_available_payment_gateways', 'disable_payment_gateway_subcategory' );
    function disable_payment_gateway_subcategory( $payment_gateways ) {
        if ( is_admin() ) return $payment_gateways; // Not on admin
    
        $taxonomy     = 'product_cat';
        $term_slug    = 'tobacco'; // Main top category
        $term_id      = get_term_by( 'slug', $term_slug, $taxonomy )->term_id; // get term Id
        $children_ids = get_term_children( $term_id, $taxonomy ); // Get all children terms Ids
        array_unshift( $children_ids, $term_id ); // Adding main term Id to the array
    
        // Check each cart item for given subcategories of a top category
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $term_ids = wp_get_post_terms( $cart_item['product_id'], $taxonomy, array('fields' => 'ids') );
    
            if ( array_intersect( $children_ids, $term_ids ) ) {
                unset($payment_gateways['ppec_paypal']);
                break; // stop the loop
            }
        }
        return $payment_gateways;
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 谢谢,但不幸的是它不起作用。与将付款方式替换为“bacs”相同。
    • 我猜这仅适用于结帐或购物车?这也应该在单个产品页面上禁用。快速 PayPal 结帐也在单个产品页面上激活。抱歉,我不想占用您的时间,但我对 PHP 的了解不如您,通常我是一名 Java 开发人员。也许你可以拯救我的一天:)
    • 老兄!!!它现在完美运行。非常感谢你!我会接受答案。但是还有一个问题:如果我添加一个“烟草”项目并结帐,我的服务器日志会输出一个“408”错误代码而没有任何进一步的信息——它来自您的代码。有任何想法吗?尽管如此,还是感谢您的宝贵时间!
    • @OzzyB。我没听懂……您的意思是,如果您添加一个将“tobbaco”作为类别的项目,您会收到错误消息?我刚刚更新了代码以将“烟草”术语 ID 包含到术语 ID 数组中……
    • 使用代码(之前的代码也是如此)WooCommerce 存在一些问题。我无法打开 WooCommerce 仪表板和客户。其他都没有问题。这是服务器的输出:i.ibb.co/B2n6tB6/Unbenannt.png
    【解决方案2】:

    对于有同样问题的其他人。 @LoicTheAztec 代码由代码部分扩展,以检查单个产品站点是否也是该类别的一部分:

     * Disable payment gateway based on category.
     */
    add_filter( 'woocommerce_available_payment_gateways', 'disable_payment_gateway_subcategory' );
    function disable_payment_gateway_subcategory( $payment_gateways ) {
        if ( is_admin() ) return $payment_gateways; // Not on admin
    
        $taxonomy     = 'product_cat';
        $term_slug    = 'tobacco'; // Main top category
        $term_id      = get_term_by( 'slug', $term_slug, $taxonomy )->term_id; // get term Id
        $children_ids = get_term_children( $term_id, $taxonomy ); // Get all children terms Ids
        array_unshift( $children_ids, $term_id ); // Adding main term Id to the array
    
        // Check on single product site 
        $cateID = wp_get_post_terms( get_the_ID(), $taxonomy, array('fields' => 'ids') );
        if ( array_intersect( $children_ids, $cateID ) ) {
            unset($payment_gateways['ppec_paypal']);
        }
    
        // Check each cart item for given subcategories of a top category
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $term_ids = wp_get_post_terms( $cart_item['product_id'], $taxonomy, array('fields' => 'ids') );
    
            if ( array_intersect( $children_ids, $term_ids ) ) {
                unset($payment_gateways['ppec_paypal']);
                break; // stop the loop
            }
        }
        return $payment_gateways;
            
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-16
      • 1970-01-01
      • 2021-08-24
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 2018-06-10
      相关资源
      最近更新 更多