在我的 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;
}
慕婉清6462132
郎朗坤