Woocommerce – Undefined variable in downloads.php template

تبليغ
سؤال

يرجى شرح بإيجاز لمإذا تشعر أنك ينبغي الإبلاغ عن هذا السؤال.

تبليغ
‎إلغاء

I’m trying to build my own downloads.php template. So I built a table which should return product name, id, date, etc… however I get some undefined variable errors.

Notice: Undefined variable: customer_orders on line 29

Notice: Trying to get property ‘orders’ of non-object on line 29

Warning: Invalid argument supplied for foreach() on line 29

Notice: Undefined variable: customer_orders on line 98

Notice: Trying to get property ‘max_num_pages’ of non-object on line 98

Notice: Undefined variable: current_page on line 99

Below is the content of the lines

On line 29: foreach ($customer_orders-> orders as $customer_order)

On line 98: ‘total’ => $customer_orders->max_num_pages,

On line 99: ‘current’ => $current_page,

I think I’m doing the foreach loop or something close to wrong. Can anyone help me figure out where am I wrong? I appreciate any help, thanks.

This is the original downloads.php template file: https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/templates/myaccount/downloads.php

This is the change I made:

<?php

if ( ! defined( ‘ABSPATH’ ) ) {
exit;
}

$downloads = WC()->customer->get_downloadable_products();
$has_downloads = (bool) $downloads;

do_action( ‘woocommerce_before_account_downloads’, $has_downloads ); ?>

<?php if ( $has_downloads ) : ?>

<table class=”table_orders heading”><tr>
<td class=”product_number”>Ordine</td>
<td class=”product_name”>Prodotto</td>
<td class=”product_data”>Data</td>
<td class=”product_price”>Totale</td>
<td class=”product_status”>Stato</td>
<td class=”product_action”>File</td>
</tr></table>

<?php

// Get Access $order variable Foreach – Definisci le variabili sotto per poi utilizzarle in echo
foreach ( $customer_orders->orders as $customer_order ) {
// Get $product object from $order / $order_id
$order = wc_get_order( $customer_order );
$items = $order->get_items();

$orders_id = $order->get_id();
$status = wc_get_order_status_name( $order->get_status() );
$date_created = $order->get_date_created()->date(‘d/m/Y’);
$payment_method = $order->get_payment_method_title();
$order_total = $order->get_formatted_order_total();

// Get Access Items & Product Variable Foreach
foreach ( $items as $item ) {
$product_name = $item->get_name();
$view_order = $order->get_view_order_url();

//Get product download button
$downloads = $order->get_downloadable_items();
if(is_array($downloads)) {
foreach($downloads as $product){
$download_button = ‘<a href=”‘. $product[‘download_url’] .'” target=”_blank”>Download</a>’;
}
}

echo ‘
<table class=”table_orders”>
<tr class=”table_row_items”>
<td class=”product_number”>
<span class=”mobile title”>Ordine</span>
<span>#’. esc_attr($orders_id) .'</span>
</td>

<td class=”product_name”>
<span class=”mobile title”>Prodotto</span>
<a href=”‘. wp_kses_post($view_order) .'”>’. wp_kses_post($product_name) .'</a>
</td>

<td class=”product_data”>
<span class=”mobile title”>Data</span>
<span>’. wp_kses_post($date_created) .'</span>
</td>

<td class=”product_price”>
<span class=”mobile title”>Prezzo</span>
<span>’. wp_kses_post($order_total) .'</span>
</td>

<td class=”product_status”>
<span class=”mobile title”>Stato</span>
<span class=”label ‘ . $order->get_status() . ‘”>’. wp_kses_post($status) .'</span>
</td>

<td class=”product_action”>
<span class=”mobile title”>File</span>
<a target=”_blank” href=”‘. esc_url($download_button) .'”>Scarica<i class=”fa-duotone fa-eye”></i></a>
</td>
</tr>
</table>
‘;

// Button Download – if($downloads) { echo ‘<div class=”container_orders_download”> ‘. $download_button .’ </div>’; }
}
}

// Pagination button – Responsabile dei bottoni e numerazione delle pagine della cronologia ordini
?><div class=”container-pagination”><?php
$args = array(
‘base’ => esc_url( wc_get_endpoint_url( ‘orders’) ) . ‘%_%’,
‘format’ => ‘%#%’,
‘total’ => $customer_orders->max_num_pages,
‘current’ => $current_page,
‘show_all’ => false,
‘end_size’ => 3,
‘mid_size’ => 3,
‘prev_next’ => true,
‘prev_text’ => __(‘<i class=”fa-regular fa-angle-left”></i><span>Indietro</span>’),
‘next_text’ => __(‘<span>Avanti</span><i class=”fa-regular fa-angle-right”></i>’),
‘type’ => ‘plain’,
‘add_args’ => false,
‘add_fragment’ => ”
);
echo paginate_links($args);
?></div>
</div>

<?php else : ?>
<div class=”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 downloads available yet.’, ‘woocommerce’ ); ?>
</div>
<?php endif; ?>

<?php do_action( ‘woocommerce_after_account_downloads’, $has_downloads ); ?>

‫أضف إجابة

تصفح
تصفح

مجهول يجيب