I am trying to view the download button belonging to the last order placed by a customer. Basically I have this code. It worked for other things like: displaying the purchase date, order total, product name etc, but it doesn't work with the downlad.
<?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();
it shows the word array instead of the download button. Could someone tell me where am I wrong? Excuse me, but I am relatively new to php.
// Edit - With probable solution //
Maybe I have found a solution. I modified the code by adding wc_get_template and array. This works well for users who have a download available. Unfortunately, however, users who have not made any purchases and therefore do not have a download available find themselves with a broken layout.
Is there any way to display an error message? or to correct this in any other way?
<?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
}
}