Untitled
unknown
php
3 years ago
2.1 kB
5
Indexable
add_action( 'wp_ajax_nopriv_registro_mailchimp', "registro_mailchimp" ); function registro_mailchimp(){ $mailchimp = new MailchimpMarketing\ApiClient(); $mailchimp->setConfig([ 'apiKey' => '', 'server' => '' ]); $response = new stdClass(); $response->result = false; parse_str( $_POST['data'], $data); $response->data = $data; try { $merge_vars = array( 'EMAIL' => $data['email'], 'FNAME' => $data['nombre'], 'LNAME' => $data['apellido'], 'BIRTHDAY' => $data['mes'] . '/' . $data['dia'] ); $response->result = $mailchimp->lists->addListMember("8d542b62ab", [ "email_address" => $data['email'], "full_name" => $data['nombre'], "status" => "subscribed", "source" => "Popup Newsletter", "merge_fields" => $merge_vars ]); } catch (Exception $e) { } if( $response->result != false ){ $response->cupon = $response->result->unique_email_id; $coupon_code = $response->cupon; // Code $amount = '15'; // Amount $discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product $coupon = array( 'post_title' => $coupon_code, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'shop_coupon'); $new_coupon_id = wp_insert_post( $coupon ); // Add meta update_post_meta( $new_coupon_id, 'discount_type', $discount_type ); update_post_meta( $new_coupon_id, 'coupon_amount', $amount ); update_post_meta( $new_coupon_id, 'individual_use', 'yes' ); update_post_meta( $new_coupon_id, 'product_ids', '' ); update_post_meta( $new_coupon_id, 'exclude_product_ids', '' ); update_post_meta( $new_coupon_id, 'usage_limit', '1' ); update_post_meta( $new_coupon_id, 'expiry_date', '' ); update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' ); update_post_meta( $new_coupon_id, 'free_shipping', 'no' ); $response->cupon = $response->result->unique_email_id; } echo json_encode( $response ); wp_die(); }
Editor is loading...