Advertisement
verygoodplugins

Untitled

Aug 5th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. function wpf_sync_coupon( $affiliate_id ) {
  2.  
  3.     if ( ! function_exists( 'wp_fusion' ) ) {
  4.         return;
  5.     }
  6.  
  7.     $args = array(
  8.         'post_type'   => 'shop_coupon',
  9.         'numberposts' => 1,
  10.         'meta_key'    => 'affwp_discount_affiliate',
  11.         'meta_value'  => $affiliate_id,
  12.         'fields'      => 'ids',
  13.     );
  14.  
  15.     $coupons = get_posts( $args );
  16.  
  17.     if ( ! empty( $coupons ) ) {
  18.  
  19.         $coupon = new WC_Coupon( $coupons[0] );
  20.  
  21.         $affiliate = affwp_get_affiliate( $affiliate_id );
  22.  
  23.         wp_fusion()->user->push_user_meta( $affiliate->user_id, array( 'affwp_coupon_code' => $coupon->get_code() ) );
  24.  
  25.     }
  26.  
  27. }
  28.  
  29. add_action( 'affwp_insert_affiliate', 'wpf_sync_coupon', 20 );
  30.  
  31.  
  32. function add_coupon_field( $meta_fields ) {
  33.  
  34.     $meta_fields['affwp_coupon_code'] = array( 'label' => 'Generated coupon code', 'group' => 'awp' );
  35.  
  36.     return $meta_fields;
  37.  
  38. }
  39.  
  40. add_filter( 'wpf_meta_fields', 'add_coupon_field' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement