【问题标题】:How to fix php shortcode to show woocommerce download button如何修复 php 短代码以显示 woocommerce 下载按钮
【发布时间】:2022-02-13 01:18:28
【问题描述】:

我正在尝试查看属于客户最后一个订单的下载按钮。基本上我有这个代码。它适用于其他方面,例如:显示购买日期、订单总额、产品名称等,但不适用于下载者。

<?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-&gt;get_downloadable_items(); 它显示单词数组而不是下载按钮。有人能告诉我我哪里错了吗?对不起,我对php比较陌生。

// 编辑 - 使用可能的解决方案 //

也许我找到了解决办法。我通过添加 wc_get_template 和数组来修改代码。这适用于有可用下载的用户。然而,不幸的是,没有购买任何东西因此没有可用下载的用户会发现自己的布局有问题。

有什么方法可以显示错误信息吗?或以任何其他方式更正此问题?

<?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 html wordpress woocommerce shortcode


    【解决方案1】:
    <?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
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 2022-01-20
      • 2021-10-15
      相关资源
      最近更新 更多