14

How to Gate Content with Easy Digital Downloads and Oxygen Builder Based on User Purchases and Subscriptions

Powered by WPCodeBox

This snippet provides Oxygen Builder conditions for Easy Digital Downloads, allowing you to gate content based on user purchases and subscriptions, including “User Has Purchased,” “User Has Not Purchased,” “User Is Subscribed To,” and “User Is Not Subscribed To” a specific download ID.


<?php

add_action('plugins_loaded', function () {
    
    if (function_exists('oxygen_vsb_register_condition')) {

        oxygen_vsb_register_condition('User...', array('options' => array(), 'custom' => true), array('has purchased', 'has not purchased'), 'ex_edd_has_user_purchased_callback', 'EDD');

        function ex_edd_has_user_purchased_callback($value, $operator)
        {

            $current_user = get_current_user_id();
            $value = intval($value);

            if ($operator == 'has purchased') {
                if (edd_has_user_purchased($current_user, $value)) {
                    return true;
                } else {
                    return false;
                }
            } else if ($operator == 'has not purchased') {
                if (!edd_has_user_purchased($current_user, $value)) {
                    return true;
                } else {
                    return false;
                }
            }

        }

        oxygen_vsb_register_condition('User is...', array('options' => array(), 'custom' => true), array('subscribed to', 'not subscribed to'), 'ex_edd_has_active_subscription_callback', 'EDD');

        function ex_edd_has_active_subscription_callback($value, $operator)
        {

            $subscriber = new EDD_recurring_subscriber(get_current_user_id());
            $value = intval($value);

            if ($operator == 'subscribed to') {
                if ($subscriber->get_subscriptions($value, array('active'))) {
                    return true;
                } else {
                    return false;
                }
            } else if ($operator == 'not subscribed to') {
                if (!$subscriber->get_subscriptions($value, array('active'))) {
                    return true;
                } else {
                    return false;
                }
            }

        }

    }
});

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.