Instantly share code, notes, and snippets.
Last active
January 26, 2016 12:40
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save jgarciaruiz/d7f61aa41172137e38e2 to your computer and use it in GitHub Desktop.
Add bday field to register form using WP plugin yith-woocommerce-coupon-email-system-premium: lines 104-105
This file contains 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
<?php | |
/** | |
* This file belongs to the YIT Plugin Framework. | |
* | |
* This source file is subject to the GNU GENERAL PUBLIC LICENSE (GPL 3.0) | |
* that is bundled with this package in the file LICENSE.txt. | |
* It is also available through the world-wide-web at this URL: | |
* http://www.gnu.org/licenses/gpl-3.0.txt | |
*/ | |
if ( !defined( 'ABSPATH' ) ) { | |
exit; | |
} // Exit if accessed directly | |
/** | |
* Main class | |
* | |
* @class YITH_WC_Coupon_Email_System_Premium | |
* @package Yithemes | |
* @since 1.0.0 | |
* @author Your Inspiration Themes | |
*/ | |
if ( !class_exists( 'YITH_WC_Coupon_Email_System_Premium' ) ) { | |
class YITH_WC_Coupon_Email_System_Premium extends YITH_WC_Coupon_Email_System { | |
/** | |
* Returns single instance of the class | |
* | |
* @return \YITH_WC_Coupon_Email_System_Premium | |
* @since 1.0.0 | |
*/ | |
public static function get_instance() { | |
if ( is_null( self::$instance ) ) { | |
self::$instance = new self( $_REQUEST ); | |
} | |
return self::$instance; | |
} | |
/** | |
* Constructor | |
* | |
* @since 1.0.0 | |
* @return mixed | |
* @author Alberto Ruggiero | |
*/ | |
public function __construct() { | |
parent::__construct(); | |
$this->_email_templates = array( | |
'ywces-1' => array( | |
'folder' => '/emails/template-1', | |
'path' => YWCES_TEMPLATE_PATH | |
), | |
'ywces-2' => array( | |
'folder' => '/emails/template-2', | |
'path' => YWCES_TEMPLATE_PATH | |
), | |
'ywces-3' => array( | |
'folder' => '/emails/template-3', | |
'path' => YWCES_TEMPLATE_PATH | |
), | |
); | |
// register plugin to licence/update system | |
add_action( 'wp_loaded', array( $this, 'register_plugin_for_activation' ), 99 ); | |
add_action( 'admin_init', array( $this, 'register_plugin_for_updates' ) ); | |
$this->includes_premium(); | |
add_filter( 'yith_wcet_email_template_types', array( $this, 'add_yith_wcet_template' ) ); | |
if ( is_admin() ) { | |
add_action( 'admin_notices', array( $this, 'ywces_admin_notices_premium' ) ); | |
add_action( 'admin_notices', array( $this, 'check_active_options_premium' ), 10 ); | |
add_filter( 'ywces_admin_scripts_filter', array( $this, 'ywces_admin_scripts_filter' ) ); | |
//Custom Fields | |
add_action( 'woocommerce_admin_field_ywces-table', 'YWCES_Custom_Table::output' ); | |
add_action( 'woocommerce_admin_field_ywces-collapse', 'YWCES_Custom_Collapse::output' ); | |
add_action( 'woocommerce_admin_field_ywces-select', 'YWCES_Custom_Select::output' ); | |
add_action( 'woocommerce_admin_field_ywces-coupon', 'YWCES_Custom_Coupon::output' ); | |
add_action( 'woocommerce_admin_field_ywces-mailskin', 'YWCES_Mail_Skin::output' ); | |
} | |
else { | |
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) ); | |
add_action( 'woocommerce_edit_account_form', array( $this, 'add_birthday_field' ) ); | |
add_action( 'woocommerce_save_account_details', array( $this, 'save_birthday_field' ) ); | |
add_filter( 'woocommerce_checkout_fields', array( $this, 'add_birthday_field_checkout' ) ); | |
//hook & action bday | |
add_action( 'register_form', array( $this, 'add_birthday_field' ) );//add field to reg. form | |
add_action( 'user_register', array( $this, 'save_birthday_field' ) );//save field from reg. form | |
} | |
add_action( 'ywces_daily_send_mail_job', array( $this, 'ywces_daily_send_mail_job' ) ); | |
add_action( 'woocommerce_created_customer', array( $this, 'ywces_user_registration' ), 10, 2 ); | |
} | |
/** | |
* Files inclusion | |
* | |
* @since 1.0.0 | |
* @return void | |
* @author Alberto Ruggiero | |
*/ | |
private function includes_premium() { | |
include_once( 'includes/class-ywces-mandrill.php' ); | |
if ( is_admin() ) { | |
include_once( 'templates/admin/custom-table.php' ); | |
include_once( 'templates/admin/custom-collapse.php' ); | |
include_once( 'templates/admin/custom-select.php' ); | |
include_once( 'templates/admin/custom-coupon.php' ); | |
include_once( 'templates/admin/custom-mailskin.php' ); | |
} | |
} | |
/** | |
* If is active YITH WooCommerce Email Templates, add YWCES to list | |
* | |
* @since 1.0.1 | |
* | |
* @param $templates | |
* | |
* @return array | |
* @author Alberto Ruggiero | |
*/ | |
public function add_yith_wcet_template( $templates ) { | |
$templates[] = array( | |
'id' => 'yith-coupon-email-system', | |
'name' => 'YITH WooCommerce Coupon Email System', | |
); | |
return $templates; | |
} | |
/** | |
* ADMIN FUNCTIONS | |
*/ | |
/** | |
* Add premium strings for localization | |
* | |
* @since 1.0.0 | |
* | |
* @param $strings | |
* | |
* @return array | |
* @author Alberto Ruggiero | |
*/ | |
public function ywces_admin_scripts_filter( $strings ) { | |
$strings['test_mail_no_threshold'] = __( 'You need to set at least a threshold to send a test email', 'yith-woocommerce-coupon-email-system' ); | |
$strings['test_mail_no_product'] = __( 'Please select at least a product', 'yith-woocommerce-coupon-email-system' ); | |
$strings['test_mail_no_amount'] = __( 'You need to select at least the amount/percentage of a coupon to send it in a test email', 'yith-woocommerce-coupon-email-system' ); | |
$strings['test_mail_days_elapsed'] = __( 'Please specify the number of days', 'yith-woocommerce-coupon-email-system' ); | |
return $strings; | |
} | |
/** | |
* Advise if the plugin cannot be performed | |
* | |
* @since 1.0.0 | |
* @return void | |
* @author Alberto Ruggiero | |
*/ | |
public function ywces_admin_notices_premium() { | |
if ( get_option( 'ywces_mandrill_enable' ) == 'yes' && get_option( 'ywces_mandrill_apikey' ) == '' ) : ?> | |
<div class="error"> | |
<p> | |
<?php _e( 'Please enter Mandrill API Key for YITH WooCommerce Coupon Email System', 'yith-woocommerce-coupon-email-system' ); ?> | |
</p> | |
</div> | |
<?php endif; | |
} | |
/** | |
* Check if active options have a coupon assigned | |
* | |
* @since 1.0.0 | |
* @return void | |
* @author Alberto Ruggiero | |
*/ | |
public function check_active_options_premium() { | |
if ( isset( $_POST['ywces_enable_first_purchase'] ) && '1' == $_POST['ywces_enable_first_purchase'] ) { | |
if ( $_POST['ywces_coupon_first_purchase'] == '' ) : | |
?> | |
<div class="error"> | |
<p> | |
<?php _e( 'You need to select a coupon to send one for a new first purchase', 'yith-woocommerce-coupon-email-system' ); ?> | |
</p> | |
</div> | |
<?php | |
endif; | |
} | |
if ( isset( $_POST['ywces_enable_purchases'] ) && '1' == $_POST['ywces_enable_purchases'] ) { | |
if ( !isset( $_POST['ywces_thresholds_purchases'] ) ) : | |
?> | |
<div class="error"> | |
<p> | |
<?php _e( 'You need to set a threshold to send a coupon once a user reaches a specific number of purchases', 'yith-woocommerce-coupon-email-system' ); ?> | |
</p> | |
</div> | |
<?php | |
update_option( 'ywces_thresholds_purchases', '' ); | |
else: | |
$count = 0; | |
foreach ( maybe_unserialize( $_POST['ywces_thresholds_purchases'] ) as $threshold ) { | |
if ( $threshold['coupon'] == '' ) { | |
$count ++; | |
} | |
} | |
if ( $count > 0 ): | |
?> | |
<div class="error"> | |
<p> | |
<?php _e( 'You need to set a coupon for each threshold to send one when users reach a specific number of purchases.', 'yith-woocommerce-coupon-email-system' ); ?> | |
</p> | |
</div> | |
<?php | |
endif; | |
endif; | |
} | |
if ( isset( $_POST['ywces_enable_spending'] ) && '1' == $_POST['ywces_enable_spending'] ) { | |
if ( !isset( $_POST['ywces_thresholds_spending'] ) ) : | |
?> | |
<div class="error"> | |
<p> | |
<?php _e( 'You need to set a threshold to send a coupon once a user reaches a specific spent amount', 'yith-woocommerce-coupon-email-system' ); ?> | |
</p> | |
</div> | |
<?php | |
update_option( 'ywces_thresholds_spending', '' ); | |
else: | |
$count = 0; | |
foreach ( maybe_unserialize( $_POST['ywces_thresholds_spending'] ) as $threshold ) { | |
if ( $threshold['coupon'] == '' ) { | |
$count ++; | |
} | |
} | |
if ( $count > 0 ): | |
?> | |
<div class="error"> | |
<p> | |
<?php _e( 'You need to set a coupon for each threshold to send one when users reach a specific spent amount.', 'yith-woocommerce-coupon-email-system' ); ?> | |
</p> | |
</div> | |
<?php | |
endif; | |
endif; | |
} | |
if ( isset( $_POST['ywces_enable_product_purchasing'] ) && '1' == $_POST['ywces_enable_product_purchasing'] ) { | |
if ( !isset( $_POST['ywces_targets_product_purchasing'] ) || $_POST['ywces_targets_product_purchasing'] == '' ) : | |
?> | |
<div class="error"> | |
<p> | |
<?php _e( 'You need to select at least one product to send a coupon once purchased', 'yith-woocommerce-coupon-email-system' ); ?> | |
</p> | |
</div> | |
<?php | |
endif; | |
$coupon = maybe_unserialize( $_POST['ywces_coupon_product_purchasing'] ); | |
if ( $coupon['coupon_amount'] == '' ) : | |
?> | |
<div class="error"> | |
<p> | |
<?php _e( 'You need to select at least the amount/percentage of a coupon to send it for the purchase of a specific product', 'yith-woocommerce-coupon-email-system' ); ?> | |
</p> | |
</div> | |
<?php | |
endif; | |
} | |
if ( isset( $_POST['ywces_enable_birthday'] ) && '1' == $_POST['ywces_enable_birthday'] ) { | |
$coupon = maybe_unserialize( $_POST['ywces_coupon_birthday'] ); | |
if ( $coupon['coupon_amount'] == '' ) : | |
?> | |
<div class="error"> | |
<p> | |
<?php _e( 'You need to select at least the amount/percentage of a coupon to send it for the birthday of a user', 'yith-woocommerce-coupon-email-system' ); ?> | |
</p> | |
</div> | |
<?php | |
endif; | |
} | |
if ( isset( $_POST['ywces_enable_last_purchase'] ) && '1' == $_POST['ywces_enable_last_purchase'] ) { | |
$coupon = maybe_unserialize( $_POST['ywces_coupon_birthday'] ); | |
if ( $coupon['coupon_amount'] == '' ) : | |
?> | |
<div class="error"> | |
<p> | |
<?php _e( 'You need to select at least the amount/percentage of a coupon to send it after a specific number of days following the last order', 'yith-woocommerce-coupon-email-system' ); ?> | |
</p> | |
</div> | |
<?php | |
endif; | |
} | |
} | |
/** | |
* Trigger coupons on user purchase | |
* | |
* @since 1.0.0 | |
* | |
* @param $order | |
* @param $customer_id | |
* @param $order_count | |
* | |
* @return void | |
* @author Alberto Ruggiero | |
*/ | |
public function ywces_user_purchase_premium( $order, $customer_id, $order_count ) { | |
if ( count( $this->_available_coupons ) > 0 ) { | |
//check if uses has reached an order threshold | |
if ( get_option( 'ywces_enable_purchases' ) == 'yes' ) { | |
$purchase_threshold = $this->check_threshold( $order_count, 'purchases', $customer_id ); | |
if ( !empty( $purchase_threshold ) ) { | |
$coupon_code = $purchase_threshold['coupon_id']; | |
if ( $this->check_if_coupon_exists( $coupon_code ) ) { | |
$args = array( | |
'order_date' => date( 'Y-m-d', strtotime( $order->order_date ) ), | |
'threshold' => $purchase_threshold['threshold'], | |
); | |
$this->bind_coupon( $coupon_code, $order->billing_email ); | |
YWCES_Emails()->prepare_coupon_mail( $customer_id, 'purchases', $coupon_code, $args ); | |
} | |
return; | |
} | |
} | |
$money_spent = get_user_meta( $customer_id, '_money_spent', true ); | |
//check if uses has reached a spending threshold | |
if ( get_option( 'ywces_enable_spending' ) == 'yes' ) { | |
$spending_threshold = $this->check_threshold( $money_spent, 'spending', $customer_id ); | |
if ( !empty( $spending_threshold ) ) { | |
$coupon_code = $spending_threshold['coupon_id']; | |
if ( $this->check_if_coupon_exists( $coupon_code ) ) { | |
$args = array( | |
'order_date' => date( 'Y-m-d', strtotime( $order->order_date ) ), | |
'threshold' => $spending_threshold['threshold'], | |
'expense' => $money_spent, | |
); | |
$this->bind_coupon( $coupon_code, $order->billing_email ); | |
YWCES_Emails()->prepare_coupon_mail( $customer_id, 'spending', $coupon_code, $args ); | |
} | |
return; | |
} | |
} | |
} | |
if ( get_option( 'ywces_enable_product_purchasing' ) == 'yes' && get_option( 'ywces_targets_product_purchasing' ) != '' ) { | |
$target_products = get_option( 'ywces_targets_product_purchasing' ); | |
$target_products = explode( ',', $target_products ); | |
$order_items = $order->get_items(); | |
$found_product = ''; | |
foreach ( $order_items as $item ) { | |
if ( in_array( implode( '', $item['item_meta']['_product_id'] ), $target_products ) && $found_product == '' ) { | |
$found_product = implode( '', $item['item_meta']['_product_id'] ); | |
} | |
} | |
if ( $found_product != '' ) { | |
$coupon_code = YITH_WCES()->create_coupon( $customer_id, 'product_purchasing' ); | |
$args = array( | |
'order_date' => date( 'Y-m-d', strtotime( $order->order_date ) ), | |
'product' => $found_product, | |
); | |
YWCES_Emails()->prepare_coupon_mail( $customer_id, 'product_purchasing', $coupon_code, $args ); | |
} | |
} | |
} | |
/** | |
* Check if a threshold is reached and returns the coupon code and the threshold | |
* | |
* @since 1.0.0 | |
* | |
* @param $amount | |
* @param $type | |
* @param $customer_id | |
* | |
* @return array | |
* @author Alberto Ruggiero | |
*/ | |
public function check_threshold( $amount, $type, $customer_id = false ) { | |
$thresholds = get_option( 'ywces_thresholds_' . $type ); | |
$closest_threshold = 0; | |
$result = array(); | |
if ( $thresholds != '' ) { | |
foreach ( $thresholds as $key => $threshold ) { | |
if ( $amount >= $threshold['amount'] && $closest_threshold < $threshold['amount'] ) { | |
$customers = isset( $threshold['customers'] ) ? explode( ',', $threshold['customers'] ) : array(); | |
if ( !empty( $customers ) && in_array( $customer_id, $customers ) ) { | |
continue; | |
} | |
$closest_threshold = $threshold['amount']; | |
$result = array( 'coupon_id' => $threshold['coupon'], 'threshold' => $threshold['amount'] ); | |
if ( $customer_id ) { | |
$customers[] = $customer_id; | |
$thresholds[$key]['customers'] = implode( ',', $customers ); | |
update_option( 'ywces_thresholds_' . $type, $thresholds ); | |
} | |
} | |
} | |
} | |
return $result; | |
} | |
/** | |
* Creates a coupon with specific settings | |
* | |
* @since 1.0.0 | |
* | |
* @param $user_id | |
* @param $type | |
* @param $coupon_args | |
* | |
* @return string | |
* @author Alberto Ruggiero | |
*/ | |
public function create_coupon( $user_id, $type, $coupon_args = array() ) { | |
$user_nickname = get_user_meta( $user_id, 'nickname', true ); | |
$user_email = get_user_meta( $user_id, 'billing_email', true ); | |
$coupon_code = $user_nickname . '-' . current_time( 'YmdHis' ); | |
$coupon_data = array( | |
'post_title' => $coupon_code, | |
'post_content' => '', | |
'post_status' => 'publish', | |
'post_author' => 1, | |
'post_type' => 'shop_coupon' | |
); | |
$coupon_id = wp_insert_post( $coupon_data ); | |
if ( empty( $coupon_args ) ) { | |
$coupon_option = get_option( 'ywces_coupon_' . $type ); | |
} | |
else { | |
$coupon_option = $coupon_args; | |
} | |
$expiry_date = ( $coupon_option['expiry_days'] != '' ) ? date( 'Y-m-d', strtotime( '+' . $coupon_option['expiry_days'] . ' days' ) ) : ''; | |
update_post_meta( $coupon_id, 'discount_type', $coupon_option['discount_type'] ); | |
update_post_meta( $coupon_id, 'coupon_amount', $coupon_option['coupon_amount'] ); | |
update_post_meta( $coupon_id, 'individual_use', ( isset( $coupon_option['individual_use'] ) && $coupon_option['individual_use'] != '' ? 'yes' : 'no' ) ); | |
update_post_meta( $coupon_id, 'usage_limit', '1' ); | |
update_post_meta( $coupon_id, 'expiry_date', $expiry_date ); | |
update_post_meta( $coupon_id, 'customer_email', $user_email ); | |
update_post_meta( $coupon_id, 'minimum_amount', $coupon_option['minimum_amount'] ); | |
update_post_meta( $coupon_id, 'maximum_amount', $coupon_option['maximum_amount'] ); | |
update_post_meta( $coupon_id, 'free_shipping', ( isset( $coupon_option['free_shipping'] ) && $coupon_option['free_shipping'] != '' ? 'yes' : 'no' ) ); | |
update_post_meta( $coupon_id, 'exclude_sale_items', ( isset( $coupon_option['exclude_sale_items'] ) && $coupon_option['exclude_sale_items'] != '' ? 'yes' : 'no' ) ); | |
return $coupon_code; | |
} | |
/** | |
* Daily cron job | |
* | |
* @since 1.0.0 | |
* @return void | |
* @author Alberto Ruggiero | |
*/ | |
public function ywces_daily_send_mail_job() { | |
if ( get_option( 'ywces_enable_last_purchase' ) == 'yes' ) { | |
$users = $this->get_customers_id_by_last_purchase(); | |
if ( !empty( $users ) ) { | |
foreach ( $users as $customer_id ) { | |
$coupon_code = YITH_WCES()->create_coupon( $customer_id, 'last_purchase' ); | |
$args = array( | |
'days_ago' => get_option( 'ywces_days_last_purchase' ) | |
); | |
YWCES_Emails()->prepare_coupon_mail( $customer_id, 'last_purchase', $coupon_code, $args ); | |
//Set the user to not receive another coupon until he does a new purchase | |
update_user_meta( $customer_id, '_last_purchase_coupon_sent', 'yes' ); | |
} | |
} | |
} | |
if ( get_option( 'ywces_enable_birthday' ) == 'yes' ) { | |
$users = $this->get_customers_id_by_birthdate(); | |
if ( !empty( $users ) ) { | |
foreach ( $users as $customer_id ) { | |
$coupon_code = YITH_WCES()->create_coupon( $customer_id, 'birthday' ); | |
YWCES_Emails()->prepare_coupon_mail( $customer_id, 'birthday', $coupon_code ); | |
} | |
} | |
} | |
} | |
/** | |
* Get a list of id of customers by birthdate who need to receive the coupon | |
* | |
* @since 1.0.0 | |
* @return array | |
* @author Alberto Ruggiero | |
*/ | |
public function get_customers_id_by_birthdate() { | |
global $wpdb; | |
$customers_ids = array(); | |
$user_query = $wpdb->get_results( "SELECT user_id FROM {$wpdb->prefix}usermeta WHERE meta_key = 'ywces_birthday' AND MONTH(meta_value) = MONTH(NOW()) AND DAY(meta_value) = DAY(NOW())" ); | |
if ( !empty( $user_query ) ) { | |
foreach ( $user_query as $user ) { | |
$customers_ids[] = $user->user_id; | |
} | |
} | |
return $customers_ids; | |
} | |
/** | |
* Set custom where condition | |
* | |
* @since 1.0.0 | |
* | |
* @param $where | |
* | |
* @return string | |
* @author Alberto Ruggiero | |
*/ | |
public function ywces_filter_where_orders( $where = '' ) { | |
$days = get_option( 'ywces_days_last_purchase' ); | |
$where .= " AND post_date <= '" . date( 'Y-m-d', strtotime( '-' . $days . ' days' ) ) . "'"; | |
return $where; | |
} | |
/** | |
* Get a list of id of customers by last purchase who need to receive the coupon | |
* | |
* @since 1.0.0 | |
* @return array | |
* @author Alberto Ruggiero | |
*/ | |
public function get_customers_id_by_last_purchase() { | |
$found_ids = array(); | |
$args = array( | |
'post_type' => 'shop_order', | |
'post_status' => 'wc-completed', | |
'posts_per_page' => - 1 | |
); | |
add_filter( 'posts_where', array( $this, 'ywces_filter_where_orders' ) ); | |
$query = new WP_Query( $args ); | |
remove_filter( 'posts_where', array( $this, 'ywces_filter_where_orders' ) ); | |
if ( $query->have_posts() ) { | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
$found_ids[] = $query->post->_customer_user; | |
} | |
} | |
wp_reset_query(); | |
wp_reset_postdata(); | |
$customers_ids = array(); | |
$user_args = array( | |
'include' => array_unique( $found_ids ), | |
'meta_query' => array( | |
'relation' => 'OR', | |
array( | |
'key' => '_last_purchase_coupon_sent', | |
'value' => 'no', | |
'compare' => '=' | |
), | |
array( | |
'key' => '_last_purchase_coupon_sent', | |
'compare' => 'NOT EXISTS' | |
) | |
) | |
); | |
$user_query = new WP_User_Query( $user_args ); | |
if ( !empty( $user_query->results ) ) { | |
foreach ( $user_query->results as $user ) { | |
$customers_ids[] = $user->ID; | |
} | |
} | |
return $customers_ids; | |
} | |
/** | |
* FRONTEND FUNCTIONS | |
*/ | |
/** | |
* Initializes CSS and javascript | |
* | |
* @since 1.0.0 | |
* @return void | |
* @author Alberto Ruggiero | |
*/ | |
public function frontend_scripts() { | |
wp_enqueue_style( 'ywces-frontend', YWCES_ASSETS_URL . '/css/jquery-ui.css' ); | |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; | |
wp_enqueue_script( 'ywces-frontend', YWCES_ASSETS_URL . '/js/ywces-frontend' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker' ) ); | |
} | |
/** | |
* Add customer birth date field to checkout process | |
* | |
* @since 1.0.0 | |
* | |
* @param $fields | |
* | |
* @return array | |
* @author Alberto Ruggiero | |
*/ | |
public function add_birthday_field_checkout( $fields ) { | |
$fields['account']['ywces_birthday'] = array( | |
'label' => __( 'Birth date', 'yith-woocommerce-coupon-email-system' ), | |
'custom_attributes' => array( | |
'pattern' => '[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])' | |
), | |
'placeholder' => 'YYYY-MM-DD', | |
'input_class' => array( 'ywces-birthday' ) | |
); | |
return $fields; | |
} | |
/** | |
* Add customer birth date field to edit account page | |
* | |
* @since 1.0.0 | |
* @return void | |
* @author Alberto Ruggiero | |
*/ | |
public function add_birthday_field() { | |
$user = get_user_by( 'id', get_current_user_id() ); | |
$birth_date = ( empty ( $user ) ) ? '' : esc_attr( $user->ywces_birthday ) | |
?> | |
<p class="form-row form-row-wide"> | |
<label for="ywces_birthday"> | |
<?php _e( 'Birth date', 'yith-woocommerce-coupon-email-system' ); ?> | |
</label> | |
<input | |
type="text" | |
class="input-text ywces-birthday" | |
name="ywces_birthday" id="ywces_birthday" | |
value="<?php echo $birth_date; ?>" | |
placeholder="YYYY-MM-DD" | |
maxlength="10" | |
pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" | |
/> | |
</p> | |
<?php | |
} | |
/** | |
* Save customer birth date from edit account page | |
* | |
* @since 1.0.0 | |
* | |
* @param $customer_id | |
* | |
* @return void | |
* @author Alberto Ruggiero | |
*/ | |
public function save_birthday_field( $customer_id ) { | |
if ( isset( $_POST['ywces_birthday'] ) ) { | |
update_user_meta( $customer_id, 'ywces_birthday', sanitize_text_field( $_POST['ywces_birthday'] ) ); | |
} | |
} | |
/** | |
* Trigger coupon on user registration | |
* | |
* @since 1.0.0 | |
* | |
* @param $customer_id | |
* @param $new_customer_data | |
* | |
* @return void | |
* @author Alberto Ruggiero | |
*/ | |
public function ywces_user_registration( $customer_id , $new_customer_data) { | |
$coupon_code = get_option( 'ywces_coupon_register' ); | |
if ( get_option( 'ywces_enable_register' ) == 'yes' && count( $this->_available_coupons ) > 0 && YITH_WCES()->check_if_coupon_exists( $coupon_code ) ) { | |
$this->bind_coupon( $coupon_code, $new_customer_data['user_email'] ); | |
YWCES_Emails()->prepare_coupon_mail( $customer_id, 'register', $coupon_code ); | |
} | |
} | |
/** | |
* YITH FRAMEWORK | |
*/ | |
/** | |
* Register plugins for activation tab | |
* | |
* @return void | |
* @since 2.0.0 | |
* @author Andrea Grillo <andrea.grillo@yithemes.com> | |
*/ | |
public function register_plugin_for_activation() { | |
if ( !class_exists( 'YIT_Plugin_Licence' ) ) { | |
require_once 'plugin-fw/licence/lib/yit-licence.php'; | |
require_once 'plugin-fw/licence/lib/yit-plugin-licence.php'; | |
} | |
YIT_Plugin_Licence()->register( YWCES_INIT, YWCES_SECRET_KEY, YWCES_SLUG ); | |
} | |
/** | |
* Register plugins for update tab | |
* | |
* @return void | |
* @since 2.0.0 | |
* @author Andrea Grillo <andrea.grillo@yithemes.com> | |
*/ | |
public function register_plugin_for_updates() { | |
if ( !class_exists( 'YIT_Upgrade' ) ) { | |
require_once( 'plugin-fw/lib/yit-upgrade.php' ); | |
} | |
YIT_Upgrade()->register( YWCES_SLUG, YWCES_INIT ); | |
} | |
} | |
} | |
if ( !function_exists( 'save_checkout_fields' ) ) { | |
/** | |
* Save customer birth date from checkout process | |
* | |
* @since 1.0.0 | |
* | |
* @param $customer_id | |
* @param $posted | |
* | |
* @return void | |
* @author Alberto Ruggiero | |
*/ | |
function save_birthday_field_checkout( $customer_id, $posted ) { | |
if ( isset( $_POST['ywces_birthday'] ) ) { | |
update_user_meta( $customer_id, 'ywces_birthday', sanitize_text_field( $_POST['ywces_birthday'] ) ); | |
} | |
} | |
add_action( 'woocommerce_checkout_update_user_meta', 'save_birthday_field_checkout', 10, 2 ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment