' . __( 'Pricing rules use your user role, the contents of your cart and the date to work out prices. This happens on this website and isn’t stored.', 'joinery-core' ) . '
' .
'' . __( 'If you receive store credit, we keep a record of each credit and use of credit (amount, reason, related order and expiry date) linked to your account, for as long as you have a balance and as long as tax and accounting law requires.', 'joinery-core' ) . '
'
)
);
}
/**
* Register the exporter.
*
* @param array $exporters Exporters.
* @return array
*/
public function add_exporter( array $exporters ): array {
$exporters['joinery-wallet'] = array(
'exporter_friendly_name' => __( 'Joinery store credit', 'joinery-core' ),
'callback' => array( $this, 'export' ),
);
return $exporters;
}
/**
* Register the eraser.
*
* @param array $erasers Erasers.
* @return array
*/
public function add_eraser( array $erasers ): array {
$erasers['joinery-wallet'] = array(
'eraser_friendly_name' => __( 'Joinery store credit', 'joinery-core' ),
'callback' => array( $this, 'erase' ),
);
return $erasers;
}
/**
* Export one page of a customer's wallet entries.
*
* @param string $email Email address of the request.
* @param int $page Page number, from 1.
* @return array{data: array, done: bool}
*/
public function export( string $email, int $page = 1 ): array {
$user = get_user_by( 'email', $email );
if ( ! $user ) {
return array(
'data' => array(),
'done' => true,
);
}
$entries = $this->wallet->entries( $user->ID, self::PAGE_SIZE, ( max( 1, $page ) - 1 ) * self::PAGE_SIZE );
$data = array();
foreach ( $entries as $entry ) {
$data[] = array(
'group_id' => 'joinery-wallet',
'group_label' => __( 'Store credit', 'joinery-core' ),
'item_id' => 'joinery-wallet-' . $entry['id'],
'data' => array(
array(
'name' => __( 'Date', 'joinery-core' ),
'value' => (string) $entry['created_at'],
),
array(
'name' => __( 'Amount', 'joinery-core' ),
'value' => $entry['amount'] . ' ' . $entry['currency'],
),
array(
'name' => __( 'Reason', 'joinery-core' ),
'value' => (string) $entry['source'],
),
array(
'name' => __( 'Order', 'joinery-core' ),
'value' => (string) ( $entry['order_id'] ?? '' ),
),
array(
'name' => __( 'Note', 'joinery-core' ),
'value' => (string) $entry['note'],
),
array(
'name' => __( 'Expires', 'joinery-core' ),
'value' => (string) ( $entry['expires_at'] ?? '' ),
),
),
);
}
return array(
'data' => $data,
'done' => count( $entries ) < self::PAGE_SIZE,
);
}
/**
* Erase a customer's wallet entries, unless they still have credit.
*
* @param string $email Email address of the request.
* @return array{items_removed: bool, items_retained: bool, messages: string[], done: bool}
*/
public function erase( string $email ): array {
$result = array(
'items_removed' => false,
'items_retained' => false,
'messages' => array(),
'done' => true,
);
$user = get_user_by( 'email', $email );
if ( ! $user ) {
return $result;
}
if ( abs( $this->wallet->balance( $user->ID ) ) > 0.00001 ) {
$result['items_retained'] = true;
$result['messages'][] = __( 'Store credit records were kept because the customer still has a balance. Refund or expire the balance, then erase again.', 'joinery-core' );
return $result;
}
$result['items_removed'] = $this->wallet->delete_customer( $user->ID ) > 0;
return $result;
}
}