Advertisement
designbymerovingi

myCRED Payout based on Woo Product Variation

Apr 9th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1. /**
  2.  * Award Points on Order Completed
  3.  * Award 100 points for each license a user purchases.
  4.  * Licenses are a product variation.
  5.  * @version 1.0
  6.  */
  7. add_action( 'woocommerce_order_status_completed', 'mycred_pro_award_based_on_variation' );
  8. function mycred_pro_award_based_on_variation( $order_id ) {
  9.  
  10.     // Make sure myCRED is installed
  11.     if ( ! function_exists( 'mycred' ) ) return;
  12.  
  13.     $order = new WC_Order( $order_id );
  14.  
  15.     $items = $order->get_items();
  16.  
  17.     $payout = 0;
  18.  
  19.     // Loop though the order items to count licenses
  20.     foreach ( $items as $item ) {
  21.  
  22.         // Only applicable for products that have the variation "pa_license"
  23.         if ( ! isset( $item['pa_license'] ) ) continue;
  24.  
  25.         if ( $item['pa_license'] == 'single-site-license' )
  26.             $number = 1;
  27.         elseif ( $item['pa_license'] == '5-site-licenses' )
  28.             $number = 5;
  29.         elseif ( $item['pa_license'] == '10-site-licenses' )
  30.             $number = 10;
  31.         else
  32.             $number = 1;
  33.  
  34.         $payout = $payout + ( $number * 100 );
  35.  
  36.     }
  37.  
  38.     if ( $payout > 0 ) {
  39.  
  40.         $point_type = 'mycred_default';
  41.         $mycred     = mycred( $point_type );
  42.  
  43.         if ( ! $mycred->exclude_user( $order->user_id ) )
  44.             $mycred->add_creds(
  45.                 'store_reward',
  46.                 $order->user_id,
  47.                 $payout,
  48.                 'Store purchase reward',
  49.                 $order_id,
  50.                 array( 'ref_id' => 'post' ),
  51.                 $point_type
  52.             );
  53.  
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement