Skip to content

Instantly share code, notes, and snippets.

@ChrisFlannagan
Created August 22, 2016 22:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ChrisFlannagan/3aaf4f58a627fa307180604886f2335c to your computer and use it in GitHub Desktop.
Save ChrisFlannagan/3aaf4f58a627fa307180604886f2335c to your computer and use it in GitHub Desktop.
<?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