| |
| <?php
|
| function mpimp_load_expanded_coupon_importer() {
|
| global $mpimp;
|
| $mpimp->importers['couponaffiliate'] = 'MpimpExtendedCouponsImporter';
|
| }
|
| add_action('admin_init', 'mpimp_load_expanded_coupon_importer', 110);
|
|
|
| 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 );
|
|
|
|
|
| $row = array_merge( array( 'code' => $random_code,
|
| 'expires_at' => null,
|
| 'usage_amount' => 0,
|
| 'discount_mode' => 'standard' ), $row );
|
|
|
| $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 ) {
|
|
|
| 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":
|
|
|
| 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 {
|
|
|
| $cell = preg_replace('#/#','-',$cell);
|
| $this->fail_if_not_date($col, $cell);
|
| $coupon->should_expire = true;
|
| $coupon->expires_on = strtotime($cell);
|
| }
|
| break;
|
| case "starts_on":
|
| if(empty($cell)) {
|
| $coupon->should_start = false;
|
| $coupon->starts_on = 0;
|
| }
|
| else {
|
|
|
| $cell = preg_replace('#/#','-',$cell);
|
| $this->fail_if_not_date($col, $cell);
|
| $coupon->should_start = true;
|
| $coupon->starts_on = strtotime($cell);
|
| }
|
| 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 (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