Search code examples
phpwordpresswoocommercecartwoocommerce-subscriptions

Conditionally add a subscription when product added to cart in WooCommerce 3


Hi Im looking for a code that adds my subscription ID (2282) to cart if there is added a normal product but NOT if the user is already subscriber.

Feel free to ask questions. I'm in the GMT+1 time zone

Wordpress - 4.8.1
WooCommerce – 3.1.1
WooCommerce Subscriptions - 2.2.11
WooCommerce Memberships – 1.8.8
Theme - Shopkeeper - 2.2.3

i've looked and tried to fool around with this. With no success

// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 2282;
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }
} 

How can I conditionally add a subscription when product added to cart for non active subscribers?


Solution

  • For WooCommerce subscriptions I haven't found any function to check if the current user has an active subscription.

    So I have made this answer that still works on WooCommerce 3+ and last WC Subscriptions plugin: Detecting if the current user has an active subscription

    So using a custom function hooked in woocommerce_add_to_cart action hook and using my conditional function code from my old answer, you will get it working:

    // Conditional function detecting if the current user has an active subscription
    function has_active_subscription( $user_id=null ) {
        // if the user_id is not set in function argument we get the current user ID
        if( null == $user_id )
            $user_id = get_current_user_id();
    
        // Get all active subscrptions for a user ID
        $active_subscriptions = get_posts( array(
            'numberposts' => -1,
            'meta_key'    => '_customer_user',
            'meta_value'  => $user_id,
            'post_type'   => 'shop_subscription', // Subscription post type
            'post_status' => 'wc-active', // Active subscription
        ) );
        // if user has an active subscription
        if(!empty($active_subscriptions)) return true;
        else return false;
    }
    
    
    // Conditionally checking and adding your subscription when a product is added to cart
    add_action( 'woocommerce_add_to_cart', 'add_subscription_to_cart', 10, 6 );
    function add_subscription_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    
        // Here define your subscription product
        $subscription_id = 2282;
        $found = false;
    
        if ( is_admin() || has_active_subscription() || $product_id == $subscription_id ) return;  // exit
    
        // Checking if subscription is already in cart
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $cart_item['product_id'] == $subscription_id ){
                $found = true;
                break;
            }
        }
        // if subscription is not found, we add it
        if ( ! $found )
            WC()->cart->add_to_cart( $subscription_id );
    }
    

    Code goes in any plugin php file (hooks can be used in function.php file of the active theme).

    The code is tested and works.


    And if the subscription is removed from cart, we will do a re-check in checkout:

    add_action( 'woocommerce_before_checkout_form', 'check_subscription_in_checkout', 10, 0 );
    function check_subscription_in_checkout() {
    
        // Here define your subscription product
        $subscription_id = 2282;
        $found = false;
    
        if ( is_admin() || has_active_subscription() || ! is_checkout() ) return;  // exit
    
        // Checking if subscription is already in cart
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $cart_item['product_id'] == $subscription_id ){
                $found = true;
                break;
            }
        }
        // if subscription is not found, we add it
        if ( ! $found )
            WC()->cart->add_to_cart( $subscription_id );
    }