Limit subscription to one

0.0/5
<?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( "Můžete mít pouze jedno předplatné" );
                return false;
            }
        }
    }
    return $valid;
}

Comments and reviews

{{ reviewsTotal }} Review
{{ reviewsTotal }} Reviews
{{ options.labels.newReviewButton }}
{{ userData.canReview.message }}

Description

This snippet was fetched automatically from WPDistro.cz projects.

Author

Tags

This snippet is untagged