WC_Product::set_downloads
Set downloads.
Method of the class: WC_Product{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WC_Product = new WC_Product(); $WC_Product->set_downloads( $downloads_array );
- $downloads_array(array) (required)
- Array of WC_Product_Download objects or arrays.
Changelog
| Since 3.0.0 | Introduced. |
WC_Product::set_downloads() WC Product::set downloads code WC 10.7.0
public function set_downloads( $downloads_array ) {
// When the object is first hydrated, only the previously persisted downloads will be passed in.
$existing_downloads = $this->get_object_read() ? (array) $this->get_prop( 'downloads' ) : $downloads_array;
$downloads = array();
$errors = array();
$downloads_array = $this->build_downloads_map( $downloads_array );
$existing_downloads = $this->build_downloads_map( $existing_downloads );
foreach ( $downloads_array as $download ) {
$download_id = $download->get_id();
$is_new = ! isset( $existing_downloads[ $download_id ] );
$has_changed = ! $is_new && $existing_downloads[ $download_id ]->get_file() !== $downloads_array[ $download_id ]->get_file();
try {
$download->check_is_valid( $this->get_object_read() );
$downloads[ $download_id ] = $download;
} catch ( Exception $e ) {
// We only add error messages for newly added downloads (let's not overwhelm the user if there are
// multiple existing files which are problematic).
if ( $is_new || $has_changed ) {
$errors[] = $e->getMessage();
}
// If the problem is with an existing download, disable it.
if ( ! $is_new ) {
$download->set_enabled( false );
$downloads[ $download_id ] = $download;
}
}
}
$this->set_prop( 'downloads', $downloads );
if ( $errors && $this->get_object_read() ) {
$this->error( 'product_invalid_download', $errors[0] );
}
}