Last active
June 7, 2023 11:34
-
-
Save helgatheviking/c3381322bade0227d762ba1cf429271e to your computer and use it in GitHub Desktop.
Add a shipping email/phone field to checkout and notify of new orders
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
<?php | |
/* | |
Plugin Name: WooCommerce Shipping Contact | |
Plugin URI: https://github.com/helgatheviking/wc-shipping-contact | |
Description: Add a shipping email field to checkout and notify of new orders | |
Version: 1.1.0 | |
Author: Kathy Darling | |
Author URI: http://kathyisawesome.com | |
Requires at least: 4.0 | |
Tested up to: 4.8 | |
WC Version: 3.5.0 | |
Copyright: © 2016 Kathy Darling. | |
License: GNU General Public License v3.0 | |
License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
/** | |
* The Main WC_Shipping_Contact class | |
**/ | |
if ( ! class_exists( 'WC_Shipping_Contact' ) ) : | |
class WC_Shipping_Contact { | |
/** | |
* @var WC_Shipping_Contact - the single instance of the class | |
* @since 1.0 | |
*/ | |
protected static $_instance = null; | |
/** | |
* Main WC_Shipping_Contact Instance | |
* | |
* Ensures only one instance of WC_Shipping_Contact is loaded or can be loaded. | |
* | |
* @static | |
* @see WC_Shipping_Contact() | |
* @return WC_Shipping_Contact - Main instance | |
* @since 1.0 | |
*/ | |
public static function instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new self(); | |
} | |
return self::$_instance; | |
} | |
/** | |
* Cloning is forbidden. | |
* | |
* @since 1.0 | |
*/ | |
public function __clone() { | |
_doing_it_wrong( __FUNCTION__, __( 'Cloning this object is forbidden.', 'wc-shipping-contact' ), '1.0' ); | |
} | |
/** | |
* Unserializing instances of this class is forbidden. | |
* | |
* @since 1.0 | |
*/ | |
public function __wakeup() { | |
_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'wc-shipping-contact' ), '1.0' ); | |
} | |
/** | |
* WC_Shipping_Contact Constructor | |
* | |
* @access public | |
* @return WC_Shipping_Contact | |
* @since 1.0 | |
*/ | |
public function __construct() { | |
$this->email_id = 'email'; | |
$this->email_meta = '_shipping_email'; | |
$this->email_label = __( 'Shipping Email', 'wc-shipping-contact' ); | |
$this->phone_id = 'phone'; | |
$this->phone_meta = '_shipping_phone'; | |
$this->phone_label = __( 'Shipping Phone', 'wc-shipping-contact' ); | |
// add fields to checkout | |
add_filter( 'woocommerce_shipping_fields' , array( $this, 'add_shipping_fields' ) ); | |
// add emails to admin order view | |
add_filter( 'woocommerce_admin_shipping_fields' , array( $this, 'admin_shipping_fields' ) ); | |
// add recipient to specific emails | |
add_filter( 'woocommerce_email_recipient_customer_processing_order' , array( $this, 'add_recipient' ), 20, 2 ); | |
add_filter( 'woocommerce_email_recipient_customer_completed_order' , array( $this, 'add_recipient' ), 20, 2 ); | |
add_filter( 'woocommerce_email_recipient_customer_note' , array( $this, 'add_recipient' ), 20, 2 ); | |
// display meta key in order overview | |
add_action( 'woocommerce_order_details_after_customer_details' , array( $this, 'after_customer_details' ) ); | |
// display meta key in email | |
add_action( 'woocommerce_email_customer_details' , array( $this, 'email_after_customer_details' ), 15, 3 ); | |
} | |
/*-----------------------------------------------------------------------------------*/ | |
/* Plugin Functions */ | |
/*-----------------------------------------------------------------------------------*/ | |
/** | |
* Add email to front-end shipping fields | |
* | |
* @var array $fields | |
* @return array | |
* @since 1.0 | |
*/ | |
function add_shipping_fields( $fields ) { | |
$fields['shipping_' . $this->email_id] = array( | |
'label' => $this->email_label, | |
'required' => true, | |
'class' => array( 'form-row-first' ), | |
'validate' => array( 'email' ), | |
); | |
$fields['shipping_' . $this->phone_id] = array( | |
'label' => $this->phone_label, | |
'required' => false, | |
'type' => 'tel', | |
'class' => array( 'form-row-last' ), | |
'clear' => true, | |
'validate' => array( 'phone' ), | |
); | |
return $fields; | |
} | |
/** | |
* Add email to Admin Order overview | |
* | |
* @var array $fields | |
* @return array | |
* @since 1.0 | |
*/ | |
function admin_shipping_fields( $fields ) { | |
$fields[$this->email_id] = array( | |
'label' => $this->email_label | |
); | |
$fields[$this->phone_id] = array( | |
'label' => $this->phone_label, | |
'wrapper_class' => '_shipping_state_field' // borrow a class from WC that will float it right | |
); | |
return $fields; | |
} | |
/** | |
* Add recipient to emails | |
* | |
* @param str $email, comma-delimited list of addresses | |
* @param obj WC_Order $order | |
* @return str | |
* @since 1.0 | |
*/ | |
function add_recipient( $email, $order ) { | |
$additional_email = get_post_meta( $order->get_id(), $this->email_meta, true ); | |
if( $additional_email && is_email( $additional_email )){ | |
$email = explode( ',', $email ); | |
array_push( $email, $additional_email ); | |
$email = implode(",", $email); | |
} | |
return $email; | |
} | |
/** | |
* Display meta in my-account area Order overview | |
* | |
* @var object $order | |
* @return null | |
* @since 1.0 | |
*/ | |
public function after_customer_details( $order ){ | |
$email = get_post_meta( $order->get_id(), $this->email_meta, true ); | |
if( $email ){ | |
echo '<dt>' . $this->email_label . ':</dt><dd>' . $email . '</dd>'; | |
} | |
$phone = get_post_meta( $order->get_id(), $this->phone_meta, true ); | |
if( $phone ){ | |
echo '<dt>' . $this->phone_label . ':</dt><dd>' . $phone . '</dd>'; | |
} | |
} | |
/** | |
* Display meta in my-account area Order overview | |
* | |
* @var array $fields | |
* @return array | |
* @since 1.0 | |
*/ | |
public function email_after_customer_details( $order, $sent_to_admin = false, $plain_text = false ){ | |
$email = get_post_meta( $order->get_id(), $this->email_meta, true ); | |
$phone = get_post_meta( $order->get_id(), $this->phone_meta, true ); | |
if ( $plain_text ) { | |
if( $email ){ | |
echo $this->email_label . ': ' . $email . "\n"; | |
} | |
if( $phone ){ | |
echo $this->phone_label . ': ' . $phone . "\n"; | |
} | |
} else { | |
if( $email ){ | |
echo '<p><strong>' . $this->email_label . ':</strong> ' . $email . '</p>'; | |
} | |
if( $phone ){ | |
echo '<p><strong>' . $this->phone_label . ':</strong> ' . $phone . '</p>'; | |
} | |
} | |
} | |
} //end class: do not remove or there will be no more guacamole for you | |
endif; // end class_exists check | |
/** | |
* Returns the main instance of WC_Shipping_Contact to prevent the need to use globals. | |
* | |
* @since 1.0 | |
* @return WooCommerce | |
*/ | |
function wc_shipping_contact() { | |
return WC_Shipping_Contact::instance(); | |
} | |
// Launch the whole plugin | |
add_action( 'woocommerce_loaded', 'wc_shipping_contact' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
moved here: https://github.com/helgatheviking/wc-shipping-contact