mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
* fix: Product Attribute naming conventions changed * devops: dev dependencies updated * devops: dev dependencies updated * devops: dev dependencies updated * devops: Dockerfile updated * devops: dev dependencies updated * devops: dev dependencies updated * devops: dev dependencies updated * devops: Dockerfile updated * chore: Docker composer configurations updated * chore: Coding standards met.
134 lines
4.5 KiB
PHP
134 lines
4.5 KiB
PHP
<?php
|
|
/**
|
|
* Model - Customer
|
|
*
|
|
* Models WooCommerce post-type data
|
|
*
|
|
* @package WPGraphQL\WooCommerce\Model
|
|
* @since 0.0.1
|
|
*/
|
|
|
|
namespace WPGraphQL\WooCommerce\Model;
|
|
|
|
use GraphQLRelay\Relay;
|
|
use WPGraphQL\Model\Model;
|
|
use WC_Customer;
|
|
|
|
/**
|
|
* Class Customer
|
|
*/
|
|
class Customer extends Model {
|
|
|
|
/**
|
|
* Customer constructor
|
|
*
|
|
* @param WC_Customer|int $id - User ID.
|
|
*/
|
|
public function __construct( $id = 'session' ) {
|
|
$this->data = 'session' === $id ? \WC()->customer : new WC_Customer( $id );
|
|
$allowed_restricted_fields = array(
|
|
'isRestricted',
|
|
'isPrivate',
|
|
'isPublic',
|
|
'id',
|
|
'customerId',
|
|
);
|
|
|
|
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
|
|
$restricted_cap = apply_filters( 'customer_restricted_cap', 'session' === $id ? '' : 'list_users' );
|
|
|
|
parent::__construct( $restricted_cap, $allowed_restricted_fields, $id );
|
|
}
|
|
|
|
/**
|
|
* Forwards function calls to WC_Data sub-class instance.
|
|
*
|
|
* @param string $method - function name.
|
|
* @param array $args - function call arguments.
|
|
* @return mixed
|
|
*/
|
|
public function __call( $method, $args ) {
|
|
return $this->data->$method( ...$args );
|
|
}
|
|
|
|
/**
|
|
* Initializes the Customer field resolvers.
|
|
*/
|
|
protected function init() {
|
|
if ( empty( $this->fields ) ) {
|
|
$this->fields = array(
|
|
'ID' => function() {
|
|
return ( ! empty( $this->data->get_id() ) ) ? $this->data->get_id() : \WC()->session->_customer_id;
|
|
},
|
|
'id' => function() {
|
|
return ( ! empty( $this->data->get_id() ) )
|
|
? Relay::toGlobalId( 'customer', $this->data->get_id() )
|
|
: 'guest';
|
|
},
|
|
'databaseId' => function() {
|
|
return $this->ID;
|
|
},
|
|
'isVatExempt' => function() {
|
|
return ! is_null( $this->data->get_is_vat_exempt() ) ? $this->data->get_is_vat_exempt() : null;
|
|
},
|
|
'hasCalculatedShipping' => function() {
|
|
return ! is_null( $this->data->has_calculated_shipping() ) ? $this->data->has_calculated_shipping() : null;
|
|
},
|
|
'calculatedShipping' => function() {
|
|
return ! is_null( $this->data->get_calculated_shipping() ) ? $this->data->get_calculated_shipping() : null;
|
|
},
|
|
'orderCount' => function() {
|
|
return ! is_null( $this->data->get_order_count() ) ? $this->data->get_order_count() : null;
|
|
},
|
|
'totalSpent' => function() {
|
|
return ! is_null( $this->data->get_total_spent() ) ? $this->data->get_total_spent() : null;
|
|
},
|
|
'username' => function() {
|
|
return ( ! empty( $this->data->get_username() ) ) ? $this->data->get_username() : null;
|
|
},
|
|
'email' => function() {
|
|
return ( ! empty( $this->data->get_email() ) ) ? $this->data->get_email() : null;
|
|
},
|
|
'firstName' => function() {
|
|
return ( ! empty( $this->data->get_first_name() ) ) ? $this->data->get_first_name() : null;
|
|
},
|
|
'lastName' => function() {
|
|
return ( ! empty( $this->data->get_last_name() ) ) ? $this->data->get_last_name() : null;
|
|
},
|
|
'displayName' => function() {
|
|
return ( ! empty( $this->data->get_display_name() ) ) ? $this->data->get_display_name() : null;
|
|
},
|
|
'role' => function() {
|
|
return ( ! empty( $this->data->get_role() ) ) ? $this->data->get_role() : null;
|
|
},
|
|
'date' => function() {
|
|
return ( ! empty( $this->data->get_date_created() ) ) ? $this->data->get_date_created() : null;
|
|
},
|
|
'modified' => function() {
|
|
return ( ! empty( $this->data->get_date_modified() ) ) ? $this->data->get_date_modified() : null;
|
|
},
|
|
'billing' => function() {
|
|
return ( ! empty( $this->data->get_billing() ) ) ? $this->data->get_billing() : null;
|
|
},
|
|
'shipping' => function() {
|
|
return ( ! empty( $this->data->get_shipping() ) ) ? $this->data->get_shipping() : null;
|
|
},
|
|
'isPayingCustomer' => function() {
|
|
return ( ! is_null( $this->data->get_is_paying_customer() ) ) ? $this->data->get_is_paying_customer() : null;
|
|
},
|
|
/**
|
|
* Connection resolvers fields
|
|
*
|
|
* These field resolvers are used in connection resolvers to define WP_Query argument
|
|
* Note: underscore naming style is used as a quick identifier
|
|
*/
|
|
'last_order_id' => function() {
|
|
return ( ! empty( $this->data->get_last_order() ) ) ? $this->data->get_last_order()->get_id() : null;
|
|
},
|
|
);
|
|
}//end if
|
|
|
|
parent::prepare_fields();
|
|
}
|
|
}
|