Advertisement
designbymerovingi

added support for email notice

Jan 23rd, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.40 KB | None | 0 0
  1.  
  2. /**
  3. * Plugin Name: mac-custom-mycred-coupon
  4. * Description: This plugin contains custom functions from mycred.
  5. * Version: 0.2
  6. */
  7.  
  8. /* Your code goes below here. */
  9.  
  10. /* allows for accrual of points when when paying with points */
  11. add_filter( 'mycred_woo_reward_mycred_payment', '__return_true' );
  12.  
  13. /**
  14.  * Convert myCRED Points into WooCommerce Coupon
  15.  * @version 1.4
  16.  */
  17. add_shortcode( 'mycred_to_woo_coupon', 'mycred_pro_render_points_to_coupon' );
  18. function mycred_pro_render_points_to_coupon( $atts, $content = NULL ) {
  19.         // Users must be logged in
  20.         if ( ! is_user_logged_in() )
  21.                 return 'You must be logged in to generate store coupons.';
  22.  
  23.         // myCRED must be enabled
  24.         if ( ! function_exists( 'mycred' ) )
  25.                 return 'myCRED must be enabled to use this shortcode';
  26.  
  27.         extract( shortcode_atts( array(
  28.                 'exchange'           => 1,
  29.                 'amount'             => NULL,
  30.                 'type'               => 'mycred_default',
  31.                 'button_label'       => 'Create Coupon',
  32.                 'reference'          => 'points_to_coupon',
  33.                 'log'                => '%plural% conversion into store coupon: %post_title%',
  34.                 'product_categories' => ''
  35.         ), $atts ) );
  36.  
  37.         // Load myCRED
  38.         $mycred = mycred( $type );
  39.  
  40.         // Prep
  41.         $original = $amount;
  42.         $error = $code = false;
  43.         $output = '';
  44.         $user_id = get_current_user_id();
  45.  
  46.         // No need to show this for excluded users
  47.         if ( $mycred->exclude_user( $user_id ) ) return;
  48.  
  49.         $balance = $mycred->get_users_balance( $user_id );
  50.  
  51.         // Form submission
  52.         if ( isset( $_POST['mycred_to_woo'] ) && wp_verify_nonce( $_POST['mycred_to_woo']['token'], 'points-to-woo-coupon' ) ) {
  53.  
  54.                 // Make sure amounts are always positive
  55.                 if ( $amount === NULL ) {
  56.                         $original = NULL;
  57.                         $amount = abs( $_POST['mycred_to_woo']['amount'] );
  58.                 }
  59.  
  60.                 // Exchange rate
  61.                 $value = $mycred->number( $amount*$exchange );
  62.  
  63.                 // Make sure amount is not zero
  64.                 if ( $amount == $mycred->zero() )
  65.                         $error = 'Amount can not be zero';
  66.  
  67.                 // Make sure user has enough points
  68.                 if ( $amount > $balance )
  69.                         $error = 'Insufficient Funds. Please try a lower amount';
  70.  
  71.                 // If no errors
  72.                 if ( $error === false ) {
  73.  
  74.                         $code = wp_generate_password( 12, false, false );
  75.  
  76.                         // Deduct points from user
  77.             add_filter( 'mycred_parse_log_entry', 'mycred_pro_temp_post_title_value', 10, 2 );
  78.                         $charge = $mycred->add_creds(
  79.                                 $reference,
  80.                                 $user_id,
  81.                                 0-$amount,
  82.                                 $log,
  83.                                 0,
  84.                                 $code,
  85.                                 $type
  86.                         );
  87.             remove_filter( 'mycred_parse_log_entry', 'mycred_pro_temp_post_title_value', 10, 2 );
  88.                        
  89.                         // If points were deducted create coupon
  90.                         if ( $charge !== false ) {
  91.  
  92.                                 // Create Woo Coupon
  93.                                 $new_coupon_id = wp_insert_post( array(
  94.                                         'post_title'   => $code,
  95.                                         'post_content' => '',
  96.                                         'post_status'  => 'publish',
  97.                                         'post_author'  => 1,
  98.                                         'post_type'    => 'shop_coupon'
  99.                                 ) );
  100.  
  101.                                 $balance = $balance-$amount;
  102.                                 $balance = $mycred->number( $balance );
  103.  
  104.                                 // Update Coupon details
  105.                                 update_post_meta( $new_coupon_id, 'discount_type', 'fixed_cart' );
  106.                                 update_post_meta( $new_coupon_id, 'coupon_amount', $value );
  107.                                 update_post_meta( $new_coupon_id, 'individual_use', 'no' );
  108.                                 update_post_meta( $new_coupon_id, 'product_ids', '' );
  109.                                 update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
  110.  
  111.                                 // Make sure you set usage_limit to 1 to prevent duplicate usage!!!
  112.                                 update_post_meta( $new_coupon_id, 'usage_limit', 1 );
  113.                                 update_post_meta( $new_coupon_id, 'usage_limit_per_user', 1 );
  114.                                 update_post_meta( $new_coupon_id, 'limit_usage_to_x_items', '' );
  115.                                 update_post_meta( $new_coupon_id, 'usage_count', '' );
  116.                                 update_post_meta( $new_coupon_id, 'expiry_date', '' );
  117.                                 update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
  118.                                 update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
  119.  
  120.                                 // If $product_categories is not an empty string then add the product categories limit
  121.                                 // to the coupon. Otherwise this is ignored.
  122.                                 if ( $product_categories != '' )
  123.                                         update_post_meta( $new_coupon_id, 'product_categories', array( $product_categories ) );
  124.  
  125.                                 update_post_meta( $new_coupon_id, 'exclude_product_categories', array() );
  126.                                 update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' );
  127.                                 update_post_meta( $new_coupon_id, 'minimum_amount', '' );
  128.                                 update_post_meta( $new_coupon_id, 'customer_email', array() );
  129.  
  130.                                 // Update log entry to include code and value
  131.                                 global $wpdb;
  132.  
  133.                                 $new_data = array( 'code' => $code, 'value' => $value, 'ref_type' => 'post', 'product_categories' => $product_categories, 'ex' => $exchange );
  134.                                 $wpdb->update(
  135.                                         $mycred->log_table,
  136.                                         array( 'data' => serialize( $new_data ), 'ref_id' => $new_coupon_id ),
  137.                                         array( 'data' => $code ),
  138.                                         array( '%s', '%d' ),
  139.                                         array( '%s' )
  140.                                 );
  141.  
  142.                                 // Reset amount
  143.                                 $amount = 0;
  144.  
  145.                         }
  146.  
  147.                         // Could not charge
  148.                         else {
  149.  
  150.                                 $error = 'You have reached your daily limit and can not create a coupon.';
  151.  
  152.                         }
  153.  
  154.                 }
  155.  
  156.         }
  157.  
  158.         // Show users current balance
  159.         $output .= '
  160. <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
  161.  
  162.         // Error
  163.         if ( $error !== false )
  164.                 $output .= '<p style="color:red;">' . $error . '</p>';
  165.  
  166.         // Success
  167.         elseif ( $code !== false )
  168.                 $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
  169.  
  170.         // The form for those who have points
  171.         if ( $balance > $mycred->zero() ) {
  172.                 $output .= '
  173. <form action="" method="post">
  174.        <input type="hidden" name="mycred_to_woo[token]" value="' . wp_create_nonce( 'points-to-woo-coupon' ) . '" />';
  175.        
  176.                 // If shortcode is not set to use a specific amount
  177.                 // we insert the amount field so one can be nominated
  178.                 if ( $original === NULL )
  179.                         $output .= '
  180.        <label>Amount</label>
  181.        <input type="text" size="5" name="mycred_to_woo[amount]" value="" />';
  182.  
  183.                 $output .= '
  184.        <input type="submit" name="submit" value="' . $button_label . '" />
  185. </form>';
  186.  
  187.         }
  188.         // Not enough points
  189.         else
  190.                 $output .= '<p>Not enough points to create coupons.</p>';
  191.  
  192.         return $output;
  193. }
  194.  
  195. add_filter( 'mycred_woo_profit_share', 'mycred_pro_adjust_woo_share', 10, 3 );
  196. function mycred_pro_adjust_woo_share( $percentage, $order, $product ) {
  197.  
  198.     // Get custom field
  199.         $check = get_post_meta( $product->ID, 'MYCRED_PROFIT_SHARE_PERCENT', true );
  200.         if ( $check != '' )
  201.                 $percentage = $check;
  202.        
  203.         return $percentage;
  204. }
  205.  
  206. /**
  207.  * Adjust myCRED Point Rewards
  208.  * Will move the points payout from when an order is "paid" to when
  209.  * an order is "completed".
  210.  * @version 1.0
  211.  */
  212. add_action( 'after_setup_theme', 'mycred_pro_adjust_woo_rewards', 110 );
  213. function mycred_pro_adjust_woo_rewards() {
  214.  
  215.         remove_action( 'woocommerce_payment_complete', 'mycred_woo_payout_rewards' );
  216.         add_action( 'woocommerce_order_status_completed', 'mycred_woo_payout_rewards' );
  217.  
  218. }
  219.  
  220. /**
  221.  * Add Column Header
  222.  * Add in the custom log column header on the main log page in the
  223.  * admin area.
  224.  * @version 1.0
  225.  */
  226. add_filter( 'mycred_log_column_headers', 'mycred_pro_admin_log_columns', 10, 2 );
  227. function mycred_pro_admin_log_columns( $columns, $module ) {
  228.  
  229.     // Only appliable for admin area
  230.     if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ! is_admin() ) return $columns;
  231.  
  232.     // Only applicable for myCRED Log page and not personal log
  233.     if ( isset( $module->args['ctype'] ) && isset( $_GET['page'] ) && $_GET['page'] != $module->args['ctype'] . '_history' )
  234.         $columns['column-ref'] = 'Reference';
  235.  
  236.     return $columns;
  237.  
  238. }
  239.  
  240. /**
  241.  * Render Ref Column
  242.  * Renders the result of the reference column.
  243.  * @version 1.0
  244.  */
  245. add_filter( 'mycred_log_column-ref', 'mycred_pro_custom_log_column', 10, 2 );
  246. function mycred_pro_custom_log_column( $content, $entry ) {
  247.  
  248.     $refs = mycred_get_all_references();
  249.     if ( isset( $refs[ $entry->ref ] ) )
  250.         return $refs[ $entry->ref ];
  251.  
  252.     return $entry->ref;
  253.  
  254. }
  255.  
  256. /**
  257.  * View my Coupons
  258.  * @since 1.4
  259.  * @version 1.1
  260.  */
  261. add_shortcode( 'mycred_my_coupons', 'mycred_pro_render_shortcode_my_coupons' );
  262. function mycred_pro_render_shortcode_my_coupons( $attr, $content ) {
  263.  
  264.         extract( shortcode_atts( array(
  265.                 'user_id' => NULL,
  266.                 'type'    => '',
  267.                 'ref'     => 'points_to_coupon',
  268.                 'number'  => 10
  269.         ), $attr ) );
  270.  
  271.         if ( ! function_exists( 'mycred' ) ) return 'myCRED is not installed';
  272.  
  273.         if ( $user_id === NULL && ! is_user_logged_in() ) return $content;
  274.         if ( $user_id === NULL )
  275.                 $user_id = get_current_user_id();
  276.  
  277.         $args = array();
  278.  
  279.         $args['user_id'] = $user_id;
  280.         $args['ref'] = $ref;
  281.         $args['number'] = $number;
  282.  
  283.         $log = new myCRED_Query_Log( $args );
  284.  
  285.         $log->headers = array(
  286.                 'coupon-code'   => 'Coupon Code',
  287.                 'coupon-status' => 'Status',
  288.                 'coupon-date'   => 'Date Created',
  289.                 'coupon-value'  => 'Value'
  290.         );
  291.  
  292.         ob_start();
  293.        
  294.         if ( $log->have_entries() )
  295.                 $log->display();
  296.  
  297.         else
  298.                 echo '<p>You do not have any coupons</p>';
  299.  
  300.         $content = ob_get_contents();
  301.         ob_end_clean();
  302.  
  303.         return $content;
  304.  
  305. }
  306.  
  307. /**
  308.  * View my Coupons Column : Code
  309.  * @version 1.2
  310.  */
  311. add_filter( 'mycred_log_coupon-code', 'mycred_pro_column_coupon_code', 10, 2 );
  312. function mycred_pro_column_coupon_code( $content, $log ) {
  313.  
  314.     $data = maybe_unserialize( $log->data );
  315.     return $data['code'];
  316.  
  317. }
  318.  
  319. /**
  320.  * View my Coupons Column : Status
  321.  * @version 1.0
  322.  */
  323. add_filter( 'mycred_log_coupon-status', 'mycred_pro_column_coupon_status', 10, 2 );
  324. function mycred_pro_column_coupon_status( $content, $log ) {
  325.  
  326.         if ( get_the_title( $log->ref_id ) == '' )
  327.                 return 'Used';
  328.         else
  329.                 return 'Unused';
  330.  
  331. }
  332.  
  333. /**
  334.  * View my Coupons Column : Date
  335.  * @version 1.0
  336.  */
  337. add_filter( 'mycred_log_coupon-date', 'mycred_pro_column_coupon_date', 10, 2 );
  338. function mycred_pro_column_coupon_date( $content, $log ) {
  339.  
  340.         return date_i18n( 'Y-m-d', $log->time );
  341.  
  342. }
  343.  
  344. /**
  345.  * View my Coupons Column : Value
  346.  * @version 1.1
  347.  */
  348. add_filter( 'mycred_log_coupon-value', 'mycred_pro_column_coupon_value', 10, 2 );
  349. function mycred_pro_column_coupon_value( $content, $log ) {
  350.  
  351.         $data = maybe_unserialize( $log->data );
  352.         if ( isset( $data['value'] ) )
  353.                 return '$' . $data['value'];
  354.         else
  355.                 return '-';
  356.  
  357. }
  358.  
  359. /**
  360.  * myCRED Profit Share for Woo
  361.  * Replaces the default profit share system in myCRED for WooCommerce
  362.  * orders to payout the "line_subtotal" instead of "line_total".
  363.  * @version 1.0
  364.  */
  365. add_action( 'mycred_paid_for_woo', 'mycred_pro_woo_profit_payouts', 10, 3 );
  366. function mycred_pro_woo_profit_payouts( $order, $user, $gateway ) {
  367.  
  368.         // Default Profit Share ( in percentage )
  369.         $profit_share = 10;
  370.  
  371.         // Get Items
  372.         $items = $order->get_items();
  373.  
  374.         // Loop though items
  375.         foreach ( $items as $item ) {
  376.  
  377.                 // Get Product
  378.                 $product = get_post( (int) $item['product_id'] );
  379.  
  380.                 // Continue if product has just been deleted or owner is buyer
  381.                 if ( $product === NULL || $product->post_author == $user ) continue;
  382.  
  383.                 // Calculate Share and let others play
  384.                 $percentage = apply_filters( 'mycred_woo_profit_share', $profit_share, $order, $product, $gateway );
  385.                 if ( $percentage == 0 ) continue;
  386.  
  387.                 // Sub total instead of total should exclude coupon usage
  388.                 $share = ( $percentage / 100 ) * $item['line_subtotal'];
  389.  
  390.                 // Payout
  391.                 $gateway->mycred->add_creds(
  392.                         'store_sale',
  393.                         $product->post_author,
  394.                         $share,
  395.                         $gateway->profit_sharing_log,
  396.                         $product->ID,
  397.                         array( 'ref_type' => 'post' ),
  398.                         $gateway->mycred_type
  399.                 );
  400.  
  401.         }
  402.  
  403. }
  404.  
  405. add_filter( 'mycred_add', 'restrict_points_to_once_per_day', 1, 3 );
  406. function restrict_points_to_once_per_day( $reply, $request, $mycred ) {
  407.  
  408.     // If something else already declined this, respect it
  409.     if ( $reply === false ) return $reply;
  410.  
  411.     extract( $request );
  412.  
  413.     // First we need to make sure this is a reference we need to limit
  414.     $my_custom_references = array( 'companya', 'companyb', 'companyc' );
  415.     if ( ! in_array( $ref, $my_custom_references ) ) return $reply;
  416.  
  417.     $today = mycred_get_total_by_time( 'today', 'now', $ref, $user_id, $type );
  418.  
  419.     // Decline if the user has received this amount once today.
  420.     if ( $today >= $amount )
  421.         return false;
  422.  
  423.     return $reply;
  424.  
  425. }
  426.  
  427. /**
  428.  * Replace Post Template Tags
  429.  * version 1.0
  430.  */
  431. function mycred_pro_temp_post_title_value( $content, $entry ) {
  432.  
  433.     $content = str_replace( '%post_title%', $entry->data, $content );
  434.     return $content;
  435.  
  436. }
  437.  
  438. /* Your code goes above here. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement