Merge pull request #10577 from justinshreve/customer-crud

Customer CRUD
This commit is contained in:
Mike Jolley 2016-03-18 11:12:10 +00:00
commit 07da5f8615
19 changed files with 1609 additions and 414 deletions

View File

@ -874,7 +874,7 @@ class WC_Product {
$tax_rates = WC_Tax::get_rates( $this->get_tax_class() ); $tax_rates = WC_Tax::get_rates( $this->get_tax_class() );
$base_tax_rates = WC_Tax::get_base_tax_rates( $this->tax_class ); $base_tax_rates = WC_Tax::get_base_tax_rates( $this->tax_class );
if ( ! empty( WC()->customer ) && WC()->customer->is_vat_exempt() ) { if ( ! empty( WC()->customer ) && WC()->customer->get_is_vat_exempt() ) {
$base_taxes = WC_Tax::calc_tax( $price * $qty, $base_tax_rates, true ); $base_taxes = WC_Tax::calc_tax( $price * $qty, $base_tax_rates, true );
$base_tax_amount = array_sum( $base_taxes ); $base_tax_amount = array_sum( $base_taxes );

View File

@ -144,7 +144,7 @@ abstract class WC_Shipping_Method extends WC_Settings_API {
* @return boolean * @return boolean
*/ */
public function is_taxable() { public function is_taxable() {
return wc_tax_enabled() && 'taxable' === $this->tax_status && ! WC()->customer->is_vat_exempt(); return wc_tax_enabled() && 'taxable' === $this->tax_status && ! WC()->customer->get_is_vat_exempt();
} }
/** /**

View File

@ -336,7 +336,7 @@ class WC_AJAX {
if ( isset( $_POST['country'] ) ) { if ( isset( $_POST['country'] ) ) {
WC()->customer->set_shipping_country( $_POST['country'] ); WC()->customer->set_shipping_country( $_POST['country'] );
WC()->customer->calculated_shipping( true ); WC()->customer->set_calculated_shipping( true );
} }
if ( isset( $_POST['state'] ) ) { if ( isset( $_POST['state'] ) ) {
@ -362,7 +362,7 @@ class WC_AJAX {
if ( isset( $_POST['s_country'] ) ) { if ( isset( $_POST['s_country'] ) ) {
WC()->customer->set_shipping_country( $_POST['s_country'] ); WC()->customer->set_shipping_country( $_POST['s_country'] );
WC()->customer->calculated_shipping( true ); WC()->customer->set_calculated_shipping( true );
} }
if ( isset( $_POST['s_state'] ) ) { if ( isset( $_POST['s_state'] ) ) {
@ -386,6 +386,7 @@ class WC_AJAX {
} }
} }
WC()->customer->save();
WC()->cart->calculate_totals(); WC()->cart->calculate_totals();
// Get order review fragment // Get order review fragment

View File

@ -56,11 +56,12 @@ class WC_Cache_Helper {
*/ */
public static function geolocation_ajax_get_location_hash() { public static function geolocation_ajax_get_location_hash() {
$customer = new WC_Customer(); $customer = new WC_Customer();
$customer->load_session();
$location = array(); $location = array();
$location['country'] = $customer->get_country(); $location['country'] = $customer->get_billing_country();
$location['state'] = $customer->get_state(); $location['state'] = $customer->get_billing_state();
$location['postcode'] = $customer->get_postcode(); $location['postcode'] = $customer->get_billing_postcode();
$location['city'] = $customer->get_city(); $location['city'] = $customer->get_billing_city();
return substr( md5( implode( '', $location ) ), 0, 12 ); return substr( md5( implode( '', $location ) ), 0, 12 );
} }

View File

@ -1353,7 +1353,7 @@ class WC_Cart {
} }
// VAT exemption done at this point - so all totals are correct before exemption // VAT exemption done at this point - so all totals are correct before exemption
if ( WC()->customer->is_vat_exempt() ) { if ( WC()->customer->get_is_vat_exempt() ) {
$this->remove_taxes(); $this->remove_taxes();
} }
@ -1369,7 +1369,7 @@ class WC_Cart {
$this->tax_total = WC_Tax::get_tax_total( $this->taxes ); $this->tax_total = WC_Tax::get_tax_total( $this->taxes );
// VAT exemption done at this point - so all totals are correct before exemption // VAT exemption done at this point - so all totals are correct before exemption
if ( WC()->customer->is_vat_exempt() ) { if ( WC()->customer->get_is_vat_exempt() ) {
$this->remove_taxes(); $this->remove_taxes();
} }
} }
@ -1516,7 +1516,7 @@ class WC_Cart {
return false; return false;
if ( 'yes' === get_option( 'woocommerce_shipping_cost_requires_address' ) ) { if ( 'yes' === get_option( 'woocommerce_shipping_cost_requires_address' ) ) {
if ( ! WC()->customer->has_calculated_shipping() ) { if ( ! WC()->customer->get_calculated_shipping() ) {
if ( ! WC()->customer->get_shipping_country() || ( ! WC()->customer->get_shipping_state() && ! WC()->customer->get_shipping_postcode() ) ) { if ( ! WC()->customer->get_shipping_country() || ( ! WC()->customer->get_shipping_state() && ! WC()->customer->get_shipping_postcode() ) ) {
return false; return false;
} }

View File

@ -316,18 +316,26 @@ class WC_Checkout {
$order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' ); $order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' );
$order->set_total( WC()->cart->total ); $order->set_total( WC()->cart->total );
$customer = new WC_Customer( $this->customer_id );
// Update user meta // Update user meta
if ( $this->customer_id ) { if ( $this->customer_id ) {
if ( apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) ) { if ( apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) ) {
foreach ( $billing_address as $key => $value ) { foreach ( $billing_address as $key => $value ) {
update_user_meta( $this->customer_id, 'billing_' . $key, $value ); if ( is_callable( array( $customer, "set_billing_{$key}" ) ) ) {
$customer->{"set_billing_{$key}"}( $value );
}
} }
if ( WC()->cart->needs_shipping() ) { if ( WC()->cart->needs_shipping() ) {
foreach ( $shipping_address as $key => $value ) { foreach ( $shipping_address as $key => $value ) {
update_user_meta( $this->customer_id, 'shipping_' . $key, $value ); if ( is_callable( array( $customer, "set_shipping_{$key}" ) ) ) {
$customer->{"set_shipping_{$key}"}( $value );
}
} }
} }
} }
$customer->save();
do_action( 'woocommerce_checkout_update_user_meta', $this->customer_id, $this->posted ); do_action( 'woocommerce_checkout_update_user_meta', $this->customer_id, $this->posted );
} }
@ -554,6 +562,8 @@ class WC_Checkout {
} }
WC()->customer->save();
// Update cart totals now we have customer address // Update cart totals now we have customer address
WC()->cart->calculate_totals(); WC()->cart->calculate_totals();
@ -796,15 +806,15 @@ class WC_Checkout {
switch ( $input ) { switch ( $input ) {
case 'billing_country' : case 'billing_country' :
return apply_filters( 'default_checkout_country', WC()->customer->get_country() ? WC()->customer->get_country() : WC()->countries->get_base_country(), 'billing' ); return apply_filters( 'default_checkout_country', WC()->customer->get_billing_country() ? WC()->customer->get_billing_country() : WC()->countries->get_base_country(), 'billing' );
case 'billing_state' : case 'billing_state' :
return apply_filters( 'default_checkout_state', WC()->customer->has_calculated_shipping() ? WC()->customer->get_state() : '', 'billing' ); return apply_filters( 'default_checkout_state', WC()->customer->get_calculated_shipping() ? WC()->customer->get_billing_state() : '', 'billing' );
case 'billing_postcode' : case 'billing_postcode' :
return apply_filters( 'default_checkout_postcode', WC()->customer->get_postcode() ? WC()->customer->get_postcode() : '', 'billing' ); return apply_filters( 'default_checkout_postcode', WC()->customer->get_billing_postcode() ? WC()->customer->get_billing_postcode() : '', 'billing' );
case 'shipping_country' : case 'shipping_country' :
return apply_filters( 'default_checkout_country', WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->countries->get_base_country(), 'shipping' ); return apply_filters( 'default_checkout_country', WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->countries->get_base_country(), 'shipping' );
case 'shipping_state' : case 'shipping_state' :
return apply_filters( 'default_checkout_state', WC()->customer->has_calculated_shipping() ? WC()->customer->get_shipping_state() : '', 'shipping' ); return apply_filters( 'default_checkout_state', WC()->customer->get_calculated_shipping() ? WC()->customer->get_shipping_state() : '', 'shipping' );
case 'shipping_postcode' : case 'shipping_postcode' :
return apply_filters( 'default_checkout_postcode', WC()->customer->get_shipping_postcode() ? WC()->customer->get_shipping_postcode() : '', 'shipping' ); return apply_filters( 'default_checkout_postcode', WC()->customer->get_shipping_postcode() ? WC()->customer->get_shipping_postcode() : '', 'shipping' );
default : default :

File diff suppressed because it is too large Load Diff

View File

@ -292,6 +292,8 @@ class WC_Form_Handler {
WC()->customer->set_city( $order->billing_city ); WC()->customer->set_city( $order->billing_city );
} }
WC()->customer->save();
// Terms // Terms
if ( ! empty( $_POST['terms-field'] ) && empty( $_POST['terms'] ) ) { if ( ! empty( $_POST['terms-field'] ) && empty( $_POST['terms'] ) ) {
wc_add_notice( __( 'You must accept our Terms & Conditions.', 'woocommerce' ), 'error' ); wc_add_notice( __( 'You must accept our Terms & Conditions.', 'woocommerce' ), 'error' );

View File

@ -62,7 +62,7 @@ class WC_Shipping_Rate {
* @return array * @return array
*/ */
public function get_shipping_tax() { public function get_shipping_tax() {
return apply_filters( 'woocommerce_get_shipping_tax', sizeof( $this->taxes ) > 0 && ! WC()->customer->is_vat_exempt() ? array_sum( $this->taxes ) : 0, $this ); return apply_filters( 'woocommerce_get_shipping_tax', sizeof( $this->taxes ) > 0 && ! WC()->customer->get_is_vat_exempt() ? array_sum( $this->taxes ) : 0, $this );
} }
/** /**

View File

@ -0,0 +1,224 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Legacy Customer.
*
* @version 2.7.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
abstract class WC_Legacy_Customer extends WC_Data {
/**
* __isset legacy.
* @param mixed $key
* @return bool
*/
public function __isset( $key ) {
$legacy_keys = array(
'country', 'state', 'postcode' ,'city', 'address_1', 'address', 'address_2', 'shipping_country', 'shipping_state',
'shipping_postcode', 'shipping_city', 'shipping_address_1', 'shipping_address', 'shipping_address_2', 'is_vat_exempt', 'calculated_shipping',
);
$key = $this->filter_legacy_key( $key );
return in_array( $key, $legacy_keys );
}
/**
* __get function.
* @todo use get_* methods
* @param string $key
* @return string
*/
public function __get( $key ) {
_doing_it_wrong( $key, 'Customer properties should not be accessed directly.', '2.7' );
$key = $this->filter_legacy_key( $key );
if ( in_array( $key, array( 'country', 'state', 'postcode' ,'city', 'address_1', 'address', 'address_2' ) ) ) {
$key = 'billing_' . $key;
}
return isset( $this->_data[ $key ] ) ? $this->_data[ $key ] : '';
}
/**
* __set function.
* @todo use set_* methods
* @param mixed $property
* @param mixed $key
*/
public function __set( $key, $value ) {
_doing_it_wrong( $key, 'Customer properties should not be set directly.', '2.7' );
$key = $this->filter_legacy_key( $key );
$this->_data[ $key ] = $value;
$this->_changed = true;
}
/**
* Address and shipping_address are aliased, so we want to get the 'real' key name.
* For all other keys, we can just return it.
* @since 2.7.0
* @param string $key
* @return string
*/
private function filter_legacy_key( $key ) {
if ( 'address' === $key ) {
$key = 'address_1';
}
if ( 'shipping_address' === $key ) {
$key = 'shipping_address_1';
}
return $key;
}
/**
* Is customer VAT exempt?
* @return bool
*/
public function is_vat_exempt() {
_deprecated_function( 'WC_Customer::is_vat_exempt', '2.7', 'WC_Customer::get_is_vat_exempt' );
return $this->get_is_vat_exempt();
}
/**
* Has calculated shipping?
* @return bool
*/
public function has_calculated_shipping() {
_deprecated_function( 'WC_Customer::has_calculated_shipping', '2.7', 'WC_Customer::get_calculated_shipping' );
return $this->get_calculated_shipping();
}
/**
* Get default country for a customer.
* @return string
*/
public function get_default_country() {
_deprecated_function( 'WC_Customer::get_default_country', '2.7', 'wc_get_customer_default_location' );
$default = wc_get_customer_default_location();
return $default['country'];
}
/**
* Get default state for a customer.
* @return string
*/
public function get_default_state() {
_deprecated_function( 'WC_Customer::get_default_state', '2.7', 'wc_get_customer_default_location' );
$default = wc_get_customer_default_location();
return $default['state'];
}
/**
* Set customer address to match shop base address.
*/
public function set_to_base() {
_deprecated_function( 'WC_Customer::set_to_base', '2.7', 'WC_Customer::set_billing_address_to_base' );
$this->set_billing_address_to_base();
}
/**
* Set customer shipping address to base address.
*/
public function set_shipping_to_base() {
_deprecated_function( 'WC_Customer::set_shipping_to_base', '2.7', 'WC_Customer::set_shipping_address_to_base' );
$this->set_shipping_address_to_base();
}
/**
* Calculated shipping.
* @param boolean $calculated
*/
public function calculated_shipping( $calculated = true ) {
_deprecated_function( 'WC_Customer::calculated_shipping', '2.7', 'WC_Customer::set_calculated_shipping' );
$this->set_calculated_shipping( $calculated );
}
/**
* Set default data for a customer.
*/
public function set_default_data() {
_deprecated_function( 'WC_Customer::set_default_data', '2.7', '' );
}
/**
* Is the user a paying customer?
* @todo should this be moved to a get_ readonly?
* @return bool
*/
function is_paying_customer( $user_id = '' ) {
_deprecated_function( 'WC_Customer::is_paying_customer', '2.7', 'WC_Customer::get_is_paying_customer' );
if ( ! empty( $user_id ) ) {
$user_id = get_current_user_id();
}
return '1' === get_user_meta( $user_id, 'paying_customer', true );
}
/**
* Legacy get country.
*/
function get_country() {
_deprecated_function( 'WC_Customer::get_country', '2.7', 'WC_Customer::get_billing_country' );
return $this->get_billing_country();
}
/**
* Legacy get state.
*/
function get_state() {
_deprecated_function( 'WC_Customer::get_state', '2.7', 'WC_Customer::get_billing_state' );
return $this->get_billing_state();
}
/**
* Legacy get postcode.
*/
function get_postcode() {
_deprecated_function( 'WC_Customer::get_postcode', '2.7', 'WC_Customer::get_billing_postcode' );
return $this->get_billing_postcode();
}
/**
* Legacy get city.
*/
function get_city() {
_deprecated_function( 'WC_Customer::get_city', '2.7', 'WC_Customer::get_billing_city' );
return $this->get_billing_city();
}
/**
* Legacy set country.
*/
function set_country( $country ) {
_deprecated_function( 'WC_Customer::set_country', '2.7', 'WC_Customer::set_billing_country' );
$this->set_billing_country( $country );
}
/**
* Legacy set state.
*/
function set_state( $state ) {
_deprecated_function( 'WC_Customer::set_state', '2.7', 'WC_Customer::set_billing_state' );
$this->set_billing_state( $state );
}
/**
* Legacy set postcode.
*/
function set_postcode( $postcode ) {
_deprecated_function( 'WC_Customer::set_postcode', '2.7', 'WC_Customer::set_billing_postcode' );
$this->set_billing_postcode( $postcode );
}
/**
* Legacy set city.
*/
function set_city( $city ) {
_deprecated_function( 'WC_Customer::set_city', '2.7', 'WC_Customer::set_billing_city' );
$this->set_billing_city( $city );
}
}

View File

@ -37,7 +37,9 @@ class WC_Shortcode_Cart {
WC()->customer->set_shipping_to_base(); WC()->customer->set_shipping_to_base();
} }
WC()->customer->calculated_shipping( true ); WC()->customer->set_calculated_shipping( true );
WC()->customer->save_to_session();
wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' ); wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );

View File

@ -105,6 +105,8 @@ class WC_Shortcode_Checkout {
WC()->customer->set_postcode( $order->billing_postcode ); WC()->customer->set_postcode( $order->billing_postcode );
} }
WC()->customer->save();
$available_gateways = WC()->payment_gateways->get_available_payment_gateways(); $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
if ( sizeof( $available_gateways ) ) { if ( sizeof( $available_gateways ) ) {

View File

@ -275,7 +275,7 @@ function wc_cart_totals_order_total_html() {
if ( ! empty( $tax_string_array ) ) { if ( ! empty( $tax_string_array ) ) {
$taxable_address = WC()->customer->get_taxable_address(); $taxable_address = WC()->customer->get_taxable_address();
$estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping() $estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->get_calculated_shipping()
? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] ) ? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )
: ''; : '';
$value .= '<small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>'; $value .= '<small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>';

View File

@ -44,7 +44,7 @@ if ( ! defined( 'ABSPATH' ) ) {
printf( '%3$s <input type="hidden" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d" value="%2$s" class="shipping_method" />', $index, esc_attr( $method->id ), wc_cart_totals_shipping_method_label( $method ) ); printf( '%3$s <input type="hidden" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d" value="%2$s" class="shipping_method" />', $index, esc_attr( $method->id ), wc_cart_totals_shipping_method_label( $method ) );
do_action( 'woocommerce_after_shipping_rate', $method, $index ); do_action( 'woocommerce_after_shipping_rate', $method, $index );
?> ?>
<?php elseif ( ! WC()->customer->has_calculated_shipping() ) : ?> <?php elseif ( ! WC()->customer->get_calculated_shipping() ) : ?>
<?php echo wpautop( __( 'Shipping costs will be calculated once you have provided your address.', 'woocommerce' ) ); ?> <?php echo wpautop( __( 'Shipping costs will be calculated once you have provided your address.', 'woocommerce' ) ); ?>
<?php else : ?> <?php else : ?>
<?php echo apply_filters( is_cart() ? 'woocommerce_cart_no_shipping_available_html' : 'woocommerce_no_shipping_available_html', wpautop( __( 'There are no shipping methods available. Please double check your address, or contact us if you need any help.', 'woocommerce' ) ) ); ?> <?php echo apply_filters( is_cart() ? 'woocommerce_cart_no_shipping_available_html' : 'woocommerce_no_shipping_available_html', wpautop( __( 'There are no shipping methods available. Please double check your address, or contact us if you need any help.', 'woocommerce' ) ) ); ?>

View File

@ -21,7 +21,7 @@ if ( ! defined( 'ABSPATH' ) ) {
} }
?> ?>
<div class="cart_totals <?php if ( WC()->customer->has_calculated_shipping() ) echo 'calculated_shipping'; ?>"> <div class="cart_totals <?php if ( WC()->customer->get_calculated_shipping() ) echo 'calculated_shipping'; ?>">
<?php do_action( 'woocommerce_before_cart_totals' ); ?> <?php do_action( 'woocommerce_before_cart_totals' ); ?>
@ -67,7 +67,7 @@ if ( ! defined( 'ABSPATH' ) ) {
<?php if ( wc_tax_enabled() && 'excl' === WC()->cart->tax_display_cart ) : <?php if ( wc_tax_enabled() && 'excl' === WC()->cart->tax_display_cart ) :
$taxable_address = WC()->customer->get_taxable_address(); $taxable_address = WC()->customer->get_taxable_address();
$estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping() $estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->get_calculated_shipping()
? sprintf( ' <small>(' . __( 'estimated for %s', 'woocommerce' ) . ')</small>', WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] ) ? sprintf( ' <small>(' . __( 'estimated for %s', 'woocommerce' ) . ')</small>', WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )
: ''; : '';

View File

@ -34,7 +34,34 @@ class WC_Helper_Customer {
WC_Helper_Customer::set_customer_details( $customer_data ); WC_Helper_Customer::set_customer_details( $customer_data );
return new WC_Customer(); $customer = new WC_Customer();
$customer->load_session();
return $customer;
}
/**
* Creates a customer in the tests DB.
*/
public static function create_customer() {
$customer = new WC_Customer();
$customer->set_billing_country( 'US' );
$customer->set_first_name( 'Justin' );
$customer->set_billing_state( 'PA' );
$customer->set_billing_postcode( '19123' );
$customer->set_billing_city( 'Philadelphia' );
$customer->set_billing_address( '123 South Street' );
$customer->set_billing_address_2( 'Apt 1' );
$customer->set_shipping_country( 'US' );
$customer->set_shipping_state( 'PA' );
$customer->set_shipping_postcode( '19123' );
$customer->set_shipping_city( 'Philadelphia' );
$customer->set_shipping_address( '123 South Street' );
$customer->set_shipping_address_2( 'Apt 1' );
$customer->set_username( 'testcustomer' );
$customer->set_password( 'hunter2' );
$customer->set_email( 'test@woo.local' );
$customer->create();
return $customer;
} }
/** /**

View File

@ -34,7 +34,7 @@ class WC_Helper_Order {
* *
* @return WC_Order Order object. * @return WC_Order Order object.
*/ */
public static function create_order() { public static function create_order( $customer_id = 1 ) {
// Create product // Create product
$product = WC_Helper_Product::create_simple_product(); $product = WC_Helper_Product::create_simple_product();
@ -42,7 +42,7 @@ class WC_Helper_Order {
$order_data = array( $order_data = array(
'status' => 'pending', 'status' => 'pending',
'customer_id' => 1, 'customer_id' => $customer_id,
'customer_note' => '', 'customer_note' => '',
'total' => '', 'total' => '',
); );

View File

@ -0,0 +1,450 @@
<?php
namespace WooCommerce\Tests\Customer;
/**
* Class CustomerCRUD.
* @package WooCommerce\Tests\Customer
*/
class CustomerCRUD extends \WC_Unit_Test_Case {
/**
* Test creating a new customer.
* @since 2.7.0
*/
public function test_create_customer() {
$username = 'testusername-' . time();
$customer = new \WC_Customer();
$customer->set_username( 'testusername-' . time() );
$customer->set_password( 'test123' );
$customer->set_email( 'test@woo.local' );
$customer->create();
$wp_user = new \WP_User( $customer->get_id() );
$this->assertEquals( $username, $customer->get_username() );
$this->assertNotEquals( 0, $customer->get_id() );
$this->assertEquals( strtotime( $wp_user->user_registered ), $customer->get_date_created() );
}
/**
* Test updating a customer.
* @since 2.7.0
*/
public function test_update_customer() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$this->assertEquals( 'test@woo.local', $customer->get_email() );
$this->assertEquals( 'Apt 1', $customer->get_billing_address_2() );
$customer->set_email( 'test@wc.local' );
$customer->set_first_name( 'Justin' );
$customer->set_billing_address_2( 'Apt 5' );
$customer->update();
$customer = new \WC_Customer( $customer_id ); // so we can read fresh copies from the DB
$this->assertEquals( 'test@wc.local', $customer->get_email() );
$this->assertEquals( 'Justin', $customer->get_first_name() );
$this->assertEquals( 'Apt 5', $customer->get_billing_address_2() );
}
/**
* Test saving a customer.
* @since 2.7.0
*/
public function test_save_customer() {
// test save() -> Create
$customer = new \WC_Customer();
$customer->set_username( 'testusername-' . time() );
$customer->set_password( 'test123' );
$customer->set_email( 'test@woo.local' );
$this->assertEquals( 0, $customer->get_id() );
$customer->save();
$customer_id = $customer->get_id();
$wp_user = new \WP_User( $customer->get_id() );
$this->assertNotEquals( 0, $customer->get_id() );
// test save() -> Update
$this->assertEquals( 'test@woo.local', $customer->get_email() );
$customer->set_email( 'test@wc.local' );
$customer->save();
$customer = new \WC_Customer( $customer_id );
$this->assertEquals( 'test@wc.local', $customer->get_email() );
}
/**
* Test deleting a customer.
* @since 2.7.0
*/
public function test_delete_customer() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$this->assertNotEquals( 0, $customer->get_id() );
$customer->delete();
$customer->read( $customer_id );
$this->assertEquals( 0, $customer->get_id() );
}
/**
* Test reading a customer.
* @since 2.7.0
*/
public function test_read_customer() {
$username = 'user-' . time();
$customer = new \WC_Customer();
$customer->set_username( $username );
$customer->set_email( 'test@woo.local' );
$customer->set_password( 'hunter2' );
$customer->set_first_name( 'Bob' );
$customer->set_last_name( 'Bob' );
$customer->create();
$customer_id = $customer->get_id();
$customer_read = new \WC_Customer();
$customer_read->read( $customer_id );
$this->assertEquals( $customer_id, $customer_read->get_id() );
$this->assertEquals( 'test@woo.local', $customer_read->get_email() );
$this->assertEquals( 'Bob', $customer_read->get_first_name() );
$this->assertEquals( 'Bob', $customer_read->get_last_name() );
$this->assertEquals( $username, $customer_read->get_username() );
}
/**
* Tests backwards compat / legacy handling.
* @expectedDeprecated WC_Customer::is_vat_exempt
* @expectedDeprecated WC_Customer::has_calculated_shipping
* @expectedDeprecated WC_Customer::get_default_country
* @expectedDeprecated WC_Customer::get_default_state
* @expectedDeprecated WC_Customer::is_paying_customer
* @expectedDeprecated WC_Customer::calculated_shipping
* @since 2.7.0
*/
public function test_customer_backwards_compat() {
// Properties.
// Accessing properties directly will throw some wanted deprected notices
// So we need to let PHPUnit know we are expecting them and it's fine to continue
$legacy_keys = array(
'id', 'country', 'state', 'postcode', 'city', 'address', 'address_1', 'address_2',
'shipping_country', 'shipping_state', 'shipping_postcode', 'shipping_city',
'shipping_address', 'shipping_address_1', 'shipping_address_2',
'is_vat_exempt', 'calculated_shipping',
);
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $legacy_keys );
$customer = \WC_Helper_Customer::create_customer();
$this->assertEquals( $customer->get_id(), $customer->id );
$this->assertEquals( $customer->get_billing_country(), $customer->country );
$this->assertEquals( $customer->get_billing_state(), $customer->state );
$this->assertEquals( $customer->get_billing_postcode(), $customer->postcode );
$this->assertEquals( $customer->get_billing_city(), $customer->city );
$this->assertEquals( $customer->get_billing_address(), $customer->address );
$this->assertEquals( $customer->get_billing_address(), $customer->address_1 );
$this->assertEquals( $customer->get_billing_address_2(), $customer->address_2 );
$this->assertEquals( $customer->get_shipping_country(), $customer->shipping_country );
$this->assertEquals( $customer->get_shipping_state(), $customer->shipping_state );
$this->assertEquals( $customer->get_shipping_postcode(), $customer->shipping_postcode );
$this->assertEquals( $customer->get_shipping_city(), $customer->shipping_city );
$this->assertEquals( $customer->get_shipping_address(), $customer->shipping_address );
$this->assertEquals( $customer->get_shipping_address(), $customer->shipping_address_1 );
$this->assertEquals( $customer->get_shipping_address_2(), $customer->shipping_address_2 );
$this->assertEquals( $customer->get_is_vat_exempt(), $customer->is_vat_exempt );
$this->assertEquals( $customer->get_calculated_shipping(), $customer->calculated_shipping );
// Functions
$this->assertEquals( $customer->get_is_vat_exempt(), $customer->is_vat_exempt() );
$this->assertEquals( $customer->get_calculated_shipping(), $customer->has_calculated_shipping() );
$default = wc_get_customer_default_location();
$this->assertEquals( $default['country'], $customer->get_default_country() );
$this->assertEquals( $default['state'], $customer->get_default_state() );
$this->assertFalse( $customer->get_calculated_shipping() );
$customer->calculated_shipping( true );
$this->assertTrue( $customer->get_calculated_shipping() );
$this->assertEquals( $customer->get_is_paying_customer(), $customer->is_paying_customer() );
}
/**
* Test generic getters & setters
* @since 2.7.0
*/
public function test_customer_setters_and_getters() {
$time = time();
$standard_getters_and_setters = array(
'username' => 'test', 'email' => 'test@woo.local', 'first_name' => 'Bob', 'last_name' => 'tester',
'role' => 'customer', 'last_order_id' => 5, 'last_order_date' => $time, 'orders_count' => 2,
'total_spent' => 10.57, 'date_created' => $time, 'date_modified' => $time, 'billing_postcode' => 11010,
'billing_city' => 'New York', 'billing_address' => '123 Main St.', 'billing_address_1' => '123 Main St.', 'billing_address_2' => 'Apt 2', 'billing_state' => 'NY',
'billing_country' => 'US', 'shipping_state' => 'NY', 'shipping_postcode' => 11011, 'shipping_city' =>
'New York', 'shipping_address' => '123 Main St.', 'shipping_address_1' => '123 Main St.',
'shipping_address_2' => 'Apt 2', 'is_vat_exempt' => true, 'calculated_shipping' => true,
'is_paying_customer' => true
);
$customer = new \WC_Customer;
foreach ( $standard_getters_and_setters as $function => $value ) {
$customer->{"set_{$function}"}( $value );
$this->assertEquals( $value, $customer->{"get_{$function}"}(), $function );
}
}
/**
* Test getting a customer's last order ID and date
* @since 2.7.0
*/
public function test_customer_get_last_order_info() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$order = \WC_Helper_Order::create_order( $customer_id );
$customer->read( $customer_id );
$this->assertEquals( $order->id, $customer->get_last_order_id() );
$this->assertEquals( strtotime( $order->order_date ), $customer->get_last_order_date() );
}
/**
* Test getting a customer's order count from DB.
* @since 2.7.0
*/
public function test_customer_get_orders_count_read() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
\WC_Helper_Order::create_order( $customer_id );
\WC_Helper_Order::create_order( $customer_id );
\WC_Helper_Order::create_order( $customer_id );
$customer->read( $customer_id );
$this->assertEquals( 3, $customer->get_orders_count() );
}
/**
* Test getting a customer's total amount spent from DB.
* @since 2.7.0
*/
public function test_customer_get_total_spent_read() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$order = \WC_Helper_Order::create_order( $customer_id );
$customer->read( $customer_id );
$this->assertEquals( 0, $customer->get_total_spent() );
$order->update_status( 'wc-completed' );
$customer->read( $customer_id );
$this->assertEquals( 40, $customer->get_total_spent() );
}
/**
* Test getting a customer's avatar URL.
* @since 2.7.0
*/
public function test_customer_get_avatar_url() {
$customer = \WC_Helper_Customer::create_customer();
$this->assertContains( 'gravatar.com/avatar', $customer->get_avatar_url() );
$this->assertContains( md5( 'test@woo.local' ), $customer->get_avatar_url() );
}
/**
* Test getting a customer's creation date from DB.
* @since 2.7.0
*/
public function test_customer_get_date_created_read() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$user = new \WP_User( $customer_id );
$this->assertEquals( strtotime( $user->data->user_registered ), $customer->get_date_created() );
}
/**
* Test getting a customer's modification date from DB.
* @since 2.7.0
*/
public function test_customer_get_date_modified_read() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$last = get_user_meta( $customer_id, 'last_update', true );
sleep(1);
$this->assertEquals( $last, $customer->get_date_modified() );
$customer->set_billing_address( '1234 Some St.' );
$customer->save();
$update = get_user_meta( $customer_id, 'last_update', true );
$this->assertEquals( $update, $customer->get_date_modified() );
$this->assertNotEquals( $update, $last );
}
/**
* Test getting a customer's taxable address.
* @since 2.7.0
*/
public function test_customer_get_taxable_address() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$customer->set_shipping_postcode( '11111' );
$customer->set_shipping_city( 'Test' );
$customer->save();
$customer->read( $customer_id );
update_option( 'woocommerce_tax_based_on', 'shipping' );
$taxable = $customer->get_taxable_address();
$this->assertEquals( 'US', $taxable[0] );
$this->assertEquals( 'PA', $taxable[1] );
$this->assertEquals( '11111', $taxable[2] );
$this->assertEquals( 'Test', $taxable[3] );
update_option( 'woocommerce_tax_based_on', 'billing' );
$taxable = $customer->get_taxable_address();
$this->assertEquals( 'US', $taxable[0] );
$this->assertEquals( 'PA', $taxable[1] );
$this->assertEquals( '19123', $taxable[2] );
$this->assertEquals( 'Philadelphia', $taxable[3] );
update_option( 'woocommerce_tax_based_on', 'base' );
$taxable = $customer->get_taxable_address();
$this->assertEquals( WC()->countries->get_base_country(), $taxable[0] );
$this->assertEquals( WC()->countries->get_base_state(), $taxable[1] );
$this->assertEquals( WC()->countries->get_base_postcode(), $taxable[2] );
$this->assertEquals( WC()->countries->get_base_city(), $taxable[3] );
}
/**
* Test getting a customer's downloadable products.
* @since 2.7.0
*/
public function test_customer_get_downloadable_products() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$this->assertEquals( wc_get_customer_available_downloads( $customer_id ), $customer->get_downloadable_products() );
}
/**
* Test setting a password on update - making sure it actually changes the users password.
* @since 2.7.0
*/
public function test_customer_password() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$user = get_user_by( 'id', $customer_id );
$this->assertTrue( wp_check_password( 'hunter2', $user->data->user_pass, $user->ID ) );
$customer->set_password( 'hunter3' );
$customer->save();
$user = get_user_by( 'id', $customer_id );
$this->assertTrue( wp_check_password( 'hunter3', $user->data->user_pass, $user->ID ) );
}
/**
* Test setting a customer's address to the base address.
* @since 2.7.0
*/
public function test_customer_set_address_to_base() {
$customer = \WC_Helper_Customer::create_customer();
$customer->set_billing_address_to_base();
$base = wc_get_customer_default_location();
$this->assertEquals( $base['country'], $customer->get_billing_country() );
$this->assertEquals( $base['state'], $customer->get_billing_state() );
$this->assertEmpty( $customer->get_billing_postcode() );
$this->assertEmpty( $customer->get_billing_city() );
}
/**
* Test setting a customer's shipping address to the base address.
* @since 2.7.0
*/
public function test_customer_set_shipping_address_to_base() {
$customer = \WC_Helper_Customer::create_customer();
$customer->set_shipping_address_to_base();
$base = wc_get_customer_default_location();
$this->assertEquals( $base['country'], $customer->get_shipping_country() );
$this->assertEquals( $base['state'], $customer->get_shipping_state() );
$this->assertEmpty( $customer->get_shipping_postcode() );
$this->assertEmpty( $customer->get_shipping_city() );
}
/**
* Test setting a customer's location (multiple address fields at once)
* @since 2.7.0
*/
public function test_customer_set_location() {
$customer = \WC_Helper_Customer::create_customer();
$customer->set_billing_location( 'US', 'OH', '12345', 'Cleveland' );
$this->assertEquals( 'US', $customer->get_billing_country() );
$this->assertEquals( 'OH', $customer->get_billing_state() );
$this->assertEquals( '12345', $customer->get_billing_postcode() );
$this->assertEquals( 'Cleveland', $customer->get_billing_city() );
}
/**
* Test setting a customer's shipping location (multiple address fields at once)
* @since 2.7.0
*/
public function test_customer_set_shipping_location() {
$customer = \WC_Helper_Customer::create_customer();
$customer->set_shipping_location( 'US', 'OH', '12345', 'Cleveland' );
$this->assertEquals( 'US', $customer->get_shipping_country() );
$this->assertEquals( 'OH', $customer->get_shipping_state() );
$this->assertEquals( '12345', $customer->get_shipping_postcode() );
$this->assertEquals( 'Cleveland', $customer->get_shipping_city() );
}
/**
* Test is_customer_outside_base.
* @since 2.7.0
*/
public function test_customer_is_customer_outside_base() {
$customer = \WC_Helper_Customer::create_customer();
$this->assertTrue( $customer->is_customer_outside_base() );
update_option( 'woocommerce_tax_based_on', 'base' );
$customer->set_billing_address_to_base();
$this->assertFalse( $customer->is_customer_outside_base() );
}
/**
* Test WC_Customer's session handling code.
* @since 2.7.0
*/
public function test_customer_sessions() {
$customer = \WC_Helper_Customer::create_customer();
$session = \WC_Helper_Customer::create_mock_customer(); // set into session....
$this->assertNotEmpty( $session->get_id() );
$this->assertFalse( is_numeric( $session->get_id() ) );
$this->assertEquals( '19123', $session->get_billing_postcode() );
$this->assertEquals( '123 South Street', $session->get_billing_address() );
$this->assertEquals( 'Philadelphia', $session->get_billing_city() );
$session->set_billing_address( '124 South Street' );
$session->save_to_session();
$session = new \WC_Customer();
$session->load_session();
$this->assertEquals( '124 South Street', $session->get_billing_address() );
}
/**
* Test getting meta.
* @since 2.7.0
*/
public function test_get_meta() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$meta_value = time() . '-custom-value';
add_user_meta( $customer_id, 'test_field', $meta_value, true );
$customer->read( $customer_id );
$fields = $customer->get_meta_data();
$this->assertCount( 1, $fields );
$this->assertEquals( $meta_value, $customer->get_meta( 'test_field') );
}
/**
* Test setting meta.
* @since 2.7.0
*/
public function test_set_meta() {
$customer = \WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$meta_value = time() . '-custom-value';
$customer->add_meta_data( 'my-field', $meta_value, true );
$customer->save();
$customer->read( $customer_id );
$this->assertEquals( $meta_value, $customer->get_meta( 'my-field' ) );
}
}

View File

@ -234,6 +234,8 @@ final class WooCommerce {
include_once( 'includes/class-wc-post-data.php' ); include_once( 'includes/class-wc-post-data.php' );
include_once( 'includes/class-wc-ajax.php' ); include_once( 'includes/class-wc-ajax.php' );
include_once( 'includes/abstracts/abstract-wc-data.php' ); // WC_Data for CRUD
if ( $this->is_request( 'admin' ) ) { if ( $this->is_request( 'admin' ) ) {
include_once( 'includes/admin/class-wc-admin.php' ); include_once( 'includes/admin/class-wc-admin.php' );
} }
@ -255,7 +257,6 @@ final class WooCommerce {
include_once( 'includes/class-wc-auth.php' ); // Auth Class include_once( 'includes/class-wc-auth.php' ); // Auth Class
include_once( 'includes/class-wc-post-types.php' ); // Registers post types include_once( 'includes/class-wc-post-types.php' ); // Registers post types
include_once( 'includes/abstracts/abstract-wc-data.php' ); // WC_Data for CRUD
include_once( 'includes/abstracts/abstract-wc-payment-token.php' ); // Payment Tokens include_once( 'includes/abstracts/abstract-wc-payment-token.php' ); // Payment Tokens
include_once( 'includes/abstracts/abstract-wc-product.php' ); // Products include_once( 'includes/abstracts/abstract-wc-product.php' ); // Products
include_once( 'includes/abstracts/abstract-wc-order.php' ); // Orders include_once( 'includes/abstracts/abstract-wc-order.php' ); // Orders
@ -328,6 +329,7 @@ final class WooCommerce {
if ( $this->is_request( 'frontend' ) ) { if ( $this->is_request( 'frontend' ) ) {
$this->cart = new WC_Cart(); // Cart class, stores the cart contents $this->cart = new WC_Cart(); // Cart class, stores the cart contents
$this->customer = new WC_Customer(); // Customer class, handles data such as customer location $this->customer = new WC_Customer(); // Customer class, handles data such as customer location
$this->customer->load_session();
} }
$this->load_webhooks(); $this->load_webhooks();