Inhoudsopgave
The EDD_Customer class is used for getting customer data, creating new customer entries, and editing existing customer entries. The values that may be managed with this class are:
- user_id
- name
- payment_ids
- purchase_value
- purchase_count
- notes
Getting a Customer
Your reasons for getting Customer data can vary from simply rendering the info to using it for stats or logging or anything else. Here are the steps to get and render customer information.
Step 1, Get the data
In order to create a Customer object you must pass some sort of identifier when instantiating the class. This can be email address, Customer ID, or WordPress user ID.
Examples:
Email
$customer = new EDD_Customer( '[email protected]' );
Customer ID
$customer = new EDD_Customer( 33 );
WordPress user ID
$customer = new EDD_Customer( 96, true );
Note: for the WordPress user ID you must pass a value of true as the second parameter, so that it knows to look at WordPress rather than EDD.
Step 2, Use the data
Now you can access properties of the customer object like this:
- $customer->name;
- $customer->email;
- $customer->date_created;
- $customer->purchase_count;
- $customer->purchase_value;
- $customer->id; // Klant-ID
- $customer->user_id; // WordPress gebruikers-ID
- $customer->payment_ids;
Je kunt deze echoën, parsen; wat je maar wilt.
Getting Customer Payment Data
De EDD_Customer klasse heeft een methode genaamd
get_payments(). Deze methode retourneert een array van EDD_Payment objecten. Voorbeeld:
$customer = new EDD_Customer( 33 ); $payments = $customer->get_payments();
Nu bevat $payments een array van EDD_Payment objecten. Voor meer informatie over hoe die array eruitziet,
zie de documentatie over de EDD_Payment klasse.
Creating a Customer
Instantieer eerst een object:
$customer = new EDD_Customer;
Voorbeeld 1:
$args = array( 'user_id' => 12 'name' => 'Jose Canseco', 'email' => '[email protected]', 'payment_ids' => '', 'purchase_value' => '', 'purchase_count' => '', 'notes' => 'Whatever note you'd like', );
Opmerking: de
user_id hierboven is een WordPress gebruikers-ID nummer. Het is alleen vereist als je je Klant wilt koppelen aan een WordPress gebruiker. Zo niet, verwijder het dan uit de array.
$customer->create( $args );
vereiste optie is e-mail, dus dit zou ook werken:
$args = array( 'email' => '[email protected]', );
$customer->create( $args );
Dit maakt een klant aan met alleen het opgegeven e-mailadres, wat prima werkt.
Updating a Customer
Stap 1, Verkrijg de gegevens.
Gebruik de voorbeeldcode uit "Een Klant Verkrijgen" hierboven.
Stap 2, Werk de gegevens bij
Er zijn twee manieren om klantgegevens bij te werken met EDD_Customer. Eén is het gebruik van de
update() methode en het bijwerken van meerdere velden tegelijk.
Voorbeeld:
$customer = new EDD_Customer( 33 ); $update_args = array( 'email' => '[email protected]', 'name' => 'Jose Canseco', );
Nu heeft klant 33 een bijgewerkt e-mailadres en een nieuwe notitie.
Opmerking: het bijwerken van het notitieveld met deze methode zal alle bestaande notities wissen. Gebruik de hieronder genoemde add_note() methode om een nieuwe notitie toe te voegen.
Updating Specific Fields
In plaats van de algemene update() methode hierboven te gebruiken, moet je over het algemeen proberen een van de onderstaande helpermethoden te gebruiken. Deze bieden specifieke beperkte functionaliteit die kan helpen fouten te verminderen.
Voor elk van de volgende methoden zou je eerst de klantklasse instantiëren zoals hierboven:
$customer = new EDD_Customer( 33 );
attach_payment() en remove_payment()
Deze worden gebruikt voor het koppelen en ontkoppelen van betalingen aan en van een klant. Ze nemen beide een payment_id als eerste variabele, en een boolean optie als tweede. De tweede zal statistieken bijwerken als deze op true is ingesteld.
Examples:
$customer->attach_payment( 33, true );
$customer->remove_payment( 33, true );
increase_purchase_count() en decrease_purchase_count()
These are used to increase and decrease the
number of purchases made by a single customer. They both simply take an integer, changing the stored value by that much.
Examples:
$customer->increase_purchase_count( 1 );
$customer->decrease_purchase_count( 1 );
increase_value() and decrease_value()
These are used to increase and decrease the amount spent by a single customer. They both simply take a decimal number, to two places, changing the stored value by that much.
Examples:
$customer->increase_value( 14.98 );
$customer->decrease_value( 3.00 );
get_notes()
This method returns notes left on the customer’s account. It accepts 2 variables, the first for how many notes you want, and the second is an offset.
Examples:
$customer->get_notes( 5 ); // gets the first 5 notes
$customer->decrease_value( 5, 6 ); // gets 5 notes starting at position 6, so notes 6-10
get_notes_count()
This method returns the number of notes on the customer’s account. It takes no input.
Voorbeeld:
$customer->get_notes_count();
add_note()
This method accepts a string as input, and stores it as a new note on the customer’s account. It preserves hard returns, but does not allow HTML.
Voorbeeld:
$customer->add_note( 'This is a note. Isn't that cool?' );
Customer Meta
Arbitrary customer meta may be managed using the meta functions built into the EDD_Customer class.
get_meta( $meta_key = ”, $single = true )
get_meta simply takes a meta key and returns the value. If single is set to false it will always return an array.
add_meta( $meta_key = ”, $meta_value, $unique = false )
add_meta accepts a meta_key and its value, but it also accepts a boolean for unique. If the third input is true then add_meta will only succeed if the meta_key does not previously exist. Returns false for failure. True for success.
update_meta( $meta_key = ”, $meta_value, $prev_value = ” )
update_meta accepts a meta_key and the new value you wish to set for it. If you have multiple keys of the same name they will all be updated with the new value. The third input accepts a value to compare the previous value against. So if you already have several meta_keys of
foo but only one with a value of bar, you could make bar the third input and the update would only occur on that one. Returns false for failure. True for success.
delete_meta( $meta_key = ”, $meta_value = ” )
delete_meta will delete all meta data with a matching key. If a value is also passed then both must match. Returns false for failure. True for success.
Customer Email Addresses
Each customer can have multiple email addresses, allowing them to purchase from more than one address, or purchase from one and get support from another. Each customer will also have a primary email address, which gets used as a default for the customer. In the Customer Management tab the UI looks like this:
The EDD_Customer class allows the developer to removes addresses from a customer and add addressed to a customer as well as make a specific address primary.
add_email( $email = ”, $primary = false )
Deze functie accepteert een e-mailadres dat moet worden toegevoegd aan de klant. Als de tweede invoer waar is, wordt het nieuwe e-mailadres ingesteld als het primaire adres voor de klant. Geeft false terug voor een storing. True voor succes.
set_primary_email( $new_primary_email = ” )
Deze functie accepteert een e-mailadres en als het e-mailadres al aan deze klant is gekoppeld, wordt het het primaire e-mailadres voor die klant. Geeft false terug voor een storing. True voor succes.
remove_email( $email = ” )
Deze functie accepteert een e-mailadres en als het e-mailadres al aan deze klant is gekoppeld, wordt het van die klant verwijderd. Geeft false terug voor een storing. True voor succes.
