Skip to content

Instantly share code, notes, and snippets.

@webdados
Last active May 26, 2023 10:27
Show Gist options
  • Save webdados/82b2582d36cec2fd76c4cae25b0d7d97 to your computer and use it in GitHub Desktop.
Save webdados/82b2582d36cec2fd76c4cae25b0d7d97 to your computer and use it in GitHub Desktop.
Only allow SeQura payment for orders above a certain value
<?php
// First of all define the minimum value on a constant, for easy change - Ideally this would be loaded from an option on the settings
define( 'SEQURA_MIN_VALUE', 200 );
// Set the filters
add_filter( 'woocommerce_available_payment_gateways', 'disable_sequra_for_low_values' );
add_filter( 'woocommerce_sq_is_available_in_product_page', 'disable_sequra_for_low_values_product_page', 10, 2 );
// Hide it on the checkout
function disable_sequra_for_low_values( $available_gateways ) {
if ( defined( 'SEQURA_MIN_VALUE' ) && floatval( SEQURA_MIN_VALUE ) > 0 && isset( $available_gateways['sequra'] ) ) {
$value_to_pay = null;
// Order total (pay form on My account) or cart total (regular checkout)?
$pay_slug = get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' );
$order_id = absint( get_query_var( $pay_slug ) );
if ( $order_id > 0 ) {
// Pay for on My Account
$order = wc_get_order( $order_id );
$value_to_pay = $order->get_total();
} else {
// Regular checkout?
if ( ! is_null( WC()->cart ) ) {
$value_to_pay = WC()->cart->total;
} else {
// No cart? Where are we? We shouldn't unset the payment gateway
}
}
if ( ! is_null( $value_to_pay ) ) {
if ( floatval( $value_to_pay ) < floatval( SEQURA_MIN_VALUE ) ) {
unset( $available_gateways['sequra'] );
}
}
}
return $available_gateways;
}
// Hide it on the product page (simple products only)
function disable_sequra_for_low_values_product_page( $bool, $product_id ) {
if ( $bool && defined( 'IHD_SEQURA_MIN_VALUE' ) && floatval( IHD_SEQURA_MIN_VALUE ) > 0 ) {
if ( $product = wc_get_product( $product_id ) ) {
// We can only do this for simple products as we have no way to know the selected variation
if ( $product->get_type() == 'simple' ) {
if ( floatval( $product->get_price() ) < floatval( IHD_SEQURA_MIN_VALUE ) ) {
return false;
}
}
}
}
return $bool;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment