WC_Product::set_downloads PHP Method

set_downloads() public method

Set downloads.
Since: 2.7.0
public set_downloads ( $downloads_array )
$downloads_array array of WC_Product_Download objects or arrays.
    public function set_downloads($downloads_array)
    {
        $downloads = array();
        $errors = array();
        foreach ($downloads_array as $download) {
            if (is_a($download, 'WC_Product_Download')) {
                $download_object = $download;
            } else {
                $download_object = new WC_Product_Download();
                $download_object->set_id(md5($download['file']));
                $download_object->set_name($download['name']);
                $download_object->set_file($download['file']);
                if (isset($download['previous_hash'])) {
                    $download_object->set_previous_hash($download['previous_hash']);
                }
            }
            // Validate the file extension
            if (!$download_object->is_allowed_filetype()) {
                $errors[] = sprintf(__('The downloadable file %1$s cannot be used as it does not have an allowed file type. Allowed types include: %2$s', 'woocommerce'), '<code>' . basename($download_object->get_file()) . '</code>', '<code>' . implode(', ', array_keys($download_object->get_allowed_mime_types())) . '</code>');
                continue;
            }
            // Validate the file exists.
            if (!$download_object->file_exists()) {
                $errors[] = sprintf(__('The downloadable file %s cannot be used as it does not exist on the server.', 'woocommerce'), '<code>' . $download_object->get_file() . '</code>');
                continue;
            }
            $downloads[$download_object->get_id()] = $download_object;
        }
        if ($errors) {
            $this->error('product_invalid_download', $errors[0]);
        }
        $this->set_prop('downloads', $downloads);
    }
WC_Product