WC_Gateway_COD::is_available
Check If The Gateway Is Available For Use.
Method of the class: WC_Gateway_COD{}
No Hooks.
Returns
true|false.
Usage
$WC_Gateway_COD = new WC_Gateway_COD(); $WC_Gateway_COD->is_available();
WC_Gateway_COD::is_available() WC Gateway COD::is available code WC 10.5.0
public function is_available() {
$is_virtual = true;
$shipping_methods = array();
// Get shipping methods from the cart or order.
if ( is_wc_endpoint_url( 'order-pay' ) ) {
$order = wc_get_order( absint( get_query_var( 'order-pay' ) ) );
$shipping_methods = $order ? $order->get_shipping_methods() : array();
$is_virtual = ! count( $shipping_methods );
} elseif ( WC()->cart && WC()->cart->needs_shipping() ) {
$shipping_methods = WC()->cart->get_shipping_methods();
$is_virtual = false;
}
// If COD is not enabled for virtual orders and the order does not need shipping, return false.
if ( ! $this->enable_for_virtual && $is_virtual ) {
return false;
}
// Return early if:
// - There are no shipping methods resrictions in place.
// - The order is virtual so needs no shipping.
// - Shipping methods are not set yet.
if ( empty( $this->enable_for_methods ) || $is_virtual || ! $shipping_methods ) {
return parent::is_available();
}
// Get the selected shipping method ids. This works on both WC_Shipping_Rate and WC_Order_Item_Shipping class instances.
$canonical_rate_ids = array_unique(
array_values(
array_map(
function ( $shipping_method ) {
return $shipping_method && is_callable( array( $shipping_method, 'get_method_id' ) ) && is_callable( array( $shipping_method, 'get_instance_id' ) ) ? $shipping_method->get_method_id() . ':' . $shipping_method->get_instance_id() : null;
},
$shipping_methods
)
)
);
if ( ! count( $this->get_matching_rates( $canonical_rate_ids ) ) ) {
return false;
}
return parent::is_available();
}