我需要根据购物车中添加的物品的运输类别禁用某些付款方式。
我尝试了以下代码:
add_filter( 'woocommerce_available_payment_gateways', 'unset_gateway_by_sclass' );
function unset_gateway_by_sclass( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
$unset = false;
$category_ids = array( no-cod );
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'shipping_class' );
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) ) {
$unset = true;
break;
}
}
}
if ( $unset == true ) unset( $available_gateways['cod'] );
return $available_gateways;
}
这不起作用,我在哪里出错了?
答案 0 :(得分:0)
想通了
add_filter( 'woocommerce_available_payment_gateways', 'sclass_func', 10, 2 );
function sclass_func( $available_gateways ) {
$shipping_class_target = 1157;
$in_cart = false;
foreach( WC()->cart->cart_contents as $key => $values ) {
if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if( $in_cart ) {
unset($available_gateways['cod']);
}
return $available_gateways;
}