-
-
Save easaw/08fa83e7090a208caeef01e096afc1ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Only allow one payment plan purchase (Subscription) at a time | |
*/ | |
add_filter( 'woocommerce_add_to_cart_validation', 'woo_block_sub', 10, 2 ); | |
function woo_block_sub( $valid, $product_id ) { | |
// Get the current product | |
$current_product = wc_get_product( $product_id ); | |
// See if the current product user's are viewing is a subscription product | |
if ( $current_product instanceof WC_Product_Subscription || $current_product instanceof WC_Product_Variable_Subscription ) { | |
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
// Loop through all products in the cart | |
$_product = $values['data']; | |
// Check if current item is a subscription type | |
if( $_product instanceof WC_Product_Subscription || $_product instanceof WC_Product_Subscription_Variation ) { | |
// Display a notice and cancel the addition of item to cart | |
wc_add_notice( "Only one payment plan can be purchased." ); | |
return false; | |
} | |
} | |
} | |
return $valid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment