/** * Get all customer downloads. * * @param WP_REST_Request $request * @return array */ public function get_items($request) { $downloads = wc_get_customer_available_downloads((int) $request['customer_id']); $data = array(); foreach ($downloads as $download_data) { $download = $this->prepare_item_for_response((object) $download_data, $request); $download = $this->prepare_response_for_collection($download); $data[] = $download; } return rest_ensure_response($data); }
/** * Get the available downloads for a customer * * @since 2.2 * @param int $id the customer ID * @param string $fields fields to include in response * @return array */ public function get_customer_downloads($id, $fields = null) { $id = $this->validate_request($id, 'customer', 'read'); if (is_wp_error($id)) { return $id; } $downloads = array(); $_downloads = wc_get_customer_available_downloads($id); foreach ($_downloads as $key => $download) { $downloads[$key] = $download; $downloads[$key]['access_expires'] = $this->server->format_datetime($downloads[$key]['access_expires']); } return array('downloads' => apply_filters('woocommerce_api_customer_downloads_response', $downloads, $id, $fields, $this->server)); }
/** * Gets a customer's downloadable products. * @return array Array of downloadable products */ public function get_downloadable_products() { $downloads = array(); if ($this->get_id()) { $downloads = wc_get_customer_available_downloads($this->get_id()); } return apply_filters('woocommerce_customer_get_downloadable_products', $downloads); }
/** * Get the available downloads for a customer * * @since 2.2 * @param int $id the customer ID * @param string $fields fields to include in response * @return array */ public function get_customer_downloads($id, $fields = null) { $id = $this->validate_request($id, 'customer', 'read'); if (is_wp_error($id)) { return $id; } $downloads = wc_get_customer_available_downloads($id); if (empty($downloads)) { return array('downloads' => array()); } return array('downloads' => apply_filters('woocommerce_api_customer_downloads_response', $downloads, $id, $fields, $this->server)); }
/** * Gets a user's downloadable products if they are logged in. * * @return array Array of downloadable products */ public function get_downloadable_products() { $downloads = array(); if (is_user_logged_in()) { $downloads = wc_get_customer_available_downloads(get_current_user_id()); } return apply_filters('woocommerce_customer_get_downloadable_products', $downloads); }
/** * Test getting a customer's downloadable products. * @since 2.7.0 */ public function test_customer_get_downloadable_products() { $customer = WC_Helper_Customer::create_customer(); $customer_id = $customer->get_id(); $this->assertEquals(wc_get_customer_available_downloads($customer_id), $customer->get_downloadable_products()); }
/** * View customer downloads. * * ## OPTIONS * * <customer> * : The customer ID, email or username. * * [--field=<field>] * : Instead of returning the whole customer fields, returns the value of a single fields. * * [--fields=<fields>] * : Get a specific subset of the customer's fields. * * [--format=<format>] * : Accepted values: table, json, csv. Default: table. * * ## AVAILABLE FIELDS * * * download_id * * download_name * * access_expires * * ## EXAMPLES * * wp wc customer downloads 123 * * @since 2.5.0 */ public function downloads($args, $assoc_args) { try { $user = $this->get_user($args[0]); $downloads = array(); foreach (wc_get_customer_available_downloads($user['id']) as $key => $download) { $downloads[$key] = $download; $downloads[$key]['access_expires'] = $this->format_datetime($download['access_expires']); } $downloads = apply_filters('woocommerce_cli_customer_downloads', $downloads, $user, $assoc_args); if (empty($assoc_args['fields'])) { $assoc_args['fields'] = $this->get_customer_download_fields(); } $formatter = $this->get_formatter($assoc_args); $formatter->display_items($downloads); } catch (WC_CLI_Exception $e) { WP_CLI::error($e->getMessage()); } }