从 WooCommerce 可下载产品访问可下载数据
Access downloadable data from WooCommerce downloadable products
我正在尝试使用 $product = new WC_Product( get_the_ID() );
获取 WooCommerce 产品元数据我正在获取产品价格以及产品的所有其他值都是可下载的 WooCommerce 产品,我想获取以下数据:
每当我尝试获取 $product->downloads->id
或 $product->downloads->file
时,我都会在 return 中获取空值。请告诉我我在这里做错了什么。
如果您要下载 link,请尝试:
$downloads = $product->get_downloads();
这应该 return 一个数组,因此您可以像这样使用它:
foreach( $downloads as $key => $download ) {
echo '<a href="' . $download["file"] . '">Download File</a>';
}
要访问可下载产品的所有产品下载,您将使用 WC_Product
get_downloads()
method。
它将为您提供一组 WC_Product_Download
个对象,这些对象的受保护属性可通过 WC_Product_Download
available methods (自 WooCommerce 3 起):
// Optional - Get the WC_Product object from the product ID
$product = wc_get_product( $product_id );
$output = []; // Initializing
if ( $product->is_downloadable() ) {
// Loop through WC_Product_Download objects
foreach( $product->get_downloads() as $key_download_id => $download ) {
## Using WC_Product_Download methods (since WooCommerce 3)
$download_name = $download->get_name(); // File label name
$download_link = $download->get_file(); // File Url
$download_id = $download->get_id(); // File Id (same as $key_download_id)
$download_type = $download->get_file_type(); // File type
$download_ext = $download->get_file_extension(); // File extension
## Using array properties (backward compatibility with previous WooCommerce versions)
// $download_name = $download['name']; // File label name
// $download_link = $download['file']; // File Url
// $download_id = $download['id']; // File Id (same as $key_download_id)
$output[$download_id] = '<a href="'.$download_link.'">'.$download_name.'</a>';
}
// Output example
echo implode('<br>', $output);
}
相关回答:
我正在尝试使用 $product = new WC_Product( get_the_ID() );
获取 WooCommerce 产品元数据我正在获取产品价格以及产品的所有其他值都是可下载的 WooCommerce 产品,我想获取以下数据:
每当我尝试获取 $product->downloads->id
或 $product->downloads->file
时,我都会在 return 中获取空值。请告诉我我在这里做错了什么。
如果您要下载 link,请尝试:
$downloads = $product->get_downloads();
这应该 return 一个数组,因此您可以像这样使用它:
foreach( $downloads as $key => $download ) {
echo '<a href="' . $download["file"] . '">Download File</a>';
}
要访问可下载产品的所有产品下载,您将使用 WC_Product
get_downloads()
method。
它将为您提供一组 WC_Product_Download
个对象,这些对象的受保护属性可通过 WC_Product_Download
available methods (自 WooCommerce 3 起):
// Optional - Get the WC_Product object from the product ID
$product = wc_get_product( $product_id );
$output = []; // Initializing
if ( $product->is_downloadable() ) {
// Loop through WC_Product_Download objects
foreach( $product->get_downloads() as $key_download_id => $download ) {
## Using WC_Product_Download methods (since WooCommerce 3)
$download_name = $download->get_name(); // File label name
$download_link = $download->get_file(); // File Url
$download_id = $download->get_id(); // File Id (same as $key_download_id)
$download_type = $download->get_file_type(); // File type
$download_ext = $download->get_file_extension(); // File extension
## Using array properties (backward compatibility with previous WooCommerce versions)
// $download_name = $download['name']; // File label name
// $download_link = $download['file']; // File Url
// $download_id = $download['id']; // File Id (same as $key_download_id)
$output[$download_id] = '<a href="'.$download_link.'">'.$download_name.'</a>';
}
// Output example
echo implode('<br>', $output);
}
相关回答: