从WooCommerce可下载产品访问可下载数据 [英] Access downloadable data from WooCommerce downloadable products

查看:172
本文介绍了从WooCommerce可下载产品访问可下载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 $ product = new WC_Product(get_the_ID()); 来获取WooCommerce产品元数据; 我正在获取产品价格以及所有其他价格重视产品是可下载的WooCommerce产品,我想获取以下数据:

I'm trying to fetch the WooCommerce products meta data using $product = new WC_Product( get_the_ID() ); I'm getting the product price and all the other values the products are downloadable WooCommerce products, I want to fetch the following data:

每当我尝试获取 $ product-> downloads-> id $ product-> downloads- > file 我得到的回报是null。请告诉我我在这里做错了什么。

Whenever i try to fetch $product->downloads->id or $product->downloads->file i'm getting null in return. Please tell me what i'm doing wrong over here.

推荐答案

要从可下载产品访问所有产品下载,您将使用 WC_Product get_downloads()方法

To access all product downloads from a downloadable product you will use WC_Product get_downloads() method.

它将为您提供一系列 WC_Product_Download 可以通过 WC_Product_Download 可用方法 (自WooCommerce 3)

It will give you an array of WC_Product_Download Objects which protected properties are accessible through WC_Product_Download available methods (since 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);
}

相关答案:

  • Add downloads in Woocommerce downloadable product programmatically
  • Add programmatically a downloadable file to Woocommerce products

这篇关于从WooCommerce可下载产品访问可下载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆