用户注销时在 wooCommerce 产品类别上隐藏 ClearPay

问题描述 投票:0回答:1

我的一个客户需要在 wooCommerce 上的特定产品类别选择中隐藏 ClearPay,但是如果用户登录,他们可以看到 ClearPay 选项。

我已经研究出如何隐藏产品类别的 ClearPay,但未将其设置为登录后显示。任何帮助将不胜感激。 :)

我尝试过 ClearPay 的 PHP 脚本,但它隐藏了所有用户的选项。

wordpress woocommerce payment
1个回答
0
投票

以下是如何实现这一目标的总体概述:

// Function to hide ClearPay for specific product categories
function hide_clearpay_for_categories( $available_gateways ) {
    // Check if ClearPay is among the available gateways and if the user is not logged in
    if ( ! is_user_logged_in() && isset( $available_gateways['clearpay'] ) ) {
        // Define an array of product categories where ClearPay should be hidden
        $hidden_categories = array( 'category-slug-1', 'category-slug-2' ); // Replace with actual category slugs
        
        // Get current product categories
        $product_categories = array();
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product_id = $cart_item['product_id'];
            $terms = get_the_terms( $product_id, 'product_cat' );
            if ( $terms && ! is_wp_error( $terms ) ) {
                foreach ( $terms as $term ) {
                    $product_categories[] = $term->slug;
                }
            }
        }
        
        // Check if any of the product categories match the hidden categories
        if ( ! empty( array_intersect( $product_categories, $hidden_categories ) ) ) {
            // Remove ClearPay from available gateways
            unset( $available_gateways['clearpay'] );
        }
    }
    
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'hide_clearpay_for_categories' );
© www.soinside.com 2019 - 2024. All rights reserved.