/** * shopp_add_product_download * * Add product download file to a product/variation. * * @api * @since 1.2 * * @param int $product id of the product the download asset will be added to * @param string $file full or correct relative path to the download file asset. * @param int $variant id of the variant the download asset will be attached to. For products with variants, this is a required parameter. * @return mixed false of failure, the new download asset id on success **/ function shopp_add_product_download($product, $file, $variant = false) { if (empty($product) || empty($file)) { shopp_debug(__FUNCTION__ . ' failed: One or more missing parameters.'); return false; } $File = new ProductDownload(); $instore = $File->found($file); if (!$instore && (!is_file($file) || !is_readable($file))) { shopp_debug(__FUNCTION__ . " failed for file {$file}: File missing or unreadable."); return false; } $Product = new ShoppProduct($product); if (empty($Product->id)) { shopp_debug(__FUNCTION__ . " failed for file {$file}: No such product with id {$product}."); return false; } $Product->load_data(array('summary', 'prices')); if ("on" == $Product->variants && false === $variant) { shopp_debug(__FUNCTION__ . " failed for file {$file}: You must specify the variant id parameter for product {$product}."); return false; } $Price = reset($Product->prices); if (empty($Price->id)) { shopp_debug(__FUNCTION__ . " failed for file {$file}: Failed to load product variants."); return false; } if ($variant) { $Price = false; foreach ($Product->prices as $Price) { if ($variant == $Price->id) { break; } } if (false === $Price) { shopp_debug(__FUNCTION__ . " failed for file {$file}: You must specify a valid variant id parameter for product {$product}."); return false; } } // Save the uploaded file $File->load(array('type' => 'download', 'parent' => $Price->id)); $File->parent = $Price->id; $File->context = "price"; $File->type = "download"; $File->name = basename($file); $File->filename = $File->name; if (!$instore) { $File->mime = file_mimetype($file, $File->name); $File->size = filesize($file); $File->store($file, 'file'); } else { $File->uri = $file; $File->readmeta(); } $File->save(); if ($File->id) { return $File->id; } shopp_debug(__FUNCTION__ . " failed for file {$file}"); return false; }