Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Plugin Name: mac-custom-mycred-coupon
- * Description: This plugin contains custom functions from mycred.
- * Version: 0.2
- */
- /* Your code goes below here. */
- /* allows for accrual of points when when paying with points */
- add_filter( 'mycred_woo_reward_mycred_payment', '__return_true' );
- /**
- * Convert myCRED Points into WooCommerce Coupon
- * @version 1.4
- */
- add_shortcode( 'mycred_to_woo_coupon', 'mycred_pro_render_points_to_coupon' );
- function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
- // Users must be logged in
- if ( ! is_user_logged_in() )
- return 'You must be logged in to generate store coupons.';
- // myCRED must be enabled
- if ( ! function_exists( 'mycred' ) )
- return 'myCRED must be enabled to use this shortcode';
- extract( shortcode_atts( array(
- 'exchange' => 1,
- 'amount' => NULL,
- 'type' => 'mycred_default',
- 'button_label' => 'Create Coupon',
- 'reference' => 'points_to_coupon',
- 'log' => '%plural% conversion into store coupon: %post_title%',
- 'product_categories' => ''
- ), $atts ) );
- // Load myCRED
- $mycred = mycred( $type );
- // Prep
- $original = $amount;
- $error = $code = false;
- $output = '';
- $user_id = get_current_user_id();
- // No need to show this for excluded users
- if ( $mycred->exclude_user( $user_id ) ) return;
- $balance = $mycred->get_users_balance( $user_id );
- // Form submission
- if ( isset( $_POST['mycred_to_woo'] ) && wp_verify_nonce( $_POST['mycred_to_woo']['token'], 'points-to-woo-coupon' ) ) {
- // Make sure amounts are always positive
- if ( $amount === NULL ) {
- $original = NULL;
- $amount = abs( $_POST['mycred_to_woo']['amount'] );
- }
- // Exchange rate
- $value = $mycred->number( $amount*$exchange );
- // Make sure amount is not zero
- if ( $amount == $mycred->zero() )
- $error = 'Amount can not be zero';
- // Make sure user has enough points
- if ( $amount > $balance )
- $error = 'Insufficient Funds. Please try a lower amount';
- // If no errors
- if ( $error === false ) {
- $code = wp_generate_password( 12, false, false );
- // Deduct points from user
- add_filter( 'mycred_parse_log_entry', 'mycred_pro_temp_post_title_value', 10, 2 );
- $charge = $mycred->add_creds(
- $reference,
- $user_id,
- 0-$amount,
- $log,
- 0,
- $code,
- $type
- );
- remove_filter( 'mycred_parse_log_entry', 'mycred_pro_temp_post_title_value', 10, 2 );
- // If points were deducted create coupon
- if ( $charge !== false ) {
- // Create Woo Coupon
- $new_coupon_id = wp_insert_post( array(
- 'post_title' => $code,
- 'post_content' => '',
- 'post_status' => 'publish',
- 'post_author' => 1,
- 'post_type' => 'shop_coupon'
- ) );
- $balance = $balance-$amount;
- $balance = $mycred->number( $balance );
- // Update Coupon details
- update_post_meta( $new_coupon_id, 'discount_type', 'fixed_cart' );
- update_post_meta( $new_coupon_id, 'coupon_amount', $value );
- update_post_meta( $new_coupon_id, 'individual_use', 'no' );
- update_post_meta( $new_coupon_id, 'product_ids', '' );
- update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
- // Make sure you set usage_limit to 1 to prevent duplicate usage!!!
- update_post_meta( $new_coupon_id, 'usage_limit', 1 );
- update_post_meta( $new_coupon_id, 'usage_limit_per_user', 1 );
- update_post_meta( $new_coupon_id, 'limit_usage_to_x_items', '' );
- update_post_meta( $new_coupon_id, 'usage_count', '' );
- update_post_meta( $new_coupon_id, 'expiry_date', '' );
- update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
- update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
- // If $product_categories is not an empty string then add the product categories limit
- // to the coupon. Otherwise this is ignored.
- if ( $product_categories != '' )
- update_post_meta( $new_coupon_id, 'product_categories', array( $product_categories ) );
- update_post_meta( $new_coupon_id, 'exclude_product_categories', array() );
- update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' );
- update_post_meta( $new_coupon_id, 'minimum_amount', '' );
- update_post_meta( $new_coupon_id, 'customer_email', array() );
- // Update log entry to include code and value
- global $wpdb;
- $new_data = array( 'code' => $code, 'value' => $value, 'ref_type' => 'post', 'product_categories' => $product_categories, 'ex' => $exchange );
- $wpdb->update(
- $mycred->log_table,
- array( 'data' => serialize( $new_data ), 'ref_id' => $new_coupon_id ),
- array( 'data' => $code ),
- array( '%s', '%d' ),
- array( '%s' )
- );
- // Reset amount
- $amount = 0;
- }
- // Could not charge
- else {
- $error = 'You have reached your daily limit and can not create a coupon.';
- }
- }
- }
- // Show users current balance
- $output .= '
- <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
- // Error
- if ( $error !== false )
- $output .= '<p style="color:red;">' . $error . '</p>';
- // Success
- elseif ( $code !== false )
- $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
- // The form for those who have points
- if ( $balance > $mycred->zero() ) {
- $output .= '
- <form action="" method="post">
- <input type="hidden" name="mycred_to_woo[token]" value="' . wp_create_nonce( 'points-to-woo-coupon' ) . '" />';
- // If shortcode is not set to use a specific amount
- // we insert the amount field so one can be nominated
- if ( $original === NULL )
- $output .= '
- <label>Amount</label>
- <input type="text" size="5" name="mycred_to_woo[amount]" value="" />';
- $output .= '
- <input type="submit" name="submit" value="' . $button_label . '" />
- </form>';
- }
- // Not enough points
- else
- $output .= '<p>Not enough points to create coupons.</p>';
- return $output;
- }
- add_filter( 'mycred_woo_profit_share', 'mycred_pro_adjust_woo_share', 10, 3 );
- function mycred_pro_adjust_woo_share( $percentage, $order, $product ) {
- // Get custom field
- $check = get_post_meta( $product->ID, 'MYCRED_PROFIT_SHARE_PERCENT', true );
- if ( $check != '' )
- $percentage = $check;
- return $percentage;
- }
- /**
- * Adjust myCRED Point Rewards
- * Will move the points payout from when an order is "paid" to when
- * an order is "completed".
- * @version 1.0
- */
- add_action( 'after_setup_theme', 'mycred_pro_adjust_woo_rewards', 110 );
- function mycred_pro_adjust_woo_rewards() {
- remove_action( 'woocommerce_payment_complete', 'mycred_woo_payout_rewards' );
- add_action( 'woocommerce_order_status_completed', 'mycred_woo_payout_rewards' );
- }
- /**
- * Add Column Header
- * Add in the custom log column header on the main log page in the
- * admin area.
- * @version 1.0
- */
- add_filter( 'mycred_log_column_headers', 'mycred_pro_admin_log_columns', 10, 2 );
- function mycred_pro_admin_log_columns( $columns, $module ) {
- // Only appliable for admin area
- if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ! is_admin() ) return $columns;
- // Only applicable for myCRED Log page and not personal log
- if ( isset( $module->args['ctype'] ) && isset( $_GET['page'] ) && $_GET['page'] != $module->args['ctype'] . '_history' )
- $columns['column-ref'] = 'Reference';
- return $columns;
- }
- /**
- * Render Ref Column
- * Renders the result of the reference column.
- * @version 1.0
- */
- add_filter( 'mycred_log_column-ref', 'mycred_pro_custom_log_column', 10, 2 );
- function mycred_pro_custom_log_column( $content, $entry ) {
- $refs = mycred_get_all_references();
- if ( isset( $refs[ $entry->ref ] ) )
- return $refs[ $entry->ref ];
- return $entry->ref;
- }
- /**
- * View my Coupons
- * @since 1.4
- * @version 1.1
- */
- add_shortcode( 'mycred_my_coupons', 'mycred_pro_render_shortcode_my_coupons' );
- function mycred_pro_render_shortcode_my_coupons( $attr, $content ) {
- extract( shortcode_atts( array(
- 'user_id' => NULL,
- 'type' => '',
- 'ref' => 'points_to_coupon',
- 'number' => 10
- ), $attr ) );
- if ( ! function_exists( 'mycred' ) ) return 'myCRED is not installed';
- if ( $user_id === NULL && ! is_user_logged_in() ) return $content;
- if ( $user_id === NULL )
- $user_id = get_current_user_id();
- $args = array();
- $args['user_id'] = $user_id;
- $args['ref'] = $ref;
- $args['number'] = $number;
- $log = new myCRED_Query_Log( $args );
- $log->headers = array(
- 'coupon-code' => 'Coupon Code',
- 'coupon-status' => 'Status',
- 'coupon-date' => 'Date Created',
- 'coupon-value' => 'Value'
- );
- ob_start();
- if ( $log->have_entries() )
- $log->display();
- else
- echo '<p>You do not have any coupons</p>';
- $content = ob_get_contents();
- ob_end_clean();
- return $content;
- }
- /**
- * View my Coupons Column : Code
- * @version 1.2
- */
- add_filter( 'mycred_log_coupon-code', 'mycred_pro_column_coupon_code', 10, 2 );
- function mycred_pro_column_coupon_code( $content, $log ) {
- $data = maybe_unserialize( $log->data );
- return $data['code'];
- }
- /**
- * View my Coupons Column : Status
- * @version 1.0
- */
- add_filter( 'mycred_log_coupon-status', 'mycred_pro_column_coupon_status', 10, 2 );
- function mycred_pro_column_coupon_status( $content, $log ) {
- if ( get_the_title( $log->ref_id ) == '' )
- return 'Used';
- else
- return 'Unused';
- }
- /**
- * View my Coupons Column : Date
- * @version 1.0
- */
- add_filter( 'mycred_log_coupon-date', 'mycred_pro_column_coupon_date', 10, 2 );
- function mycred_pro_column_coupon_date( $content, $log ) {
- return date_i18n( 'Y-m-d', $log->time );
- }
- /**
- * View my Coupons Column : Value
- * @version 1.1
- */
- add_filter( 'mycred_log_coupon-value', 'mycred_pro_column_coupon_value', 10, 2 );
- function mycred_pro_column_coupon_value( $content, $log ) {
- $data = maybe_unserialize( $log->data );
- if ( isset( $data['value'] ) )
- return '$' . $data['value'];
- else
- return '-';
- }
- /**
- * myCRED Profit Share for Woo
- * Replaces the default profit share system in myCRED for WooCommerce
- * orders to payout the "line_subtotal" instead of "line_total".
- * @version 1.0
- */
- add_action( 'mycred_paid_for_woo', 'mycred_pro_woo_profit_payouts', 10, 3 );
- function mycred_pro_woo_profit_payouts( $order, $user, $gateway ) {
- // Default Profit Share ( in percentage )
- $profit_share = 10;
- // Get Items
- $items = $order->get_items();
- // Loop though items
- foreach ( $items as $item ) {
- // Get Product
- $product = get_post( (int) $item['product_id'] );
- // Continue if product has just been deleted or owner is buyer
- if ( $product === NULL || $product->post_author == $user ) continue;
- // Calculate Share and let others play
- $percentage = apply_filters( 'mycred_woo_profit_share', $profit_share, $order, $product, $gateway );
- if ( $percentage == 0 ) continue;
- // Sub total instead of total should exclude coupon usage
- $share = ( $percentage / 100 ) * $item['line_subtotal'];
- // Payout
- $gateway->mycred->add_creds(
- 'store_sale',
- $product->post_author,
- $share,
- $gateway->profit_sharing_log,
- $product->ID,
- array( 'ref_type' => 'post' ),
- $gateway->mycred_type
- );
- }
- }
- add_filter( 'mycred_add', 'restrict_points_to_once_per_day', 1, 3 );
- function restrict_points_to_once_per_day( $reply, $request, $mycred ) {
- // If something else already declined this, respect it
- if ( $reply === false ) return $reply;
- extract( $request );
- // First we need to make sure this is a reference we need to limit
- $my_custom_references = array( 'companya', 'companyb', 'companyc' );
- if ( ! in_array( $ref, $my_custom_references ) ) return $reply;
- $today = mycred_get_total_by_time( 'today', 'now', $ref, $user_id, $type );
- // Decline if the user has received this amount once today.
- if ( $today >= $amount )
- return false;
- return $reply;
- }
- /**
- * Replace Post Template Tags
- * version 1.0
- */
- function mycred_pro_temp_post_title_value( $content, $entry ) {
- $content = str_replace( '%post_title%', $entry->data, $content );
- return $content;
- }
- /* Your code goes above here. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement