在WooCommerce我的账户下载页面中显示订单ID和订单日期。
P粉330232096
P粉330232096 2023-07-27 13:10:51
0
1
389
<p>我想在“我的账户”下载页面中显示订单ID、订单日期和产品图片。</p>
add_filter( 'woocommerce_account_downloads_column_download-product', 'display_product_image_on_account_downloads' );
函数 display_product_image_on_account_downloads( $download ) {
    // 仅定位查看订单页面
    if ( !is_wc_endpoint_url( '下载' ) ) return;

    if ( $download['product_id'] > 0 ) {
        $product = wc_get_product( $download['product_id'] ); 
        $image = $product->get_image( array(324, 194) ); // 产品图片
        $order_id = $order->get_id(); // 订单编号

        如果 ( $download['product_url'] ) {
            回显 $image 。 '<a href="' .esc_url( $download['product_url'] ) . '">' 。 esc_html( $download['product_name'] ) . '</a>';
        回显'

' 。 esc_html( $order_id ) 。 '</p>'; 回显'

' 。 esc_html( wc_format_datetime( $order->get_date_created() ) ) . '</p>'; } 别的 { 回显 $image 。 esc_html( $download['产品名称'] ); } } }</pre> <p>产品图片已显示,但订单ID和订单日期未显示正确。有没有办法实现这个?谢谢。</p>

P粉330232096
P粉330232096

全部回复(1)
P粉903969231

我仔细检查了您的代码并发现其中有一些问题。所以您可以使用以下版本:

add_filter( 'woocommerce_account_downloads_column_download-product', 'display_product_image_order_info_on_account_downloads', 10, 2 );
function display_product_image_order_info_on_account_downloads( $download, $item ) {
    // Targeting view order pages only
    if ( ! is_wc_endpoint_url( 'downloads' ) ) return;

    if ( $download['product_id'] > 0 ) {
        $product    = wc_get_product( $download['product_id'] );
        $image      = $product->get_image( array( 324, 194 ) ); // The product image
        $order_id   = $item->get_order_id(); // Get the order ID from the $item object
        $order_date = $item->get_order()->get_date_created(); // Get the order date from the $item object

        if ( $download['product_url'] ) {
            echo $image . '<a href="' . esc_url( $download['product_url'] ) . '">' . esc_html( $download['product_name'] ) . '</a>';
            echo '<p>' . esc_html( $order_id ) . '</p>';
            echo '<p>' . esc_html( wc_format_datetime( $order_date ) ) . '</p>'; 
        } else {
            echo $image . esc_html( $download['product_name'] );
        }
    }
}

您可以将其添加到您的子主题的functions.php文件中。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!