Hide cash on delievery for orders over certain amo

2020-05-06 11:43发布

I want to have the cash on delivery option only for price below 100$ and hide it automatically when cart is above 100$. The problem is that, I have 3 different payment methods right now. Paypal, cheque and COD. When a person buy something, and choose cash on delievry method, I've written a description there saying "you can choose COD if your order is below 100$". But some people neglect it and still choose COD even their purchase is above 100$. So, I want to hide COD automatically, when a purchase is above 100$. Hence, when a purchase is above 100$, there would be just two options, Paypal and Cheque. Hope I could clarify it a bit more.

Thanks

1条回答
放我归山
2楼-- · 2020-05-06 12:05

You can use the woocommerce_available_payment_gateways hook to edit woocommerce gateways.

add_filter( 'woocommerce_available_payment_gateways' , 'change_payment_gateway', 20, 1);

/**
 * remove cod gateway if cart total > 100
 * @param $gateways
 * @return mixed
 */
function change_payment_gateway( $gateways ){
    // Compare cart subtotal (without shipment fees)
    if( WC()->cart->subtotal > 100 ){
         // then unset the 'cod' key (cod is the unique id of COD Gateway)
         unset( $gateways['cod'] );
    }
    return $gateways;
}
查看更多
登录 后发表回答