WC_REST_Products_Catalog_Controller::get_catalog_file_infoprivateWC 1.0

Get catalog file information based on fields.

Method of the class: WC_REST_Products_Catalog_Controller{}

No Hooks.

Returns

Array|WP_Error. Array with 'filepath', 'url', and 'directory' keys, or WP_Error on failure.

Usage

// private - for code of main (parent) class only
$result = $this->get_catalog_file_info( $fields );
$fields(array) (required)
Product/variation fields to include in the catalog.

WC_REST_Products_Catalog_Controller::get_catalog_file_info() code WC 10.4.3

private function get_catalog_file_info( $fields ) {
	$upload_dir = wp_upload_dir();

	if ( ! empty( $upload_dir['error'] ) ) {
		return new WP_Error( 'upload_dir_error', $upload_dir['error'], array( 'status' => 500 ) );
	}

	$catalog_dir = trailingslashit( $upload_dir['basedir'] ) . 'wc-catalog/';
	$catalog_url = trailingslashit( $upload_dir['baseurl'] ) . 'wc-catalog/';

	$today        = gmdate( 'Y-m-d' );
	$catalog_hash = wp_hash( $today . wp_json_encode( $fields ) );
	$filename     = "products-{$today}-{$catalog_hash}.json";

	return array(
		'filepath'  => $catalog_dir . $filename,
		'url'       => $catalog_url . $filename,
		'directory' => $catalog_dir,
	);
}