Skip to content

Commit

Permalink
fix: this gateway no longer shows up for single purchase products, on…
Browse files Browse the repository at this point in the history
…ly if there is at least one subscription product in the cart
  • Loading branch information
Marcuzz committed Nov 26, 2021
1 parent 4fd5f78 commit 70bba68
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Alternatively you could look into using WooCommerce "Early renewals": [https://d
* Added: Support for Klarna Checkout.
* Fixed: No longer attempt to load Vipps Recurring Payments if WooCommerce is disabled/not installed.
* Fixed: If you have a variable subscription product the Vipps settings from the "parent" will now be respected.
* Fixed: This gateway no longer shows up for single purchase products, only if there is at least one subscription product in the cart.

= 1.11.0 =
* Added: You can now pay for single payment products in the same shopping cart as a subscription.
Expand Down
24 changes: 24 additions & 0 deletions woo-vipps-recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ public function init() {
add_filter( 'woocommerce_product_data_tabs', [ $this, 'woocommerce_product_data_tabs' ] );
add_filter( 'woocommerce_product_data_panels', [ $this, 'woocommerce_product_data_panels' ] );
add_filter( 'woocommerce_process_product_meta', [ $this, 'woocommerce_process_product_meta' ] );

// Disable this gateway unless we're purchasing at least one subscription product.
add_filter( 'woocommerce_available_payment_gateways', [ $this, 'maybe_disable_gateway' ] );
}

/**
Expand Down Expand Up @@ -379,6 +382,27 @@ public function admin_head() {
<?php
}

public function maybe_disable_gateway( $methods ) {
if ( is_admin() || ! is_checkout() ) {
return $methods;
}

$has_subscription_product = false;
foreach ( WC()->cart->get_cart_contents() as $values ) {
$product = wc_get_product( $values['product_id'] );

if ( $product->is_type( [ 'subscription', 'variable-subscription' ] ) ) {
$has_subscription_product = true;
}
}

if ( ! $has_subscription_product ) {
unset( $methods['vipps_recurring'] );
}

return $methods;
}

/**
* @return string
*/
Expand Down

0 comments on commit 70bba68

Please sign in to comment.