improved structured data - fixes fatal error with grouped products, thanks @leewillis77
This commit is contained in:
commit
6f3974d0e2
|
@ -253,19 +253,21 @@ abstract class WC_Data {
|
|||
$raw_meta_data = $wpdb->get_results( $wpdb->prepare( "
|
||||
SELECT " . $db_info['meta_id_field'] . ", meta_key, meta_value
|
||||
FROM " . $db_info['table'] . "
|
||||
WHERE " . $db_info['object_id_field'] . " = %d ORDER BY " . $db_info['meta_id_field'] . "
|
||||
WHERE " . $db_info['object_id_field'] . "=%d AND meta_key NOT LIKE 'wp\_%%' ORDER BY " . $db_info['meta_id_field'] . "
|
||||
", $this->get_id() ) );
|
||||
|
||||
if ( $raw_meta_data ) {
|
||||
foreach ( $raw_meta_data as $meta ) {
|
||||
if ( in_array( $meta->meta_key, $this->get_internal_meta_keys() ) ) {
|
||||
continue;
|
||||
}
|
||||
$this->_meta_data[] = (object) array(
|
||||
'key' => $meta->meta_key,
|
||||
'value' => $meta->meta_value,
|
||||
'value' => maybe_unserialize( $meta->meta_value ),
|
||||
'meta_id' => $meta->{ $db_info['meta_id_field'] },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $this->_cache_group ) ) {
|
||||
wp_cache_set( $cache_key, $this->_meta_data, $this->_cache_group );
|
||||
|
@ -283,7 +285,8 @@ abstract class WC_Data {
|
|||
$all_meta_ids = array_map( 'absint', $wpdb->get_col( $wpdb->prepare( "
|
||||
SELECT " . $db_info['meta_id_field'] . " FROM " . $db_info['table'] . "
|
||||
WHERE " . $db_info['object_id_field'] . " = %d", $this->get_id() ) . "
|
||||
AND meta_key NOT IN ('" . implode( "','", array_map( 'esc_sql', $this->get_internal_meta_keys() ) ) . "');
|
||||
AND meta_key NOT IN ('" . implode( "','", array_map( 'esc_sql', $this->get_internal_meta_keys() ) ) . "')
|
||||
AND meta_key NOT LIKE 'wp\_%%';
|
||||
" ) );
|
||||
$set_meta_ids = array();
|
||||
|
||||
|
|
|
@ -2203,7 +2203,7 @@ class WC_Admin_Post_Types {
|
|||
foreach ( $existing_permissions as $existing_permission ) {
|
||||
$order = wc_get_order( $existing_permission->order_id );
|
||||
|
||||
if ( ! empty( $order->get_id() ) ) {
|
||||
if ( $order->get_id() ) {
|
||||
// Remove permissions
|
||||
if ( ! empty( $removed_download_ids ) ) {
|
||||
foreach ( $removed_download_ids as $download_id ) {
|
||||
|
|
|
@ -153,7 +153,7 @@ class WC_Meta_Box_Order_Data {
|
|||
$payment_gateways = array();
|
||||
}
|
||||
|
||||
$payment_method = ! empty( $order->get_payment_method() ) ? $order->get_payment_method() : '';
|
||||
$payment_method = $order->get_payment_method() ? $order->get_payment_method() : '';
|
||||
|
||||
$order_type_object = get_post_type_object( $post->post_type );
|
||||
wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
|
||||
|
@ -219,7 +219,7 @@ class WC_Meta_Box_Order_Data {
|
|||
|
||||
<p class="form-field form-field-wide wc-customer-user">
|
||||
<label for="customer_user"><?php _e( 'Customer:', 'woocommerce' ) ?> <?php
|
||||
if ( ! empty( $order->get_user_id() ) ) {
|
||||
if ( $order->get_user_id() ) {
|
||||
$args = array( 'post_status' => 'all',
|
||||
'post_type' => 'shop_order',
|
||||
'_customer_user' => absint( $order->get_user_id() )
|
||||
|
@ -233,7 +233,7 @@ class WC_Meta_Box_Order_Data {
|
|||
<?php
|
||||
$user_string = '';
|
||||
$user_id = '';
|
||||
if ( ! empty( $order->get_user_id() ) ) {
|
||||
if ( $order->get_user_id() ) {
|
||||
$user_id = absint( $order->get_user_id() );
|
||||
$user = get_user_by( 'id', $user_id );
|
||||
$user_string = esc_html( $user->display_name ) . ' (#' . absint( $user->ID ) . ' – ' . esc_html( $user->user_email ) . ')';
|
||||
|
|
|
@ -126,18 +126,18 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
|
||||
$data = array(
|
||||
'id' => $order->get_id(),
|
||||
'parent_id' => $post->post_parent,
|
||||
'parent_id' => $order->get_parent_id(),
|
||||
'status' => $order->get_status(),
|
||||
'order_key' => $order->get_order_key(),
|
||||
'number' => $order->get_order_number(),
|
||||
'currency' => $order->get_currency(),
|
||||
'version' => $order->order_version,
|
||||
'prices_include_tax' => $order->prices_include_tax,
|
||||
'date_created' => wc_rest_prepare_date_response( $post->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt ),
|
||||
'version' => $order->get_version(),
|
||||
'prices_include_tax' => $order->get_prices_include_tax(),
|
||||
'date_created' => wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $order->get_date_created() ) ) ),
|
||||
'date_modified' => wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $order->get_date_modified() ) ) ),
|
||||
'customer_id' => $order->get_user_id(),
|
||||
'discount_total' => wc_format_decimal( $order->get_total_discount(), $dp ),
|
||||
'discount_tax' => wc_format_decimal( $order->cart_discount_tax, $dp ),
|
||||
'discount_tax' => wc_format_decimal( $order->get_discount_tax(), $dp ),
|
||||
'shipping_total' => wc_format_decimal( $order->get_total_shipping(), $dp ),
|
||||
'shipping_tax' => wc_format_decimal( $order->get_shipping_tax(), $dp ),
|
||||
'cart_tax' => wc_format_decimal( $order->get_cart_tax(), $dp ),
|
||||
|
@ -150,9 +150,9 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
'transaction_id' => $order->get_transaction_id(),
|
||||
'customer_ip_address' => $order->get_customer_ip_address(),
|
||||
'customer_user_agent' => $order->get_customer_user_agent(),
|
||||
'created_via' => $order->created_via,
|
||||
'created_via' => $order->get_created_via(),
|
||||
'customer_note' => $order->get_customer_note(),
|
||||
'date_completed' => wc_rest_prepare_date_response( $order->completed_date ),
|
||||
'date_completed' => wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $order->get_date_completed() ) ) ),
|
||||
'date_paid' => $order->get_date_paid(),
|
||||
'cart_hash' => $order->get_cart_hash(),
|
||||
'line_items' => array(),
|
||||
|
@ -377,9 +377,9 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
|
|||
);
|
||||
}
|
||||
|
||||
if ( 0 !== (int) $order->post->post_parent ) {
|
||||
if ( 0 !== (int) $order->get_parent_id() ) {
|
||||
$links['up'] = array(
|
||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $order->post->post_parent ) ),
|
||||
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $order->get_parent_id() ) ),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -122,8 +122,8 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
'individual_use' => $coupon->get_individual_use(),
|
||||
'product_ids' => array_map( 'absint', (array) $coupon->get_product_ids() ),
|
||||
'exclude_product_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),
|
||||
'usage_limit' => ( ! empty( $coupon->get_usage_limit() ) ) ? $coupon->get_usage_limit() : null,
|
||||
'usage_limit_per_user' => ( ! empty( $coupon->get_usage_limit_per_user() ) ) ? $coupon->get_usage_limit_per_user() : null,
|
||||
'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null,
|
||||
'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null,
|
||||
'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(),
|
||||
'usage_count' => (int) $coupon->get_usage_count(),
|
||||
'expiry_date' => $this->server->format_datetime( $coupon->get_expiry_date() ),
|
||||
|
|
|
@ -132,11 +132,11 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
'individual_use' => $coupon->get_individual_use(),
|
||||
'product_ids' => array_map( 'absint', (array) $coupon->get_product_ids() ),
|
||||
'exclude_product_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),
|
||||
'usage_limit' => ( ! empty( $coupon->get_usage_limit() ) ) ? $coupon->get_usage_limit() : null,
|
||||
'usage_limit_per_user' => ( ! empty( $coupon->get_usage_limit_per_user() ) ) ? $coupon->get_usage_limit_per_user() : null,
|
||||
'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null,
|
||||
'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null,
|
||||
'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(),
|
||||
'usage_count' => (int) $coupon->get_usage_count(),
|
||||
'expiry_date' => ( ! empty( $coupon->get_expiry_date() ) ) ? $this->server->format_datetime( $coupon->get_expiry_date() ) : null,
|
||||
'expiry_date' => $coupon->get_expiry_date() ? $this->server->format_datetime( $coupon->get_expiry_date() ) : null,
|
||||
'enable_free_shipping' => $coupon->get_free_shipping(),
|
||||
'product_category_ids' => array_map( 'absint', (array) $coupon->get_product_categories() ),
|
||||
'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_categories() ),
|
||||
|
|
|
@ -132,11 +132,11 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
'individual_use' => $coupon->get_individual_use(),
|
||||
'product_ids' => array_map( 'absint', (array) $coupon->get_product_ids() ),
|
||||
'exclude_product_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),
|
||||
'usage_limit' => ( ! empty( $coupon->get_usage_limit() ) ) ? $coupon->get_usage_limit() : null,
|
||||
'usage_limit_per_user' => ( ! empty( $coupon->get_usage_limit_per_user() ) ) ? $coupon->get_usage_limit_per_user() : null,
|
||||
'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null,
|
||||
'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null,
|
||||
'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(),
|
||||
'usage_count' => (int) $coupon->get_usage_count(),
|
||||
'expiry_date' => ( ! empty( $coupon->get_expiry_date() ) ) ? $this->server->format_datetime( $coupon->get_expiry_date() ) : null,
|
||||
'expiry_date' => $coupon->get_expiry_date() ? $this->server->format_datetime( $coupon->get_expiry_date() ) : null,
|
||||
'enable_free_shipping' => $coupon->get_free_shipping(),
|
||||
'product_category_ids' => array_map( 'absint', (array) $coupon->get_product_categories() ),
|
||||
'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_categories() ),
|
||||
|
|
|
@ -317,8 +317,6 @@ class WC_Checkout {
|
|||
// Save the order
|
||||
$order_id = $order->save();
|
||||
|
||||
$customer = new WC_Customer( $this->customer_id );
|
||||
|
||||
// Update user meta
|
||||
$this->update_customer_data();
|
||||
|
||||
|
@ -818,7 +816,6 @@ class WC_Checkout {
|
|||
return $current_user->user_email;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
switch ( $input ) {
|
||||
|
|
|
@ -34,12 +34,20 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
'password' => '', // write only
|
||||
'date_created' => '', // read only
|
||||
'date_modified' => '', // read only
|
||||
'billing_postcode' => '',
|
||||
'billing_city' => '',
|
||||
'billing_first_name' => '',
|
||||
'billing_last_name' => '',
|
||||
'billing_company' => '',
|
||||
'billing_phone' => '',
|
||||
'billing_email' => '',
|
||||
'billing_address_1' => '',
|
||||
'billing_address_2' => '',
|
||||
'billing_state' => '',
|
||||
'billing_postcode' => '',
|
||||
'billing_city' => '',
|
||||
'billing_country' => '',
|
||||
'shipping_first_name' => '',
|
||||
'shipping_last_name' => '',
|
||||
'shipping_company' => '',
|
||||
'shipping_postcode' => '',
|
||||
'shipping_city' => '',
|
||||
'shipping_address_1' => '',
|
||||
|
@ -59,6 +67,8 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
'billing_postcode', 'billing_city', 'billing_address_1', 'billing_address', 'billing_address_2',
|
||||
'billing_state', 'billing_country', 'shipping_postcode', 'shipping_city', 'shipping_address_1', 'shipping_address',
|
||||
'shipping_address_2', 'shipping_state', 'shipping_country', 'is_vat_exempt', 'calculated_shipping',
|
||||
'billing_first_name', 'billing_last_name', 'billing_company', 'billing_phone', 'billing_email',
|
||||
'shipping_first_name', 'shipping_last_name', 'shipping_company',
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -70,7 +80,11 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
'billing_postcode', 'billing_city', 'billing_address_1', 'billing_address_2', 'billing_state',
|
||||
'billing_country', 'shipping_postcode', 'shipping_city', 'shipping_address_1',
|
||||
'shipping_address_2', 'shipping_state', 'shipping_country', 'paying_customer',
|
||||
'last_update', 'first_name', 'last_name',
|
||||
'last_update', 'first_name', 'last_name', 'show_admin_bar_front',
|
||||
'use_ssl', 'admin_color', 'rich_editing', 'comment_shortcuts', 'dismissed_wp_pointers', 'show_welcome_panel',
|
||||
'_woocommerce_persistent_cart', 'session_tokens',
|
||||
'billing_first_name', 'billing_last_name', 'billing_company', 'billing_phone', 'billing_email',
|
||||
'shipping_first_name', 'shipping_last_name', 'shipping_company',
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -86,37 +100,27 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
protected $_changed = false;
|
||||
|
||||
/**
|
||||
* If some of the customer information is loaded by session (instead of just from the DB).
|
||||
* If this is the customer session, this is true. When true, guest accounts will not be saved to the DB.
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_from_session = false;
|
||||
|
||||
/**
|
||||
* WC_Customer can also return an object for a logged out user (session).
|
||||
* $_is_user will be false in this case. It will be true for all other cases
|
||||
* (logged in users or getting a WC_Customer for another object)
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_is_user = false;
|
||||
protected $_is_session = false;
|
||||
|
||||
/**
|
||||
* Load customer data based on how WC_Customer is called.
|
||||
* @param mixed $customer WC_Customer object or customer ID is accepted.
|
||||
* if $customer is 'new', you can build a new WC_Customer object. If it's empty, some
|
||||
*
|
||||
* If $customer is 'new', you can build a new WC_Customer object. If it's empty, some
|
||||
* data will be pulled from the session for the current user/customer.
|
||||
*
|
||||
* @param int $customer_id Customer ID
|
||||
* @param bool $is_session True if this is the customer session
|
||||
*/
|
||||
public function __construct( $customer = '' ) {
|
||||
if ( $customer instanceof WC_Customer ) {
|
||||
$this->_is_user = true;
|
||||
$this->read( absint( $customer->get_id() ) );
|
||||
} elseif ( is_numeric( $customer ) ) {
|
||||
$this->_is_user = true;
|
||||
$this->read( $customer );
|
||||
} elseif ( empty( $customer ) ) {
|
||||
$this->_is_user = true; // unless load_session gets called after.
|
||||
public function __construct( $customer_id = 0, $is_session = false ) {
|
||||
if ( $customer_id > 0 ) {
|
||||
$this->read( $customer_id );
|
||||
}
|
||||
|
||||
if ( $this->_from_session ) {
|
||||
if ( $is_session ) {
|
||||
$this->_is_session = true;
|
||||
$this->load_session();
|
||||
add_action( 'shutdown', array( $this, 'save_session_if_changed' ), 10 );
|
||||
}
|
||||
}
|
||||
|
@ -135,13 +139,42 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
* Loads a WC session into the customer class.
|
||||
*/
|
||||
public function load_session() {
|
||||
$this->_from_session = true;
|
||||
if ( is_user_logged_in() ) {
|
||||
$this->_is_user = true;
|
||||
$this->read( get_current_user_id() );
|
||||
} else {
|
||||
$this->_is_user = false;
|
||||
$this->read( WC()->session->get_customer_id() );
|
||||
$data = (array) WC()->session->get( 'customer' );
|
||||
if ( ! empty( $data ) ) {
|
||||
foreach ( $this->_session_keys as $session_key ) {
|
||||
$function_key = $session_key;
|
||||
if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
|
||||
$session_key = str_replace( 'billing_', '', $session_key );
|
||||
}
|
||||
if ( ! empty( $data[ $session_key ] ) && is_callable( array( $this, "set_{$function_key}" ) ) ) {
|
||||
$this->{"set_{$function_key}"}( $data[ $session_key ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->load_defaults();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load default values if props are unset.
|
||||
*/
|
||||
protected function load_defaults() {
|
||||
$default = wc_get_customer_default_location();
|
||||
|
||||
// Set some defaults if some of our values are still not set.
|
||||
if ( ! $this->get_billing_country() ) {
|
||||
$this->set_billing_country( $default['country'] );
|
||||
}
|
||||
|
||||
if ( ! $this->get_shipping_country() ) {
|
||||
$this->set_shipping_country( $this->get_billing_country() );
|
||||
}
|
||||
|
||||
if ( ! $this->get_billing_state() ) {
|
||||
$this->set_billing_state( $default['state'] );
|
||||
}
|
||||
|
||||
if ( ! $this->get_shipping_state() ) {
|
||||
$this->set_shipping_state( $this->get_billing_state() );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,9 +186,9 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
*/
|
||||
|
||||
/**
|
||||
* Return a customer's user ID. If the current customer is logged out, this will be a session key.
|
||||
* Return a customer's user ID. Logged out users have ID 0.
|
||||
* @since 2.7.0
|
||||
* @return mixed
|
||||
* @return int
|
||||
*/
|
||||
public function get_id() {
|
||||
return $this->_data['id'];
|
||||
|
@ -278,6 +311,46 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
return absint( $this->_data['date_modified'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets billing first name.
|
||||
* @return string
|
||||
*/
|
||||
public function get_billing_first_name() {
|
||||
return $this->_data['billing_first_name'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets billing last name.
|
||||
* @return string
|
||||
*/
|
||||
public function get_billing_last_name() {
|
||||
return $this->_data['billing_last_name'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets billing company.
|
||||
* @return string
|
||||
*/
|
||||
public function get_billing_company() {
|
||||
return $this->_data['billing_company'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets billing phone.
|
||||
* @return string
|
||||
*/
|
||||
public function get_billing_phone() {
|
||||
return $this->_data['billing_phone'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets billing email.
|
||||
* @return string
|
||||
*/
|
||||
public function get_billing_email() {
|
||||
return $this->_data['billing_email'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets customer postcode.
|
||||
* @return string
|
||||
|
@ -334,6 +407,30 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
return $this->_data['billing_country'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets shipping first name.
|
||||
* @return string
|
||||
*/
|
||||
public function get_shipping_first_name() {
|
||||
return $this->_data['shipping_first_name'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets shipping last name.
|
||||
* @return string
|
||||
*/
|
||||
public function get_shipping_last_name() {
|
||||
return $this->_data['shipping_last_name'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets shipping company.
|
||||
* @return string
|
||||
*/
|
||||
public function get_shipping_company() {
|
||||
return $this->_data['shipping_company'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customer's shipping state.
|
||||
* @return string
|
||||
|
@ -445,7 +542,7 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
*/
|
||||
public function get_downloadable_products() {
|
||||
$downloads = array();
|
||||
if ( $this->_is_user ) {
|
||||
if ( $this->get_id() ) {
|
||||
$downloads = wc_get_customer_available_downloads( $this->get_id() );
|
||||
}
|
||||
return apply_filters( 'woocommerce_customer_get_downloadable_products', $downloads );
|
||||
|
@ -629,6 +726,46 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
$this->_data['billing_city'] = $city;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set billing first name.
|
||||
* @return string
|
||||
*/
|
||||
public function set_billing_first_name( $value ) {
|
||||
$this->_data['billing_first_name'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set billing last name.
|
||||
* @return string
|
||||
*/
|
||||
public function set_billing_last_name( $value ) {
|
||||
$this->_data['billing_last_name'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set billing company.
|
||||
* @return string
|
||||
*/
|
||||
public function set_billing_company( $value ) {
|
||||
$this->_data['billing_company'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set billing phone.
|
||||
* @return string
|
||||
*/
|
||||
public function set_billing_phone( $value ) {
|
||||
$this->_data['billing_phone'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set billing email.
|
||||
* @return string
|
||||
*/
|
||||
public function set_billing_email( $email ) {
|
||||
$this->_data['billing_email'] = sanitize_email( $email );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set customer country.
|
||||
* @param mixed $country
|
||||
|
@ -685,6 +822,30 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
$this->_data['billing_address_2'] = $address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set shipping first name.
|
||||
* @return string
|
||||
*/
|
||||
public function set_shipping_first_name( $value ) {
|
||||
$this->_data['shipping_first_name'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set shipping last name.
|
||||
* @return string
|
||||
*/
|
||||
public function set_shipping_last_name( $value ) {
|
||||
$this->_data['shipping_last_name'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set shipping company.
|
||||
* @return string
|
||||
*/
|
||||
public function set_shipping_company( $value ) {
|
||||
$this->_data['shipping_company'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set shipping country.
|
||||
* @param string $country
|
||||
|
@ -807,15 +968,23 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
*/
|
||||
public function create() {
|
||||
$customer_id = wc_create_new_customer( $this->get_email(), $this->get_username(), $this->_data['password'] );
|
||||
unset( $this->_data['password'] );
|
||||
if ( $customer_id ) {
|
||||
|
||||
if ( ! is_wp_error( $customer_id ) ) {
|
||||
$this->_data['id'] = $customer_id;
|
||||
update_user_meta( $this->get_id(), 'billing_first_name', $this->get_billing_first_name() );
|
||||
update_user_meta( $this->get_id(), 'billing_last_name', $this->get_billing_last_name() );
|
||||
update_user_meta( $this->get_id(), 'billing_company', $this->get_billing_company() );
|
||||
update_user_meta( $this->get_id(), 'billing_phone', $this->get_billing_phone() );
|
||||
update_user_meta( $this->get_id(), 'billing_email', $this->get_billing_email() );
|
||||
update_user_meta( $this->get_id(), 'billing_postcode', $this->get_billing_postcode() );
|
||||
update_user_meta( $this->get_id(), 'billing_city', $this->get_billing_city() );
|
||||
update_user_meta( $this->get_id(), 'billing_address_1', $this->get_billing_address() );
|
||||
update_user_meta( $this->get_id(), 'billing_address_2', $this->get_billing_address_2() );
|
||||
update_user_meta( $this->get_id(), 'billing_state', $this->get_billing_state() );
|
||||
update_user_meta( $this->get_id(), 'billing_country', $this->get_billing_country() );
|
||||
update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() );
|
||||
update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() );
|
||||
update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() );
|
||||
update_user_meta( $this->get_id(), 'shipping_postcode', $this->get_shipping_postcode() );
|
||||
update_user_meta( $this->get_id(), 'shipping_city', $this->get_shipping_city() );
|
||||
update_user_meta( $this->get_id(), 'shipping_address_1', $this->get_shipping_address() );
|
||||
|
@ -823,14 +992,14 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
update_user_meta( $this->get_id(), 'shipping_state', $this->get_shipping_state() );
|
||||
update_user_meta( $this->get_id(), 'shipping_country', $this->get_shipping_country() );
|
||||
update_user_meta( $this->get_id(), 'paying_customer', $this->get_is_paying_customer() );
|
||||
$this->set_date_modified( time() );
|
||||
update_user_meta( $this->get_id(), 'last_update', $this->get_date_modified() );
|
||||
update_user_meta( $this->get_id(), 'first_name', $this->get_first_name() );
|
||||
update_user_meta( $this->get_id(), 'last_name', $this->get_last_name() );
|
||||
$this->set_date_modified( time() );
|
||||
wp_update_user( array( 'ID' => $this->get_id(), 'role' => $this->get_role() ) );
|
||||
$wp_user = new WP_User( $this->get_id() );
|
||||
$this->set_date_created( strtotime( $wp_user->user_registered ) );
|
||||
$this->save_meta_data();
|
||||
$this->read_meta_data();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -841,41 +1010,19 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
*/
|
||||
public function read( $id ) {
|
||||
global $wpdb;
|
||||
$pull_from_db = true;
|
||||
$data = array();
|
||||
if ( $this->_from_session ) {
|
||||
$data = (array) WC()->session->get( 'customer' );
|
||||
if ( ! empty( $data ) ) {
|
||||
$pull_from_db = false;
|
||||
foreach ( $this->_session_keys as $session_key ) {
|
||||
$function_key = $session_key;
|
||||
if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
|
||||
$session_key = str_replace( 'billing_', '', $session_key );
|
||||
}
|
||||
if ( ! empty( $data[ $session_key ] ) && is_callable( array( $this, "set_{$function_key}" ) ) ) {
|
||||
$this->{"set_{$function_key}"}( $data[ $session_key ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $this->_is_user ) {
|
||||
|
||||
if ( $id ) {
|
||||
// Only continue reading if the customer exists.
|
||||
$user_object = get_userdata( $id );
|
||||
if ( empty( $user_object ) || empty ( $user_object->ID ) ) {
|
||||
|
||||
if ( empty( $user_object ) || empty( $user_object->ID ) ) {
|
||||
$this->_data['id'] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( array_keys( $this->_data ) as $key ) {
|
||||
if ( 'billing_' === substr( $key, 0, 8 ) ) {
|
||||
$session_key = str_replace( 'billing_', '', $key );
|
||||
}
|
||||
if ( ! $pull_from_db && ! empty( $data[ $session_key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
$this->_data['id'] = $id;
|
||||
|
||||
foreach ( array_keys( $this->_data ) as $key ) {
|
||||
$meta_value = get_user_meta( $id, $key, true );
|
||||
if ( $meta_value && is_callable( array( $this, "set_{$key}" ) ) ) {
|
||||
$this->{"set_{$key}"}( $meta_value );
|
||||
|
@ -935,31 +1082,9 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
|
||||
$this->set_orders_count( $count );
|
||||
$this->set_total_spent( $spent );
|
||||
|
||||
$this->read_meta_data();
|
||||
}
|
||||
|
||||
$this->_data['id'] = $id;
|
||||
|
||||
$default = wc_get_customer_default_location();
|
||||
|
||||
// Set some defaults if some of our values are still not set.
|
||||
if ( empty( $this->get_billing_country() ) ) {
|
||||
$this->set_billing_country( $default['country'] );
|
||||
}
|
||||
|
||||
if ( empty( $this->get_shipping_country() ) ) {
|
||||
$this->set_shipping_country( $this->get_billing_country() );
|
||||
}
|
||||
|
||||
if ( empty( $this->get_billing_state() ) ) {
|
||||
$this->set_billing_state( $default['state'] );
|
||||
}
|
||||
|
||||
if ( empty( $this->get_shipping_state() ) ) {
|
||||
$this->set_shipping_state( $this->get_billing_state() );
|
||||
}
|
||||
|
||||
unset( $this->_data['password'] ); // password is write only, never ever read it
|
||||
}
|
||||
|
||||
|
@ -977,11 +1102,19 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
unset( $this->_data['password'] );
|
||||
}
|
||||
|
||||
update_user_meta( $this->get_id(), 'billing_first_name', $this->get_billing_first_name() );
|
||||
update_user_meta( $this->get_id(), 'billing_last_name', $this->get_billing_last_name() );
|
||||
update_user_meta( $this->get_id(), 'billing_company', $this->get_billing_company() );
|
||||
update_user_meta( $this->get_id(), 'billing_phone', $this->get_billing_phone() );
|
||||
update_user_meta( $this->get_id(), 'billing_email', $this->get_billing_email() );
|
||||
update_user_meta( $this->get_id(), 'billing_postcode', $this->get_billing_postcode() );
|
||||
update_user_meta( $this->get_id(), 'billing_city', $this->get_billing_city() );
|
||||
update_user_meta( $this->get_id(), 'billing_address_1', $this->get_billing_address() );
|
||||
update_user_meta( $this->get_id(), 'billing_address_2', $this->get_billing_address_2() );
|
||||
update_user_meta( $this->get_id(), 'billing_state', $this->get_billing_state() );
|
||||
update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() );
|
||||
update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() );
|
||||
update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() );
|
||||
update_user_meta( $this->get_id(), 'billing_country', $this->get_billing_country() );
|
||||
update_user_meta( $this->get_id(), 'shipping_postcode', $this->get_shipping_postcode() );
|
||||
update_user_meta( $this->get_id(), 'shipping_city', $this->get_shipping_city() );
|
||||
|
@ -1016,29 +1149,20 @@ class WC_Customer extends WC_Legacy_Customer {
|
|||
* @since 2.7.0
|
||||
*/
|
||||
public function save() {
|
||||
if ( $this->_from_session && ! $this->_is_user ) {
|
||||
if ( $this->_is_session ) {
|
||||
$this->save_session_if_changed();
|
||||
return;
|
||||
}
|
||||
if ( ! $this->_is_user ) {
|
||||
$this->create();
|
||||
} else {
|
||||
if ( ! $this->get_id() ) {
|
||||
} elseif ( ! $this->get_id() ) {
|
||||
$this->create();
|
||||
} else {
|
||||
$this->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves data to the session only (does not overwrite DB values).
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public function save_to_session() {
|
||||
if ( ! $this->_from_session ) {
|
||||
return;
|
||||
}
|
||||
$data = array();
|
||||
foreach ( $this->_session_keys as $session_key ) {
|
||||
$function_key = $session_key;
|
||||
|
|
|
@ -598,7 +598,7 @@ class WC_Form_Handler {
|
|||
// Load the previous order - Stop if the order does not exist
|
||||
$order = wc_get_order( absint( $_GET['order_again'] ) );
|
||||
|
||||
if ( empty( $order->get_id() ) ) {
|
||||
if ( ! $order->get_id() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -230,7 +230,7 @@ class WC_Structured_Data {
|
|||
return false;
|
||||
}
|
||||
|
||||
$variations = count( $product->get_children() ) > 1 ? $product->get_available_variations() : array( $product );
|
||||
$variations = $product->is_type( 'variable' ) ? $product->get_available_variations() : array( $product );
|
||||
|
||||
foreach ( $variations as $variation ) {
|
||||
$product_variation = count( $variations ) > 1 ? wc_get_product( $variation['variation_id'] ) : $variation;
|
||||
|
|
|
@ -275,7 +275,7 @@ function wc_customer_has_capability( $allcaps, $caps, $args ) {
|
|||
}
|
||||
|
||||
$order = wc_get_order( $order_id );
|
||||
if ( $user_id == $order->get_user_id() || empty( $order->get_user_id() ) ) {
|
||||
if ( $user_id == $order->get_user_id() || ! $order->get_user_id() ) {
|
||||
$allcaps['pay_for_order'] = true;
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
Contributors: automattic, mikejolley, jameskoster, claudiosanches, jshreve, coderkevin, woothemes, BFTrick, iCaleb
|
||||
Tags: ecommerce, e-commerce, store, sales, sell, shop, cart, checkout, downloadable, downloads, paypal, storefront
|
||||
Requires at least: 4.4
|
||||
Tested up to: 4.5
|
||||
Tested up to: 4.6
|
||||
Stable tag: 2.6.3
|
||||
License: GPLv3
|
||||
License URI: https://www.gnu.org/licenses/gpl-3.0.html
|
||||
|
|
|
@ -406,8 +406,6 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
|
|||
$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() );
|
||||
|
@ -415,17 +413,17 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
|
|||
$session->set_billing_address( '124 South Street' );
|
||||
$session->save_to_session();
|
||||
|
||||
$session = new \WC_Customer();
|
||||
$session = new \WC_Customer( 0, true );
|
||||
$session->load_session();
|
||||
$this->assertEquals( '124 South Street', $session->get_billing_address() );
|
||||
|
||||
$session = new \WC_Customer();
|
||||
$session = new \WC_Customer( 0, true );
|
||||
$session->load_session();
|
||||
$session->set_billing_postcode( '32191' );
|
||||
$session->save();
|
||||
|
||||
// should still be session ID, not a created row, since we are working with guests/sessions
|
||||
$this->assertFalse( is_numeric( $session->get_id() ) );
|
||||
$this->assertFalse( $session->get_id() > 0 );
|
||||
$this->assertEquals( '32191' , $session->get_billing_postcode() );
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
* Author: WooThemes
|
||||
* Author URI: https://woothemes.com
|
||||
* Requires at least: 4.4
|
||||
* Tested up to: 4.5
|
||||
* Tested up to: 4.6
|
||||
*
|
||||
* Text Domain: woocommerce
|
||||
* Domain Path: /i18n/languages/
|
||||
|
@ -342,9 +342,8 @@ final class WooCommerce {
|
|||
// Classes/actions loaded for the frontend and for ajax requests.
|
||||
if ( $this->is_request( 'frontend' ) ) {
|
||||
$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( get_current_user_id(), true ); // Customer class, handles data such as customer location
|
||||
$this->structured_data = new WC_Structured_Data(); // Structured Data class, generates and handles structured data
|
||||
$this->customer->load_session();
|
||||
}
|
||||
|
||||
$this->load_webhooks();
|
||||
|
|
Loading…
Reference in New Issue