WC_Customer::__construct( WC_Customer|int $data, bool $is_session = false )

Load customer data based on how WC_Customer is called.


Description Description

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.


Parameters Parameters

$data

(Required) Customer ID or data.

$is_session

(Optional) True if this is the customer session.

Default value: false


Top ↑

Source Source

File: includes/class-wc-customer.php

	public function __construct( $data = 0, $is_session = false ) {
		parent::__construct( $data );

		if ( $data instanceof WC_Customer ) {
			$this->set_id( absint( $data->get_id() ) );
		} elseif ( is_numeric( $data ) ) {
			$this->set_id( $data );
		}

		$this->data_store = WC_Data_Store::load( 'customer' );

		// If we have an ID, load the user from the DB.
		if ( $this->get_id() ) {
			try {
				$this->data_store->read( $this );
			} catch ( Exception $e ) {
				$this->set_id( 0 );
				$this->set_object_read( true );
			}
		} else {
			$this->set_object_read( true );
		}

		// If this is a session, set or change the data store to sessions. Changes do not persist in the database.
		if ( $is_session ) {
			$this->data_store = WC_Data_Store::load( 'customer-session' );
			$this->data_store->read( $this );
		}
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.