我正在嘗試讓一些 php 代碼在簡碼中作業。
這是我在 woocommerce dashboard.php 中所做的。這段代碼顯示了客戶最后一次下訂單的資料,一切正常。如您所見,它包含 if、endif 和 else。
<?php
// For logged in users only
if ( is_user_logged_in() ) :
// Get the current WC_Customer instance Object
$customer = new WC_Customer( get_current_user_id() );
// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
?>
<?php if( is_a($last_order, 'WC_Order') ) : ?>
<?php foreach ( $last_order->get_items() as $item ) : ?>
<div class="last_order_items"><?php echo $item->get_name(); ?></div>
<div class="last_order_items"><?php echo $item->get_total(); ?>€</div>
<div class="order_number">#<?php echo $last_order->get_order_number(); ?> </div>
<div class="order_number">Data acquisto <?php echo $last_order->get_date_created()->date('d/m/Y - H:i'); ?> </div>
<div class="order_number">Stato dell'ordine <?php echo esc_html( wc_get_order_status_name( $last_order->get_status() ) ); ?>
<?php endforeach; ?>
<?php 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 endif; ?>
<?php endif; ?>
這是我在 functions.php 檔案中所做的。簡碼作業正常,但如果用戶未登錄或未下訂單,則會發生錯誤。在dashboard.php 檔案中不存在錯誤,因為該if (is_user_logged_in ())部分存在并且在else用戶未下任何訂單時顯示訊息的部分。
//// LAST ORDER ON DASHBOARD WOOCOMMERCE SHORTCODE ////
add_shortcode( 'short_test' , 'test' );
function test(){
$customer = new WC_Customer( get_current_user_id() );
$last_order = $customer->get_last_order();
return $last_order->get_order_number();
}
我如何在這個簡碼中實作 if、endif 和 else?我希望能夠僅向登錄用戶顯示資料,對于那些沒有下任何訂單的用戶,我希望像在dashboard.php 檔案中那樣顯示一條訊息。我嘗試了很多方法,但都做不到。我想使用簡碼,因為它對我來說更方便,而且我更喜歡保持 woocommerce 模板“干凈”并將所有內容直接寫在 functions.php 檔案中
對不起,但我是一個粉絲,對這個主題沒有太多的了解。
uj5u.com熱心網友回復:
這是相同簡碼的兩個版本[short-test]
- 一個允許您將一些 HTML 寫入字串,這可能會變得混亂,特別是如果您需要將客戶資料添加到其中。
- 兩個將允許您在主題的子目錄中創建一個 .php 檔案,并將該模板作為要回傳的文本拉取。
functions.php
add_shortcode('short-test', 'test');
function test()
{
if (get_current_user_id() == 0) {
// User isn't logged in, don't output anything.
return '';
}
// Customer order count
$order_count = wc_get_customer_order_count(get_current_user_id());
if ($order_count == 0) {
return '<p>This is where you would put your HTML</p>';
}
}
add_shortcode('short-test', function () {
if (!get_current_user_id()) {
// User isn't logged in, don't output anything.
return '';
}
// Customer order count
$order_count = wc_get_customer_order_count(get_current_user_id());
if ($order_count == 0) {
// Customer has no orders, pull our template from /templates/customer-has-no-orders.php
ob_start();
include __DIR__ . '/templates/customer-has-no-orders.php';
return ob_get_clean();
}
return '';
});
templates/customer-has-no-orders.php
<p>You can write whatever you want here, in HMTL/PHP/JS</p>
用法:
- 在 .php 模板檔案中:
<?= do_shortcode('[short-test]'); ?> - 在帖子編輯器中:
[short-test]
如果您只撰寫少量文本并且不打算將客戶資料添加到文本中,例如他們的姓名,則使用第一個選項并洗掉第二個選項。我已經將第二個回呼設為匿名回呼,因此如果您同時復制粘貼,它不會中斷。
輸出緩沖檔案 ob_start()
wc_get_customer_order_count
uj5u.com熱心網友回復:
我找到了解決方案,這就是我所做的:
<?php
add_shortcode( 'short_test' , 'test' );
function test() {
// 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();
// Then I added the if condition. If this is the last order, then it shows the data from it
if ( is_a( $last_order, 'WC_Order' ) ) {
return $last_order->get_order_number(); // Change get_order_number to another parameter if you want to show different information
}
// With else show the warning message if the user has not placed any order
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
}
}
我為那些遇到同樣問題的人留下了一些資訊。
如果您想更改要顯示的資訊型別,只需更改
get_order_number();您想要的引數即可。示例如下:$last_order = $customer->get_formatted_order_total();將顯示訂單總額而不是訂單號。在這里您可以找到所有 woocommerce 引數的串列https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/如果要添加items等不同的引數,那么就得在if里面引入foreach,如下。
// Get and Loop Over Order Items
if ( is_a( $last_order, 'WC_Order' ) ) {
foreach ( $last_order->get_items() as $item ) {
return $last_order = $item->get_product_id();
}
}
- 如果您想在同一短代碼中顯示更多資訊,請按如下方式編輯 if 部分。請記住,只有 $ 項進入 foreach。如果您不需要 $ item 引數,您也可以洗掉 foreach。
if ( is_a( $last_order, 'WC_Order' ) ) {
?><div class="last_order" style="display:block">Order Number <?php echo $last_order->get_order_number();
?><div class="last_order">Order Total <?php echo $last_order->get_formatted_order_total();
?><div class="last_order">Order Status <?php echo $last_order->get_status();
foreach ( $last_order->get_items() as $item ) {
?><div class="last_order">Product ID <?php echo $last_order = $item->get_product_id();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/420984.html
標籤:
