【问题标题】:How to work if, endif, else in wordpress shortcode如何在wordpress简码中使用if,endif,else
【发布时间】:2023-03-30 01:23:02
【问题描述】:

我正在尝试让一些 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 文件中

抱歉,我是粉丝,对此主题了解不多。

【问题讨论】:

    标签: php html wordpress woocommerce shortcode


    【解决方案1】:

    这是同一简码[short-test]的两个版本

    • 允许您将一些 HTML 写入字符串,这可能会变得混乱,尤其是当您需要将客户数据添加到其中时。
    • Two 将允许您在主题的子目录中创建一个 .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 模板文件中:&lt;?= do_shortcode('[short-test]'); ?&gt;
    • 在帖子编辑器中:[short-test]

    如果您只编写少量文本并且不打算在文本中添加客户数据(例如他们的姓名),则使用第一个选项并删除第二个选项。我已将第二个回调设为匿名回调,因此如果您同时复制粘贴,它不会中断。

    Output buffering documentationob_start()

    wc_get_customer_order_count

    【讨论】:

    • 感谢您的回复和您宝贵的时间。在我注意到你的答案之前,我开始写我的“自我答案”。无论如何,我设法为我找到了一个舒适的解决方案。再次感谢。
    • 我尝试按照您的建议进行操作,但没有成功,您能告诉我我哪里错了吗?
    • add_shortcode( 'order_download' , 'last_order_info_08' ); function last_order_info_08(){ $customer = new WC_Customer( get_current_user_id() ); $last_order = $customer-&gt;get_last_order(); $downloads = $last_order-&gt;get_downloadable_items(); if (is_a($last_order, 'WC_Order')) { wc_get_template('button-downloads.php', array( 'downloads' =&gt; $downloads, 'show_title' =&gt; true, ) ); } $order_count = wc_get_customer_order_count(get_current_user_id()); if ($order_count == 0) { return '&lt;p&gt;This is where you would put your HTML&lt;/p&gt;'; } }
    • 它会产生错误还是什么也不输出?
    • 感谢您的回答。没有输出,但布局呈现损坏,如原始问题中所述。我已经尝试过您的代码,它适用于从最后一个订单中检索信息的各种短代码。但是,由于某种原因,if ($ order_count == 0) 代码段不能与if (is_a ($ last_order, 'WC_Order')) 一起使用。很抱歉之前评论中的代码混乱,但我对此比较陌生。
    【解决方案2】:

    我找到了解决方案,这就是我所做的:

    <?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
    } 
    }
    
    

    我为遇到同样问题的人留下了一些信息。

    1. 如果您想更改要显示的信息类型,只需将get_order_number(); 更改为您想要的参数即可。示例如下:$last_order = $customer-&gt;get_formatted_order_total(); 将显示订单总数而不是订单号。在这里您可以找到所有 woocommerce 参数的列表https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/

    2. 如果要添加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();   
         }
        } 
    
    1. 如果您想在同一短代码中显示更多信息,请按如下方式编辑 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(); 
         }
        } 
    

    使用@Josh Bonnick 的解决方案进行编辑 在用各种方法“玩”了一下以获取错误消息而不是破坏布局之后,我还尝试了@Josh Bonnick 的解决方案,我在以下动作中实现了该解决方案。 (我采用了代码的第一部分,而不是需要模板的第二部分)。

    <?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  
    } 
    
    // Josh Bonnick recommended piece of code
    $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>'; 
    }
    

    【讨论】:

      【解决方案3】:

      我找到了一个解决方案,可以让下载按钮工作,如果用户没有可用的下载,这会破坏布局。

      <?php
      add_shortcode( 'order_download' , 'last_order_info_08' );
      function last_order_info_08(){
      $customer = new WC_Customer( get_current_user_id() );
      $last_order = $customer->get_last_order();
      
        if ( is_a( $last_order, 'WC_Order' ) ) {
           $downloads = $last_order->get_downloadable_items();
           
           if ($downloads){
               wc_get_template(
               'button-downloads.php',
               array(
               'downloads'  => $downloads,
               'show_title' => true,
               ));
               }
           } 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
          }     
      }
      

      【讨论】:

        猜你喜欢
        • 2011-06-14
        • 1970-01-01
        • 2014-10-04
        • 2012-08-05
        • 1970-01-01
        • 1970-01-01
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多