Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Membership 2] - List Member Coupons
<?php
/**
* Plugin Name: M2P List Member Coupons
* Description: M2P List Member Coupons
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/tho2757
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', 'wpmudev_ms_load_member_coupons_func', 100 );
function wpmudev_ms_load_member_coupons_func(){
if( defined('MS_IS_PRO') && MS_IS_PRO && class_exists('MS_Addon_Coupon_Model') ){
/**
* Retrive coupon from invoice
*
* Make sure you enabled coupon addon
* @param mix $invoice object MS_Model_Invoice or int invoice id
*/
function wpmudev_ms_get_coupon_code( $invoice ){
if( is_numeric( $invoice ) ){
$invoice = MS_Factory::load( 'MS_Model_Invoice', $invoice_id );
}
if( is_a( $invoice, 'MS_Model_Invoice' ) && ! empty( $invoice->coupon_id ) ){
$coupon = MS_Factory::load( 'MS_Addon_Coupon_Model', $invoice->coupon_id );
// show coupon code
return esc_html( $coupon->code );
}
return false;
}
/**
* Short code retrive all invoices of a member
* @param array $atts shortcode params: user_id and limit (limit number invoices)
* @return mix html or false
*/
function wpmudev_ms_list_member_coupons($atts){
// get current member
$atts = shortcode_atts( array(
'user_id' => 0, // same as member_id
'limit' => -1 // number invoice
), $atts );
extract( $atts );
if( empty( $user_id ) ){
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
}
$member = MS_Factory::load( 'MS_Model_Member', $user_id );
// return if is not a member or is admin
if( ! $member->is_member || $member->is_admin_user( $member->id ) ){
return;
}
// limit number invoices
$limit = (int) $limit;
$limit = $limit > 0 ? $limit : -1;
// Get public invoice(s) of current member, included all invoice(s) of old membership
$all_public_invoices = MS_Model_Invoice::get_public_invoices(
$member->id,
$limit
);
if( ! empty( $all_public_invoices ) ){
// try to loop here
ob_start();
printf(__('<p>All coupon code from all invoices, included all invoices of old membership of %s</p>','wpmudev'), $member->name );
foreach( $all_public_invoices as $invoice ){
// show counpon code here
echo esc_html($invoice->name) .': ';
echo wpmudev_ms_get_coupon_code( $invoice );
echo '<br/>';
}
return ob_get_clean();
}
// Note: you also can check another way to retrive the invoice(s) in class MS_Model_Invoice, ex: MS_Model_Invoice::get_invoices()
return false;
}
add_shortcode( 'wpmudev_ms_list_member_coupons', 'wpmudev_ms_list_member_coupons' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.