如何修复 php 简码以显示 woocommerce 下载按钮
How to fix php shortcode to show woocommerce download button
我正在尝试查看属于客户下的最后一个订单的下载按钮。基本上我有这段代码。它适用于其他事情,例如:显示购买日期、订单总额、产品名称等,但不适用于下载。
<?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
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 shows the data from it
if ( is_a( $last_order, 'WC_Order' ) ) {
// Works with array below
$downloads = $last_order->get_downloadable_items();
if ($downloads){
wc_get_template(
'button-downloads.php', // This is a template I placed in wp-content/themes/theme-child/woocommerce
array(
'downloads' => $downloads,
'show_title' => true,
));
}
}
//The else part shows the warning message when a user has no order, this does not break the layout.
else {
?><div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>"><?php esc_html_e( 'Browse products', 'woocommerce' ); ?></a>
<?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?>
</div><?php
}
}
我正在尝试查看属于客户下的最后一个订单的下载按钮。基本上我有这段代码。它适用于其他事情,例如:显示购买日期、订单总额、产品名称等,但不适用于下载。
<?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
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 shows the data from it
if ( is_a( $last_order, 'WC_Order' ) ) {
// Works with array below
$downloads = $last_order->get_downloadable_items();
if ($downloads){
wc_get_template(
'button-downloads.php', // This is a template I placed in wp-content/themes/theme-child/woocommerce
array(
'downloads' => $downloads,
'show_title' => true,
));
}
}
//The else part shows the warning message when a user has no order, this does not break the layout.
else {
?><div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>"><?php esc_html_e( 'Browse products', 'woocommerce' ); ?></a>
<?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?>
</div><?php
}
}