【发布时间】:2022-02-13 01:18:28
【问题描述】:
我正在尝试查看属于客户最后一个订单的下载按钮。基本上我有这个代码。它适用于其他方面,例如:显示购买日期、订单总额、产品名称等,但不适用于下载者。
<?php
add_shortcode( 'order_download' , 'last_order_info_08' );
function last_order_info_08(){
// Get the WC_Customer instance Object for the current user
$customer = new WC_Customer( get_current_user_id() );
// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
// If this is the last order, then it shows the data from it
if ( is_a( $last_order, 'WC_Order' ) ) {
return $last_order->get_downloadable_items();
}
}
return $last_order->get_downloadable_items(); 它显示单词数组而不是下载按钮。有人能告诉我我哪里错了吗?对不起,我对php比较陌生。
// 编辑 - 使用可能的解决方案 //
也许我找到了解决办法。我通过添加 wc_get_template 和数组来修改代码。这适用于有可用下载的用户。然而,不幸的是,没有购买任何东西因此没有可用下载的用户会发现自己的布局有问题。
有什么方法可以显示错误信息吗?或以任何其他方式更正此问题?
<?php
add_shortcode( 'order_download' , 'last_order_info_08' );
function last_order_info_08(){
// Get the WC_Customer instance Object for the current user
$customer = new WC_Customer( get_current_user_id() );
// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
// Works with array
$downloads = $last_order->get_downloadable_items();
// If this is the last order, then it shows the data from it
if ( is_a( $last_order, 'WC_Order' ) ) {
wc_get_template('button-downloads.php',
array(
'downloads' => $downloads,
'show_title' => true,
)
);
}
}
【问题讨论】:
标签: php html wordpress woocommerce shortcode