'How to fix php shortcode to show woocommerce download button
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,
)
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
