Advertisement
Guest User

Untitled

a guest
Nov 14th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.53 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: WooCommerce Create Customer on Order
  4. * Description: Save time and simplify your life by having the ability to create a new Customer directly on the WooCommerce Order screen
  5. * Author: cxThemes
  6. * Author URI: http://codecanyon.net/user/cxThemes
  7. * Plugin URI: http://codecanyon.net/item/create-customer-on-order-for-woocommerce/6395319
  8. * Version: 1.16
  9. * Text Domain: create-customer-order
  10. * Domain Path: /languages/
  11. *
  12. *
  13. * License: GNU General Public License v3.0
  14. * License URI: http://www.gnu.org/licenses/gpl-3.0.html
  15. *
  16. * @package WC-ADD-USER-ORDER
  17. * @author cxThemes
  18. * @category WooCommerce
  19. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
  20. */
  21.  
  22. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  23.  
  24. /**
  25. * Define Constants
  26. */
  27. define( 'WC_CREATE_CUSTOMER_ON_ORDER_VERSION', '1.16' );
  28. define( 'WC_CREATE_CUSTOMER_ON_ORDER_REQUIRED_WOOCOMMERCE_VERSION', 2.2 );
  29.  
  30. /**
  31. * Update Check
  32. */
  33. require 'plugin-updates/plugin-update-checker.php';
  34. $wc_create_customer_on_order_update = new PluginUpdateChecker(
  35. 'http://cxthemes.com/plugins/woocommerce-create-customer-order/create-customer-order.json',
  36. __FILE__,
  37. 'create-customer-order'
  38. );
  39.  
  40. /**
  41. * Check if WooCommerce is active, and is required WooCommerce version.
  42. */
  43. if ( ! WC_Create_Customer_On_Order::is_woocommerce_active() || version_compare( get_option( 'woocommerce_version' ), WC_CREATE_CUSTOMER_ON_ORDER_REQUIRED_WOOCOMMERCE_VERSION, '<' ) ){
  44. add_action( 'admin_notices', array( 'WC_Create_Customer_On_Order', 'woocommerce_inactive_notice' ) );
  45. return;
  46. }
  47.  
  48. /**
  49. * Main Class.
  50. */
  51. class WC_Create_Customer_On_Order {
  52.  
  53. private $id = 'woocommerce_create_customer_order';
  54.  
  55. private static $instance;
  56.  
  57. /**
  58. * Get Instance creates a singleton class that's cached to stop duplicate instances
  59. */
  60. public static function get_instance() {
  61. if ( ! self::$instance ) {
  62. self::$instance = new self();
  63. self::$instance->init();
  64. }
  65. return self::$instance;
  66. }
  67.  
  68. /**
  69. * Construct empty on purpose
  70. */
  71. private function __construct() {}
  72.  
  73. /**
  74. * Init behaves like, and replaces, construct
  75. */
  76. public function init(){
  77.  
  78. // Check if WooCommerce is active, and is required WooCommerce version.
  79. if ( ! class_exists( 'WooCommerce' ) || version_compare( get_option( 'woocommerce_db_version' ), WC_CREATE_CUSTOMER_ON_ORDER_REQUIRED_WOOCOMMERCE_VERSION, '<' ) ) {
  80. add_action( 'admin_notices', array( $this, 'woocommerce_inactive_notice' ) );
  81. return;
  82. }
  83.  
  84. // Localization
  85. add_action( 'init', array( $this, 'load_translation' ) );
  86.  
  87. // Enqueue Scripts
  88. add_action( 'admin_print_styles', array( $this, 'admin_scripts' ) );
  89.  
  90. // WC Order page - Create Customer Form & Ajax
  91. add_action( 'woocommerce_admin_order_data_after_order_details', array( $this, 'create_customer_on_order_page' ) );
  92. add_action( 'wp_ajax_woocommerce_order_create_user', array( $this, 'woocommerce_create_customer_on_order' ) );
  93.  
  94. // WC Order page - Save address's to customer
  95. add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'update_customer_on_order_page' ) );
  96. add_action( 'woocommerce_process_shop_order_meta', array( $this, 'save_address_from_order_to_customer') );
  97.  
  98. // Change Lost Password page message for users created by Add Customer on Order.
  99. add_action( 'loop_start', array( $this, 'condition_filter_title' ) );
  100. add_filter( 'woocommerce_reset_password_message', array( $this, 'change_lost_password_message' ) );
  101.  
  102. // After customer submits reset-password is redirect to my-accounts page and account set to standard behaviour.
  103. add_action( 'woocommerce_customer_reset_password', array( $this, 'update_customer_password_state' ) );
  104. }
  105.  
  106. function condition_filter_title( $array ){
  107. global $wp_query;
  108. if ( $array === $wp_query ) {
  109. add_filter( 'the_title', array( $this, 'woocommerce_new_customer_change_title' ) );
  110. }
  111. else {
  112. remove_filter( 'the_title', array( $this, 'woocommerce_new_customer_change_title' ) );
  113. }
  114. }
  115.  
  116. /**
  117. * Localization
  118. */
  119. public function load_translation() {
  120.  
  121. // Domain ID - used in eg __( 'Text', 'create-customer-order' )
  122. $domain = 'create-customer-order';
  123.  
  124. // get the languages locale eg 'en_US'
  125. $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
  126.  
  127. // Look for languages here: wp-content/languages/pluginname/pluginname-en_US.mo
  128. load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
  129.  
  130. // Look for languages here: wp-content/languages/pluginname-en_US.mo
  131. load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '-' . $locale . '.mo' );
  132.  
  133. // Look for languages here: wp-content/plugins/pluginname/languages/pluginname-en_US.mo
  134. load_plugin_textdomain($domain, FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
  135. }
  136.  
  137. /**
  138. * Add create customer form to Order Page
  139. */
  140. public function create_customer_on_order_page() {
  141.  
  142. global $woocommerce, $wp_roles;
  143. ?>
  144. <div class='create_user form-field form-field-wide'>
  145. <p>
  146. <button class='button create_user_form'>
  147. <?php _e( 'Create Customer', 'create-customer-order' ); ?>
  148. <span class='create_user_icon'>&nbsp;</span>
  149. </button>
  150. </p>
  151. <div class='toggle-create-user'>
  152. <p>
  153. <label for='create_user_first_name'>
  154. <?php _e( 'First Name', 'create-customer-order' ); ?>
  155. </label>
  156. <input type='text' name='create_user_first_name' id='create_user_first_name' value='' />
  157. </p>
  158. <p>
  159. <label for='create_user_last_name'>
  160. <?php _e( 'Last Name', 'create-customer-order' ); ?>
  161. </label>
  162. <input type='text' name='create_user_last_name' id='create_user_last_name' value='' />
  163. </p>
  164. <p>
  165. <label for='create_user_email_address'>
  166. <?php _e( 'Email Address', 'create-customer-order' ); ?>
  167. <span class='required-field'>*</span>
  168. </label>
  169. <input type='text' name='create_user_email_address' id='create_user_email_address' value='' />
  170. </p>
  171. <p>
  172. <label for='create_user_username'>
  173. <?php _e( 'Set Username (optional)', 'create-customer-order' ); ?>
  174. </label>
  175. <input type='text' name='create_user_username' id='create_user_username' value='' />
  176. </p>
  177. <p>
  178. <label for='create_user_role'><?php _e( 'Role', 'create-customer-order' ); ?></label>
  179. <select name='create_user_role' id='create_user_role'>
  180. <?php
  181. if ( isset( $wp_roles ) ) {
  182. foreach ( $wp_roles->get_names() as $role => $label ) {
  183.  
  184. if ( !$this->can_current_user_create_role( $role ) )
  185. $role = '';
  186. ?>
  187. <option <?php if ( '' == $role ) echo 'class="user-capability-restricted"' ?> value="<?php echo $role; ?>" <?php if ( "customer" == $role ) { echo 'selected="selected"'; } ?> >
  188. <?php echo $label; ?> <?php if ( '' == $role ) _e( '(Your user capability prevents this)', 'create-customer-order' ) ?>
  189. </option>
  190. <?php
  191. }
  192. }
  193. else{
  194. ?>
  195. <option value="customer" selected="selected">
  196. <?php _e( 'Customer', 'create-customer-order' ); ?>
  197. </option>
  198. <?php
  199. }
  200. ?>
  201. </select>
  202. </p>
  203. <?php
  204. /*
  205. * To auto pretick the disable registration email checkbox, add the following line to your theme functions.php
  206. * add_filter( 'shop_as_customer_disable_email', '__return_true' );
  207. */
  208. $disable_email = apply_filters( 'shop_as_customer_disable_email', false ); ?>
  209. <p class="create-customer-checkbox-row">
  210. <input type='checkbox' <?php echo ( $disable_email ) ? 'checked="checked"' : ''; ?> name='create_user_disable_email' id='create_user_disable_email' value='yes' />
  211. <label for='create_user_disable_email'>
  212. <?php _e( 'Disable customer registration email', 'create-customer-order' ); ?>
  213. </label>
  214. </p>
  215. <p>
  216. <button class='button submit_user_form_cancel'>
  217. <?php _e( 'Cancel', 'create-customer-order' ); ?>
  218. </button>
  219. <button class='button submit_user_form'>
  220. <?php _e( 'Create Customer', 'create-customer-order' ); ?>
  221. </button>
  222. </p>
  223. </div>
  224. </div>
  225.  
  226. <?php
  227. // Insert Add Customer
  228. wc_enqueue_js( "jQuery('.create_user.form-field').insertAfter( jQuery('#customer_user').parents('.form-field:eq(0)') );" );
  229.  
  230. }
  231.  
  232. /**
  233. * Add Save to customer checkboxes above Billing and Shipping Details on Order page
  234. */
  235. public function update_customer_on_order_page() {
  236. ?>
  237. <div class='sac-order-save-actions save-billing-address'>
  238. <label class='save-address-to-user'>
  239. <?php _e( "Save to Customer", 'create-customer-order' ); ?>
  240. <span class='save-billing-address-check'>
  241. <input type='checkbox' name='save-billing-address-input' id='save-billing-address-input' value='true' />
  242. </span>
  243. </label>
  244. </div>
  245. <div class='sac-order-save-actions save-shipping-address'>
  246. <label class='save-address-to-user'>
  247. <?php _e( "Save to Customer", 'create-customer-order' ); ?>
  248. <span class='save-shipping-address-check'>
  249. <input type='checkbox' name='save-shipping-address-input' id='save-shipping-address-input' value='true' />
  250. </span>
  251. </label>
  252. </div>
  253. <?php
  254. }
  255.  
  256. /**
  257. * Include admin scripts
  258. */
  259. public function admin_scripts() {
  260.  
  261. global $woocommerce, $wp_scripts;
  262.  
  263. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  264.  
  265. wp_register_style( 'woocommerce-create-customer-order', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/css/styles.css', basename( __FILE__ ) ), '', WC_CREATE_CUSTOMER_ON_ORDER_VERSION, 'screen' );
  266. wp_enqueue_style( 'woocommerce-create-customer-order' );
  267.  
  268. wp_register_script( 'woocommerce-create-customer-order', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/js/create-user-on-order.js', basename( __FILE__ ) ), array('jquery'), WC_CREATE_CUSTOMER_ON_ORDER_VERSION );
  269. wp_enqueue_script( 'woocommerce-create-customer-order' );
  270.  
  271. $woocommerce_create_customer_order_params = array(
  272. 'plugin_url' => $woocommerce->plugin_url(),
  273. 'ajax_url' => admin_url('admin-ajax.php'),
  274. 'create_customer_nonce' => wp_create_nonce("create-customer"),
  275. 'home_url' => get_home_url(),
  276. 'msg_email_exists' => __( 'Email Address already exists', 'create-customer-order'),
  277. 'msg_email_empty' => __( 'Please enter an email address', 'create-customer-order'),
  278. 'msg_email_invalid' => __( 'Invalid Email Address', 'create-customer-order'),
  279. 'msg_email_exists_username' => __( 'This email address already exists as another users Username', 'create-customer-order'),
  280. 'msg_username_invalid' => __( 'Invalid Username', 'create-customer-order'),
  281. 'msg_success' => __( 'User created and linked to this order', 'create-customer-order'),
  282. 'msg_email_valid' => __( 'Please enter a valid email address', 'create-customer-order'),
  283. 'msg_successful' => __( 'Success', 'create-customer-order'),
  284. 'msg_error' => __( 'Error', 'create-customer-order'),
  285. 'msg_role' => __( 'Your user role does not have the capability to create a user with this role', 'create-customer-order'),
  286. );
  287.  
  288. wp_localize_script( 'woocommerce-create-customer-order', 'woocommerce_create_customer_order_params', $woocommerce_create_customer_order_params );
  289.  
  290. }
  291.  
  292. /**
  293. * Create customer via ajax on Order page
  294. *
  295. * @access public
  296. * @return void
  297. */
  298. public function woocommerce_create_customer_on_order() {
  299. global $woocommerce, $wpdb;
  300.  
  301. check_ajax_referer( 'create-customer', 'security' );
  302.  
  303. $email_address = $_POST['email_address'];
  304. $first_name = sanitize_text_field( $_POST['first_name'] );
  305. $last_name = sanitize_text_field( $_POST['last_name'] );
  306. $username = sanitize_text_field( $_POST['username'] );
  307. $user_role = sanitize_text_field( $_POST['user_role'] );
  308. $disable_email = ( isset( $_POST['disable_email'] ) && 'true' === $_POST['disable_email'] ) ? true : false;
  309.  
  310. if ( '' == $first_name && '' == $last_name ) {
  311. $display_name = substr( $email_address, 0, strpos( $email_address, '@' ) );
  312. }
  313. else {
  314. $display_name = trim( $first_name . " " . $last_name );
  315. }
  316.  
  317. $error = false;
  318.  
  319. // Email validation
  320. if ( empty( $email_address ) ) {
  321. echo json_encode( array( "error_message" => "email_empty" ) );
  322. die();
  323. }
  324. if ( ! is_email( $email_address ) ) {
  325. echo json_encode( array( "error_message" => "email_invalid" ) );
  326. die();
  327. }
  328. if ( email_exists( $email_address ) ) {
  329. echo json_encode( array( "error_message" => "email_exists" ) );
  330. die();
  331. }
  332.  
  333. // Username validation
  334.  
  335. if ( empty( $username ) ) {
  336.  
  337. // If no username then use the email address.
  338. $username = $email_address;
  339.  
  340. if ( ! validate_username( $username ) ) {
  341.  
  342. // The email is not valid username e.g. test!@test.com
  343. // so sanitise it and grab the first part e.g. test.
  344. $username = sanitize_user( $username, TRUE );
  345. $username = substr( $username, 0, strpos( $username, '@' ) );
  346. }
  347.  
  348. if ( username_exists( $username ) ) {
  349.  
  350. // The previous username exists so try combine Firstname Lastname.
  351. if ( '' != $first_name || '' != $last_name ) {
  352. $username = trim( $first_name . ' ' . $last_name );
  353. }
  354. }
  355.  
  356. if ( '' == $username || username_exists( $username ) ){
  357.  
  358. // The previous username is empty or exists.
  359. // so grab all we have - the beginning and middle part of the email
  360. // e.g. testtest
  361. $username = sanitize_user( $email_address, TRUE );
  362. $username = substr( $username, 0, strrpos( $username, '.' ) );
  363. $username = str_replace( '@', '', $username );
  364. }
  365.  
  366. // We have no more options so proceed with what we have.
  367. }
  368.  
  369. // echo json_encode( array( "error_message" => "email_empty", "error_message_testyyy" => $username ) );
  370. // die();
  371.  
  372. if ( ! validate_username( $username ) ) {
  373. echo json_encode( array( "error_message" => "username_invalid" ) );
  374. die();
  375. }
  376. if ( username_exists( $username ) ) {
  377. echo json_encode( array( "error_message" => "username_exists" ) );
  378. die();
  379. }
  380.  
  381. // Role validation
  382. if ( ! $this->can_current_user_create_role( $user_role ) ) {
  383. echo json_encode( array( "error_message" => "role_unable" ) );
  384. die();
  385. }
  386.  
  387. $password = wp_generate_password();
  388.  
  389. // Create the new user.
  390. $user_id = wp_create_user( $username, $password, $email_address );
  391.  
  392. // Update the new user.
  393. wp_update_user( array (
  394. 'ID' => $user_id,
  395. 'first_name' => $first_name,
  396. 'last_name' => $last_name,
  397. 'role' => $user_role,
  398. 'display_name'=> $display_name,
  399. 'nickname' => $display_name,
  400. ) ) ;
  401.  
  402. // Set the password.
  403. update_user_meta( $user_id, "create_customer_on_order_password", true );
  404.  
  405. // Set the other info - billing
  406. update_user_meta( $user_id, "billing_first_name", $first_name );
  407. update_user_meta( $user_id, "billing_last_name", $last_name );
  408. update_user_meta( $user_id, "billing_email", $email_address );
  409.  
  410. // Set the other info - shipping
  411. update_user_meta( $user_id, "shipping_first_name", $first_name );
  412. update_user_meta( $user_id, "shipping_last_name", $last_name );
  413.  
  414.  
  415. $allow = apply_filters( 'allow_password_reset', true, $user_id );
  416. $key = $wpdb->get_var( $wpdb->prepare( "SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $username ) );
  417.  
  418. if ( empty( $key ) ) {
  419.  
  420. // Generate something random for a key...
  421. $key = wp_generate_password( 20, false );
  422.  
  423. //do_action( 'retrieve_password_key', $username, $key );
  424.  
  425. // Now insert the key, hashed, into the DB.
  426. if ( empty( $wp_hasher ) ) {
  427. require_once ABSPATH . 'wp-includes/class-phpass.php';
  428. $wp_hasher = new PasswordHash( 8, true );
  429. }
  430.  
  431. $hashed = $wp_hasher->HashPassword( $key );
  432.  
  433. // Now insert the new md5 key into the db
  434. $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $username ) );
  435.  
  436. $lost_password_link = esc_url_raw( add_query_arg(
  437. array( 'key' => $key, 'login' => rawurlencode( $username ) ),
  438. wc_get_endpoint_url( 'lost-password', '', get_permalink( wc_get_page_id( 'myaccount' ) ) )
  439. ) );
  440.  
  441. if ( ! $disable_email ) {
  442. $this->send_register_email( $email_address, $lost_password_link, $username );
  443. }
  444.  
  445. }
  446.  
  447. echo json_encode( array( "user_id" => $user_id, "username" => $username ) );
  448. // Quit out
  449. die();
  450. }
  451.  
  452. public function can_current_user_create_role( $role = '' ) {
  453.  
  454. if ( '' == $role ) {
  455. return FALSE;
  456. }
  457. elseif ( 'administrator' == $role && !current_user_can( 'manage_options' ) ) {
  458. return FALSE;
  459. }
  460. elseif ( 'shop_manager' == $role && !current_user_can( 'edit_posts' ) ) {
  461. return FALSE;
  462. }
  463. elseif ( !current_user_can( 'edit_posts' ) ) {
  464. return FALSE;
  465. }
  466.  
  467. // If we get here user shold not be able to do this.
  468. return TRUE;
  469. }
  470.  
  471. /**
  472. * Change Lost Password page message for users created by Add Customer on Order.
  473. */
  474. public function change_lost_password_message($msg) {
  475.  
  476. global $woocommerce;
  477.  
  478. $username = esc_attr( $_GET['login'] );
  479. $user = get_user_by( 'login', $username );
  480.  
  481. $password_not_changed = get_user_meta( $user->ID, 'create_customer_on_order_password', true );
  482.  
  483. if ( $password_not_changed ) {
  484. $msg = __( 'As this is your first time logging in, please set your password.', 'create-customer-order');
  485. }
  486.  
  487. return $msg;
  488. }
  489.  
  490. /**
  491. * Change Lost Password page message for users created by Add Customer on Order.
  492. */
  493. public function woocommerce_new_customer_change_title( $page_title ) {
  494. global $woocommerce;
  495.  
  496. $is_lost_pass_page = false;
  497.  
  498. // Check if is lost pass page
  499. if ( function_exists( 'wc_get_endpoint_url' ) ) { // Above WC 2.1
  500. if ( is_wc_endpoint_url( 'lost-password' ) ) $is_lost_pass_page = true;
  501. } else {
  502. if ( is_page( woocommerce_get_page_id( 'lost_password' ) ) ) $is_lost_pass_page = true;
  503. }
  504.  
  505. // Only do this is lost pass page, and that we have a login to check against
  506. if( $is_lost_pass_page && isset( $_GET['login'] ) ){
  507.  
  508. $username = esc_attr( $_GET['login'] );
  509. $user = get_user_by( "login", $username );
  510.  
  511. $password_not_changed = get_user_meta( $user->ID, "create_customer_on_order_password", true );
  512.  
  513. if ( $password_not_changed ) {
  514. $page_title = __( 'Set your Password', 'create-customer-order' );
  515. }
  516. }
  517.  
  518. return $page_title;
  519. }
  520.  
  521. /**
  522. * After customer submits reset-password is redirect to my-accounts page and account set to standard behaviour.
  523. */
  524. public function update_customer_password_state( $user ) {
  525.  
  526. global $woocommerce;
  527.  
  528. $username = esc_attr( $_POST['reset_login'] );
  529. $user_from_email = get_user_by( "login", $username );
  530.  
  531. $password_not_changed = get_user_meta( $user_from_email->ID, "create_customer_on_order_password", true );
  532.  
  533. if ( ($user->ID == $user_from_email->ID) && ( $password_not_changed ) ) {
  534.  
  535. delete_user_meta( $user->ID, "create_customer_on_order_password" );
  536. wc_add_notice( __( 'You have successfully activated your account. Please login with your email address and new password', 'create-customer-order' ) );
  537.  
  538. ?>
  539. <script type='text/javascript'>
  540. window.location = '<?php echo get_permalink( woocommerce_get_page_id ( "myaccount" ) ); ?>';
  541. </script>
  542. <?php
  543.  
  544. die;
  545. }
  546. }
  547.  
  548. /**
  549. * Send custom register email with lost password reset link
  550. */
  551. public function send_register_email( $email_address, $link, $username ) {
  552.  
  553. // Email Heading
  554. $email_heading = __("Your account has been created", 'create-customer-order');
  555. apply_filters( "woocommerce_create_customer_order_email_title", $email_heading );
  556.  
  557. // Email Subject
  558. $email_subject = __("Your account on %s", 'create-customer-order');
  559. $email_subject = sprintf( $email_subject, get_bloginfo("name") );
  560. apply_filters("woocommerce_create_customer_order_email_subject", $email_subject);
  561.  
  562. // Email Headers
  563. $headers[] = 'From: '.get_option( 'woocommerce_email_from_name' ).' <'.get_option( 'woocommerce_email_from_address' ).'>';
  564. add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
  565.  
  566. // Email Message
  567. $email_message = __("Hi, we've created an account for you on our site.", 'create-customer-order');
  568. $email_message = get_option( "ec_deluxe_customer_new_account_main_text" );
  569.  
  570. $email_message = nl2br( sprintf(
  571. $email_message,
  572. //get_bloginfo( 'name' ),
  573. $username,
  574. "<a href='".$link."'>".__( "click here", 'create-customer-order' )."</a>",
  575. $link,
  576. get_bloginfo("name")
  577. ) );
  578.  
  579. // Email - Start
  580. ob_start();
  581.  
  582. if ( function_exists( 'wc_get_template' ) ) {
  583. wc_get_template('emails/email-header.php', array( 'email_heading' => $email_heading ));
  584. } else {
  585. woocommerce_get_template('emails/email-header.php', array( 'email_heading' => $email_heading ));
  586. }
  587.  
  588. echo $email_message;
  589.  
  590. if ( function_exists( 'wc_get_template' ) ) {
  591. wc_get_template('emails/email-footer.php');
  592. } else {
  593. woocommerce_get_template('emails/email-footer.php');
  594. }
  595.  
  596. // Email Message - End
  597. $email_message = ob_get_clean();
  598.  
  599. apply_filters( 'woocommerce_create_customer_order_email_msg', $email_message );
  600.  
  601. // Send Email
  602. $status = wc_mail( $email_address, $email_subject, $email_message, $headers );
  603.  
  604. remove_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
  605. }
  606.  
  607. /**
  608. * WP Mail Filter - Set email body as HTML
  609. */
  610. public function set_html_content_type() {
  611. return 'text/html';
  612. }
  613.  
  614. /**
  615. * Save Billing and Shipping details to the customer when checkboxes are checked on Order page
  616. */
  617. public function save_address_from_order_to_customer( $post_id, $post=null ) {
  618. $user_id = absint( $_POST['customer_user'] );
  619.  
  620. $save_to_billing_address = ( isset( $_POST['save-billing-address-input'] ) ) ? $_POST['save-billing-address-input'] : '';
  621. $save_to_shipping_address = ( isset( $_POST['save-shipping-address-input'] ) ) ? $_POST['save-shipping-address-input'] : '';
  622.  
  623. if ($save_to_billing_address == 'true') {
  624. update_user_meta( $user_id, 'billing_first_name', woocommerce_clean( $_POST['_billing_first_name'] ) );
  625. update_user_meta( $user_id, 'billing_last_name', woocommerce_clean( $_POST['_billing_last_name'] ) );
  626. update_user_meta( $user_id, 'billing_company', woocommerce_clean( $_POST['_billing_company'] ) );
  627. update_user_meta( $user_id, 'billing_address_1', woocommerce_clean( $_POST['_billing_address_1'] ) );
  628. update_user_meta( $user_id, 'billing_address_2', woocommerce_clean( $_POST['_billing_address_2'] ) );
  629. update_user_meta( $user_id, 'billing_city', woocommerce_clean( $_POST['_billing_city'] ) );
  630. update_user_meta( $user_id, 'billing_postcode', woocommerce_clean( $_POST['_billing_postcode'] ) );
  631. update_user_meta( $user_id, 'billing_country', woocommerce_clean( $_POST['_billing_country'] ) );
  632. update_user_meta( $user_id, 'billing_state', woocommerce_clean( $_POST['_billing_state'] ) );
  633. update_user_meta( $user_id, 'billing_email', woocommerce_clean( $_POST['_billing_email'] ) );
  634. update_user_meta( $user_id, 'billing_phone', woocommerce_clean( $_POST['_billing_phone'] ) );
  635. }
  636.  
  637. if ($save_to_shipping_address == 'true') {
  638. update_user_meta( $user_id, 'shipping_first_name', woocommerce_clean( $_POST['_shipping_first_name'] ) );
  639. update_user_meta( $user_id, 'shipping_last_name', woocommerce_clean( $_POST['_shipping_last_name'] ) );
  640. update_user_meta( $user_id, 'shipping_company', woocommerce_clean( $_POST['_shipping_company'] ) );
  641. update_user_meta( $user_id, 'shipping_address_1', woocommerce_clean( $_POST['_shipping_address_1'] ) );
  642. update_user_meta( $user_id, 'shipping_address_2', woocommerce_clean( $_POST['_shipping_address_2'] ) );
  643. update_user_meta( $user_id, 'shipping_city', woocommerce_clean( $_POST['_shipping_city'] ) );
  644. update_user_meta( $user_id, 'shipping_postcode', woocommerce_clean( $_POST['_shipping_postcode'] ) );
  645. update_user_meta( $user_id, 'shipping_country', woocommerce_clean( $_POST['_shipping_country'] ) );
  646. update_user_meta( $user_id, 'shipping_state', woocommerce_clean( $_POST['_shipping_state'] ) );
  647. }
  648. }
  649.  
  650. /**
  651. * Is WooCommerce active.
  652. */
  653. public static function is_woocommerce_active() {
  654.  
  655. $active_plugins = (array) get_option( 'active_plugins', array() );
  656.  
  657. if ( is_multisite() )
  658. $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
  659.  
  660. return in_array( 'woocommerce/woocommerce.php', $active_plugins ) || array_key_exists( 'woocommerce/woocommerce.php', $active_plugins );
  661. }
  662.  
  663. /**
  664. * Display Notifications on specific criteria.
  665. *
  666. * @since 2.14
  667. */
  668. public static function woocommerce_inactive_notice() {
  669. if ( current_user_can( 'activate_plugins' ) ) :
  670. if ( !class_exists( 'WooCommerce' ) ) :
  671. ?>
  672. <div id="message" class="error">
  673. <p>
  674. <?php
  675. printf(
  676. __( '%sCreate Customer on Order for WooCommerce needs WooCommerce%s %sWooCommerce%s must be active for Create Customer on Order to work. Please install & activate WooCommerce.', 'create-customer-order' ),
  677. '<strong>',
  678. '</strong><br>',
  679. '<a href="http://wordpress.org/extend/plugins/woocommerce/" target="_blank" >',
  680. '</a>'
  681. );
  682. ?>
  683. </p>
  684. </div>
  685. <?php
  686. elseif ( version_compare( get_option( 'woocommerce_db_version' ), WC_CREATE_CUSTOMER_ON_ORDER_REQUIRED_WOOCOMMERCE_VERSION, '<' ) ) :
  687. ?>
  688. <div id="message" class="error">
  689. <!--<p style="float: right; color: #9A9A9A; font-size: 13px; font-style: italic;">For more information <a href="http://cxthemes.com/plugins/update-notice.html" target="_blank" style="color: inheret;">click here</a></p>-->
  690. <p>
  691. <?php
  692. printf(
  693. __( '%sCreate Customer on Order for WooCommerce is inactive%s This version of Create Customer on Order requires WooCommerce %s or newer. For more information about our WooCommerce version support %sclick here%s.', 'create-customer-order' ),
  694. '<strong>',
  695. '</strong><br>',
  696. WC_CREATE_CUSTOMER_ON_ORDER_REQUIRED_WOOCOMMERCE_VERSION,
  697. '<a href="https://helpcx.zendesk.com/hc/en-us/articles/202241041/" target="_blank" style="color: inheret;" >',
  698. '</a>'
  699. );
  700. ?>
  701. </p>
  702. <div style="clear:both;"></div>
  703. </div>
  704. <?php
  705. endif;
  706. endif;
  707. }
  708.  
  709. }
  710.  
  711. /**
  712. * Instantiate plugin.
  713. */
  714.  
  715. if( !function_exists( 'init_wc_create_customer_on_order' ) ) {
  716. function init_wc_create_customer_on_order() {
  717.  
  718. global $wc_create_customer_on_order;
  719.  
  720. $wc_create_customer_on_order = WC_Create_Customer_On_Order::get_instance();
  721. }
  722. }
  723. add_action( 'plugins_loaded', 'init_wc_create_customer_on_order' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement