Created
December 14, 2017 08:40
-
-
Save acanza/32785f77cfc8c68be871863dd28593db to your computer and use it in GitHub Desktop.
Crea un cupón descuento único para cada cliente al terminar su compra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Crea un cupón descuento único para cada cliente al terminar su compra | |
| if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) ){ | |
| add_filter( 'woocommerce_thankyou_order_received_text', 'create_automatic_coupon_only_for_this_customer', 99, 2 ); | |
| function create_automatic_coupon_only_for_this_customer( $message, $order ){ | |
| // Configuración del cupón | |
| $discount_type = 'percent'; // Aquí defines el tipo de descuento que aplica este cupón. Por defecto está configurado en porcentaje 'percent', para descuento fijo usa 'fixed_cart' | |
| $amount = 30; //Aquí defines la cantidad de descuento asociada a este cupón | |
| $expiration = 12; // Aquí debes indicar la validez del cupón en días | |
| // Elimina cupones caducados y comprueba si ya existe un cupón asociado a este pedido | |
| $coupon_id = clean_expired_coupons_and_check_coupons_order( $order ); | |
| $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) ); | |
| if ( $order->get_id() > 0 ) { | |
| // Comprueba que el pedido existe y que todavía no hay cupón asociado al pedido | |
| if ( $order->get_order_key() == $order_key ) { | |
| if ( !$coupon_id ) { | |
| //Crea un código de cupón único para este cliente | |
| $customer_email = $order->get_billing_email(); | |
| $customer_email = explode( '@', $customer_email ); | |
| $customer_email = $customer_email[0]; | |
| $sufix = date( 'Hms' ); | |
| // Calcula la fecha de caducidad | |
| $date = date( 'Y-m-d' ); | |
| $expiry_date = strtotime( $date .'+ '. $expiration .' days' ); | |
| $expiry_date = date( 'Y-m-d', $expiry_date ); | |
| $args = array( | |
| 'coupon_code' => $customer_email.'_'.$sufix, | |
| 'discount_type' => $discount_type, | |
| 'amount' => $amount, | |
| 'individual_use' => 'yes', | |
| 'usage_limit' => 1, | |
| 'product_ids' => '', | |
| 'exclude_product_ids' => '', | |
| 'expiry_date' => $expiry_date, | |
| 'apply_before_tax' => '', | |
| 'free_shipping' => 'no', | |
| ); | |
| $coupon_code = strtoupper( $args[ 'coupon_code' ] ); | |
| $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 ); | |
| update_post_meta( $new_coupon_id, 'discount_type', $args[ 'discount_type' ] ); | |
| update_post_meta( $new_coupon_id, 'coupon_amount', $args[ 'amount' ] ); | |
| update_post_meta( $new_coupon_id, 'individual_use', $args[ 'individual_use' ] ); | |
| update_post_meta( $new_coupon_id, 'product_ids', $args[ 'product_ids' ] ); | |
| update_post_meta( $new_coupon_id, 'exclude_product_ids', $args[ 'exclude_product_ids' ] ); | |
| update_post_meta( $new_coupon_id, 'usage_limit', $args[ 'usage_limit' ] ); | |
| update_post_meta( $new_coupon_id, 'expiry_date', $args[ 'expiry_date' ] ); | |
| update_post_meta( $new_coupon_id, 'apply_before_tax', $args[ 'apply_before_tax' ] ); | |
| update_post_meta( $new_coupon_id, 'free_shipping', $args[ 'free_shipping' ] ); | |
| update_post_meta( $new_coupon_id, 'order-id', $order->get_id() ); | |
| if ( is_wp_error( $new_coupon_id ) ){ | |
| return false; | |
| } | |
| }else{ | |
| $coupon = get_post( $coupon_id ); | |
| $coupon_code = $coupon->post_title; | |
| } | |
| // Muestra el cupón en la página de finalizar compra | |
| $message .= get_coupon_discount_for_the_next_purchase( $coupon_code, $expiration, $amount, $discount_type ); | |
| } | |
| } | |
| return $message; | |
| } | |
| function get_coupon_discount_for_the_next_purchase( $coupon_code, $expiration, $amount, $discount_type ){ | |
| $discount = $amount; | |
| $discount .= 'percent' == $discount_type? '%' : get_woocommerce_currency(); | |
| ob_start(); | |
| ?> | |
| <div style="margin: 0 0 3.5em; text-align:center;"> | |
| <p>Para agradecerte tu confianza en nosotros, te hemos preparado un <strong>cupón de descuento del <?php echo $discount; ?> para tu próxima compra:</strong></p> | |
| <h3 style="text-align:center;">TU DESCUENTO</h3> | |
| <div style="text-align: center; font-weight: bold; border: 2px #c7c7c7; border-style: dashed; padding: 13px; width: 50%; margin: 10px auto;"><?php echo $coupon_code;?></div> | |
| <p><strong>El cupón tiene una validez de <?php echo $expiration; ?> días</strong>. Pero no lo dejes demasiado porque una vez transcurrido ese periodo, el cupón ya no será valido.</p> | |
| </div> | |
| <?php | |
| $content = ob_get_contents(); | |
| ob_end_clean(); | |
| return $content; | |
| } | |
| function clean_expired_coupons_and_check_coupons_order( $order = false ){ | |
| $coupon_exist = false; | |
| if ( isset( $order ) ) { | |
| $arg = array( | |
| 'post_type' => array( 'shop_coupon' ), | |
| 'meta_key' => 'order-id' | |
| ); | |
| $auto_coupon_list = new WP_Query( $arg ); | |
| foreach ( $auto_coupon_list->posts as $key => $coupon ) { | |
| // Elimina cupones caducados | |
| $coupon_expiry_date = get_post_meta( $coupon->ID, 'expiry_date', true ); | |
| $today_date = date( 'Y-m-d' ); | |
| if ( !empty( $coupon_expiry_date ) && ( $today_date > $coupon_expiry_date ) ) { | |
| wp_delete_post( $coupon->ID ); | |
| continue; | |
| } | |
| // Busca un cupón automático asociado a este pedido | |
| $order_id_coupon_vinculated = get_post_meta( $coupon->ID, 'order-id', true ); | |
| if ( !$coupon_exist && ( $order_id_coupon_vinculated == $order->get_id() ) ) { | |
| $coupon_exist = $coupon->ID; | |
| } | |
| } | |
| } | |
| return $coupon_exist; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment