如果购物车中只有特定类别的产品,则隐藏付款方式

时间:2019-05-03 09:01:45

标签: woocommerce hide payment-gateway add-filter

如果购物车中只有特定类别的产品(327),谁知道如何在结帐页面上隐藏付款方式(在这种情况下为货到付款)?

使用以下代码,如果购物车中包含该类别的产品(327),则鳕鱼付款方式将被隐藏。但是,目标是仅在购物车中没有找到其他类别的产品时执行此操作:

add_filter( 'woocommerce_available_payment_gateways', 'wp_unset_gateway_by_category' );

function wp_unset_gateway_by_category( $available_gateways ) {
global $woocommerce;
$unset = false;
$category_ids = array(327); // The ID of the category for which the gateway will be removed.
foreach ( $woocommerce->cart->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['cod'] ); // One of the hardcoded Woocommerce standard types of payment gateways - paypal, cod, bacs, cheque
    return $available_gateways;
}

0 个答案:

没有答案