WooCommerce 禁止针对特定产品类别使用 ClearPay 付款

问题描述

我正在尝试为特定产品类别禁用 ClearPay 付款方式。

我设法隐藏了商店和产品页面的 ClearPay 选项,但它仍然允许我在购物车中使用 ClearPay。

这是我用过的代码

/**
 * @param bool $bool_result
 * @param WC_Product $product
 */
function clearpay_ips_callback( $bool_result,$product ) {
    if( has_term( array( 18,28 ),'product_cat' ) ) {
        // do something if current product in the loop is in product category with ID 18 or 25
        $bool_result = false;
    }
    return $bool_result;
}
add_filter( 'clearpay_is_product_supported','clearpay_ips_callback',10,2 );

如果我们有属于特定产品类别术语 ID 的产品,任何人都可以帮助我阻止 ClearPay 在结账时可用

解决方法

产品 ID 应在 has_term() 中指定,例如:

add_filter( 'clearpay_is_product_supported','clearpay_ips_callback',10,2 );
function clearpay_ips_callback( $bool_result,$product ) {

    if( has_term( array( 18,28 ),'product_cat',product->get_id() ) ) {
        return false;
    }
    return $bool_result;
}

您也可以在结帐页面尝试以下购物车项目:

add_filter( 'woocommerce_available_payment_gateways','conditional_hiding_payment_gateway',1,1 );
function conditional_hiding_payment_gateway( $available_gateways ) {
    // Not in backend (admin)
    if( is_admin() ) 
        return $available_gateways;

    $is_in_cart = false;

    // Iterating through each items in cart
    foreach(WC()->cart->get_cart() as $item){
        if( has_term( array( 18,$item['product_id'] ) ) {
            $is_in_cart = true;
            break;  // stop the loop
        }
    }
    if( $is_in_cart )
        unset($available_gateways['clearpay']);
    }
    return $available_gateways;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该会更好地工作。