Easy Digital Downloads (Recurring Payments) if User Has Active Subscription show Something - php

I need to show something only to users with active subscriptions, im using the edd recurring payments plugin, I found this is their docs
$subscriber->has_active_subscription()
But im not sure how to make use of it to show something only to users with active subscriptions.
So i will be adding this code in my archive.php file and show extra php code for active users.

that code you found is part of the OOP class used by Easy Digital Downloads. Their docs are here: https://docs.easydigitaldownloads.com/article/1160-recurring-payments-developer-eddrecurringsubscriber
What you'd need to do is something like:
$my_user = get_current_user_id(); //grab current user
$subscriber = new EDD_Recurring_Subscriber( $my_user, true ); //pass user id to EDD class
//The EDD class will return its own user object, and on that you can do your check from the question:
if ($subscriber->has_active_subscription()) {
echo 'some special message for users with subscriptions';
} else {
//do something else
}
Watch out though, because that method will return true both if user has an active subscription and if he/she has a cancelled subscription (but false if subscription has expired). That may or may not be what you want.
I don't have EDD so haven't tested this but I hope it at least gets you started.

Related

Getting a corporate account id from new Memberpress transaction (non subscription)

I'm using Memberpress and Memberpress Corporate on my WordPress site and I'm trying to add a custom function when a member signs up under a specific membership type or purchases a specific membership type. When this happens I need to grab the corporate account ID and do something with it.
I'm using the hook mepr-event-transaction-completed as this fires for both recurring and non-recurring transactions, though I also tried mepr-event-non-recurring-transaction-completed just to be sure.
This is my code:
$transaction = $event->get_data();
$membership_type_ids = array(1, 2, 4);
if (in_array($transaction->product_id, $membership_type_ids) && $transaction->txn_type == 'payment') {
$org_id = $transaction->corporate_account_id;
my_custom_function($org_id);
}
When the user is signing up for this membership type with a subscription, this is no problem, I can retrieve this, however if they are signing up with a one-time non-recurring transaction, the corporate account id is returning as 0, even though when I go to check the database, there is a corporate account id there.
Does the corporate account id get set at a different time for non-recurring transactions?
Okay, so after speaking to Memberpress, it turns out this just doesn't get set at the right time.
I used a workaround as so:
if($transaction->corporate_account_id !== "0" && $transaction->corporate_account_id !== 0) {
//some irrelevant code here about what to do if the corporate id actually works
} else {
write_log('sending cron to add new user due to corporate id returning as 0, please check in 2 minutes. tran_num = '.$transaction->trans_num);
wp_schedule_single_event( strtotime("+2 minutes"), 'send_fix_for_zero_transaction', array($transaction),false );
return;
}
add_action( 'send_fix_for_zero_transaction', 'single_transaction_create_corporate' );
function single_transaction_create_corporate($transaction) {
//NOTE: this function is only used it a one-time transaction is created in the backend to create a corporate membership, because there is a bug in memberpress that means the corporate_id isn't sent back by the event. This event will only fire if that is the case, otherwise this is handled by rc_setup_new_org. This should eventually be deprecated when Memberpress fix their issues.
$trans_num = $transaction->trans_num;
$full_trans = MeprTransaction::get_one_by_trans_num($trans_num);
//do whatever you need to do with the transaction here, you have the number now
}

PHP - How to check if a db variable is true for any of a users blogs in wordpress multisite

I have a wordpress multisite install with 100+ users with blogs. Some users have more than one blog.
Users can pay for their blogs (think of it as a donation scenario), or they can choose to have a free blog. If they have a free blog then there is a variable in wp_Blog-ID_options called is_free_site and it is set to 1. (Blog-ID relates to the users blog)
If the user is paying for their site, is_free_site will either be set to 0 or the variable won't exist in the db at all. See screenshot:
http://www.awesomescreenshot.com/image/1657353/0238f2bf2d49f0b165170be6c64ba3a3
I am trying to write a function called does_user_pay this will look to see if the current logged in user pays for any of their sites and if they do it returns true. This is so I can feed premium content to those who choose to pay
So for example, user A might have 2 sites, one which they pay for, one which they don't - so does_user_pay() should be true
User B might have 1 site which they don't pay for, so does_user_pay() will be false
user C might have 1 site which they pay for, so does_user_pay() will be true.
I am coding this into a custom plugin, here is what I have so far:
function does_user_pay() {
global $current_user;
$user_id = get_current_user_id();
$user_blogs = get_blogs_of_user($user_id);
// Need to write a function here that checks if any of the user blogs are paid for
if(is_user_logged_in() && USER_HAS_PAID_SITE) {
return true;
} else {
return false;
}
}
Any help would be really appreciated
You can do this check in this way (assuming user is logged and his id is stored in $user_id):
$blogs = get_blogs_of_user($user_id); // array with all user blogs
foreach($blogs as $blog){
switch_to_blog($blog->userblog_id); // switch the blog
$is_free_site = get_option('is_free_site', 0); // get the option value (if not exists, so the user paid, we'll get 0)
restore_current_blog(); // it's important to restore after a switch blog
if($is_free_site == 0) return true; // found a paid blog
}
return false; // not found a paid blog

Silverstripe: SwipeStripe Member/Customer record not defined

So I'm using the SwipeStripe module as an ecommerce shop, and having issues logging member details as customer details. I'll outline the process and what i've tried so far.
I am currently logged in as a member of my website.
I can view, and add products to my cart.
When I reach the checkout page, it asks me to register my details (become a member).
I have gone through the Customer.php, and OrderForm.php files. What I have discovered is:
Customer::currentUser() is not recognising me as a logged in user.
When I print Member::currentUser() it can see I am a logged in, registered member and I can print the details of that record.
Customer::currentUser() is as follows in the Customer.php file:
static function currentUser() {
$id = Member::currentUserID();
if($id) {
return DataObject::get_one("Customer", "\"Member\".\"ID\" = $id");
}
}
I was wondering if anyone could help me understand why Customer::currentUser() isn't recognising me as a logged in user?
Also, in the OrderForm.php, when I change the validation for the "register as a member" form fields (the function is called 'createFields') to Member::currentUserID() it recognises that I'm logged in and skips that section. It's only when it goes to save my order and customer details to the customer DB table, that it can't retrieve my email address.
I know this is an old post, but I've used this code/method all the way up to SS 3.4.
You will need to update Aram's code to work with SS 3.x.x
https://www.silverstripe.org/community/forums/e-commerce-modules/show/21390
-helenclarko

PHP: Check for profile fields completedness

I have a social networking site built on a PHP framework. I'm using a plugin to award members points when they update their profile. Right now, whenever a member clicks on 'Save' it triggers the profileupdate event, and that event triggers the points being awarded. But, even if the profile fields are all empty, the event is still triggered and they still get points… so, my clever users are gaming the system to get points without really updating their profile :\
I want to add a function that checks that the profile fields aren't empty -- or, ideally, checks that a significant amount of changes have been made to at least 1 of the profile fields, but I'm not sure how to do that.
I'm still pretty new to PHP (about 1 year experience), so if anyone could help both with explaining what the checking process should be and the specifics on the code to execute the checking function, I'd really appreciate it!
Here are the current events and functions:
When the owner of the page clicks on 'Save', this is the event in the core of the PHP framework that gets triggered to notify the user of the update:
$owner->save();
trigger_event('profileupdate', $owner->type, $owner);
system_message(echo("profile:saved"));
This is the function in the points plugin that checks to see if the plugin is configured to award points based on a profile update, and then calls the points_add function to add points to the user:
function points_profile($event, $type, $object) {
if ($points = get_plugin_setting('profileupdate')) {
if (function_exists('points_add')) {
points_add(get_logged_in_user_guid(), $points, $event, $type, $object->entity_guid);
}
}
return(true);
}
This is an example of how the individual profile fields are defined/labelled -- ie, "admin_defined_profile_1"
if (save_config("admin_defined_profile_$id", $label) &&
save_config("admin_defined_profile_type_$id", $type) &&
save_config('profile_custom_fields', $fieldlist))
look on rowcount() http://www.php.net/manual/en/pdostatement.rowcount.php
it will -on single UPDATE -return 1 if anything was actually changed and 0 if nothing was changed.

Set subscriber status in Magento programmatically

I am trying to write a module that syncs my newsletter subscribers in Magento with a external database. I need to be able to update the subscription status in Magento programmatically but I am having diffuculty getting the "setStatus" method in Magento to work. It does not throw any errors but the code does not seem to have any effect. Below is the code where I call the method:
$collection = Mage::getResourceModel('newsletter/subscriber_collection')->showStoreInfo()->showCustomerInfo();
foreach ($collection as $cust) {
$cust->setStatus(1);
}
In theory, this should set the status of all of my subscribers to "subscribed". I could optionally change the argument sent to "setStatus" to any of the below ints for a different status.
1: Subscribed
2: Status Not Active
3: Unsubscribed
How to best change the subscriber status or get this code working?
Here an import script:
<?php
require_once("./app/Mage.php");
Mage::app();
$subscribers = array('email1#server1.com', 'email2#server2.com');
foreach ($subscribers as $email) {
# create new subscriber without send an confirmation email
Mage::getModel('newsletter/subscriber')->setImportMode(true)->subscribe($email);
# get just generated subscriber
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
# change status to "subscribed" and save
$subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
$subscriber->save();
}
?>
It seems that newsletter subscribers are also stored elsewhere. What you are setting is just a check in the customer base for some other use.
You need to do the following for each customer as well.
Mage::getModel('newsletter/subscriber')->subscribe($email);
See this link for a complete reference.
Thanks to the link #Ozair shared I was able to figure out what I needed to do.
I was successfully setting the status of the subscriber in the Magento subscriber object but I was not saving the object. I needed to call Magento's save method so it would call the ORM and write it to the database. All I need to do was add
$cust->save();
in the for loop. Below is the whole code snippet.
$collection = Mage::getResourceModel('newsletter/subscriber_collection')->showStoreInfo()->showCustomerInfo();
foreach ($collection as $cust) {
$cust->setStatus(1);
$cust->save();
}
I Hope this helps someone in the future. I needed it for a Constant Contact - Magento Synchronization extension I was making: http://www.freelunchlabs.com/store/constant-contact-and-magento-sync.html

Categories