Home / Archive / MemberPress: Allow Importing Coupons Associated With Affiliates (Easy Affiliate)
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Allow Importing Coupons Associated With Affiliates (Easy Affiliate)

This code snippet is used to add the additional importing option for Easy Affiliate data. The code snippet requires the Easy Affiliate plugin to be installed and anctivated on the website.

When the code snippet is added to the website, it will add a new option in the dropdown menu for selecting the type of file to import. This options is located at Dashbaord > MemberPress > Import.

The new option added by this code snippet will allow the import of MemberPress coupons associated with affiliates from the Easy Affiliate plugin.

Code Preview
php
<?php
function mpimp_load_expanded_coupon_importer() {
	 global $mpimp;
	 $mpimp->importers['couponaffiliate'] = 'MpimpExtendedCouponsImporter';
}
add_action('admin_init', 'mpimp_load_expanded_coupon_importer', 110); //has to be after the MP hook function runs
class MpimpExtendedCouponsImporter extends MpimpCouponsImporter {
  public function form() { }
  public function import($row,$args) {
	$ea_active = is_plugin_active('easy-affiliate/easy-affiliate.php') ? true : false;
    $required = array('type','discount');
    $this->check_required('coupons', array_keys($row), $required);
    $random_code = MeprUtils::random_string( 10, false, true );
    // Merge in default values where applicable
    $row = array_merge( array( 'code' => $random_code,
                               'expires_at' => null,
                               'usage_amount' => 0,
                               'discount_mode' => 'standard' ), $row ); // 0 = infinite uses
    $coupon = new MeprCoupon();
    $valid_products = array();
    $valid_types = array('percent','dollar');
    $override = false;
    foreach( $row as $col => $cell ) {
      if( preg_match( '#^product_id_.*$#', $col ) ) {
        if(!empty($cell)) {
          $this->fail_if_not_valid_product_id( $cell );
          $valid_products[] = $cell;
        }
      }
      else {
        switch( $col ) {
          // Should consider adding valid_products and usage_count to this switch
          case "code":
            $this->fail_if_empty($col, $cell);
            $coupon->post_title = empty($cell)?$random_code:$cell;
            break;
          case "discount":
            $this->fail_if_empty($col, $cell);
            $this->fail_if_not_number($col, $cell);
            $coupon->discount_amount = $cell;
            break;
          case "type":
            $this->fail_if_empty($col, $cell);
            $this->fail_if_not_in_enum($col, $cell, $valid_types);
            $coupon->discount_type = $cell;
            break;
          case "usage_amount":
            $this->fail_if_not_number($col, $cell);
            $coupon->usage_amount = empty($cell)?0:$cell;
            break;
          case "discount_mode":
            //Defaults to 'standard' above
            if($cell == 'first-payment' || $cell == 'trial-override') {
              $coupon->discount_mode = strtolower($cell);
            }
            break;
          case "first_payment_discount_type":
            if(!empty(trim($cell))) {
              $this->fail_if_not_in_enum($col, $cell, $valid_types);
              $coupon->first_payment_discount_type = $cell;
            }
            break;
          case "first_payment_discount_amount":
            if(!empty(trim($cell))) {
              $this->fail_if_not_number($col, $cell);
              $coupon->first_payment_discount_amount = $cell;
            }
            break;
          case "trial_days":
            $this->fail_if_not_number($col, $cell);
            $coupon->trial_days = (int)$cell;
            break;
          case "trial_amount":
            $this->fail_if_not_number($col, $cell);
            $coupon->trial_amount = ((float)$cell <= 0.00)?0.00:(float)$cell;
            break;
          case "description":
            $coupon->post_content = trim($cell);
            break;
          case "use_on_upgrades":
            $coupon->use_on_upgrades = ! empty( intval( $cell ) ) ? true : false;
            break;
          case "expires_at":
            if(empty($cell)) {
              $coupon->should_expire = false;
              $coupon->expires_on = 0;
            }
            else {
              // Some spreadsheets force the use of '/' instead of '-' to separate dates
              $cell = preg_replace('#/#','-',$cell);
              $this->fail_if_not_date($col, $cell);
              $coupon->should_expire = true;
              $coupon->expires_on = strtotime($cell); //DATES must be in d/m/y or d-m-y format for strtotime to work properly
            }
            break;
          case "starts_on":
            if(empty($cell)) {
              $coupon->should_start = false;
              $coupon->starts_on = 0;
            }
            else {
              // Some spreadsheets force the use of '/' instead of '-' to separate dates
              $cell = preg_replace('#/#','-',$cell);
              $this->fail_if_not_date($col, $cell);
              $coupon->should_start = true;
              $coupon->starts_on = strtotime($cell); //DATES must be in d/m/y or d-m-y format for strtotime to work properly
            }
            break;
          case "override_existing":
            $override = 1 == $cell;
            break;
		  case "affiliate_user_id":
				
        }
      }
    }
    if ( true === $override ) {
      $maybe_coupon = MeprCoupon::get_one_from_code( $coupon->post_title, true );
      if ( ! empty( $maybe_coupon->ID ) ) {
        $coupon->ID = $maybe_coupon->ID;
      }
    }
    $coupon->valid_products = $valid_products;
    if( $coupon_id = $coupon->store() ) {
	  if($ea_active && isset($row['affliate_user_id'])) {		  
		 //If we are also using Easy Affiliate, and an affiliate user id is provided:
		 if (is_numeric($row['affliate_user_id']) && $row['affliate_user_id'] > 0) {		
			 if ( ! add_post_meta($coupon_id, 'wafp_coupon_affiliate_enabled', 1, true) ) { 
               update_post_meta ($coupon_id, 'wafp_coupon_affiliate_enabled', 1);
             }
			 if ( ! add_post_meta($coupon_id, 'wafp_coupon_affiliate', $row['affliate_user_id'], true) ) { 
               update_post_meta ($coupon_id, 'wafp_coupon_affiliate', $row['affliate_user_id']);
             }
		 } else {
			throw new Exception(__('Coupon created, but failed to associate affiliate, missing or invalid affiliate user id.'));
		 }
	  }
      return sprintf(__('Coupon (ID = %s) was %s successfully'), $coupon_id, true === $override ? __('updated') : __('created'));
	} else {
      throw new Exception(__('Coupon failed to be created'));
	}
  }
}

Comments

Add a Comment