2016-04-30 58 views
0

我在嘗試顯示我在woocommerce的訂單商品詳細信息模板的表格中創建的字段時遇到了一些麻煩,並且我對PHP不太瞭解。我創建了一個名爲sessions的字段並將其註冊爲產品帖子類型。如何在woocommerce訂單商品詳情模板上顯示自定義字段?

enter image description here

enter image description here

一旦用戶購買了產品,我想自定義字段(會話)也被顯示。

以下是woocommerce視圖順序的模板。

<?php 
/** 
* Order Item Details 
* 
* This template can be overridden by copying it to yourtheme/woocommerce/order/order-details-item.php. 
* 
*/ 

if (! defined('ABSPATH')) { 
    exit; 
} 

if (! apply_filters('woocommerce_order_item_visible', true, $item)) { 
    return; 
} 
?> 
<tr class="<?php echo esc_attr(apply_filters('woocommerce_order_item_class', 'order_item', $item, $order)); ?>"> 
    <td class="product-name"> 

     <?php 
      $is_visible = $product && $product->is_visible(); 

      echo apply_filters('woocommerce_order_item_name', $is_visible ? sprintf('<a href="%s">%s</a>', get_permalink($item['product_id']), $item['name']) : $item['name'], $item, $is_visible); 
      echo apply_filters('woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf('&times; %s', $item['qty']) . '</strong>', $item); 

      do_action('woocommerce_order_item_meta_start', $item_id, $item, $order); 

      $order->display_item_meta($item); 
      $order->display_item_downloads($item); 

      do_action('woocommerce_order_item_meta_end', $item_id, $item, $order); 
     ?> 
    </td> 
    <td class="product-total"> 
     <?php echo $order->get_formatted_line_subtotal($item); ?> 
    </td> 
</tr> 
<?php if ($show_purchase_note && $purchase_note) : ?> 
<tr class="product-purchase-note"> 
    <td colspan="3"><?php echo wpautop(do_shortcode(wp_kses_post($purchase_note))); ?></td> 
</tr> 
<?php endif; ?> 

我不知道或理解,我會插入the_field(sessions)來顯示它在結賬

+0

編輯該模板文件'的wp-content \主題\ - THEMENAME - \ woocommerce \結賬\ form-checkout.php'並且在那裏回顯字段。你使用acf嗎? –

+0

是的,我使用acf – clestcruz

+0

比你可以使用[get_field](https://www.advancedcustomfields.com/resources/get_field/)'$ field = get_field(「sessions」,idofproduct);' –

回答

0

試試這個辦法,看看你會得到什麼:

<?php 
     /** 
     * Order Item Details 
     * 
     * This template can be overridden by copying it to yourtheme/woocommerce/order/order-details-item.php. 
     * 
     */ 

     if (! defined('ABSPATH')) { 
      exit; 
     } 

     if (! apply_filters('woocommerce_order_item_visible', true, $item)) { 
      return; 
     } 
     $sessions = "sessions";  //DEFINE THE SESSIONS STRING... 
    ?> 
    <tr class="<?php echo esc_attr(apply_filters('woocommerce_order_item_class', 'order_item', $item, $order)); ?>"> 
     <td class="product-name"> 

      <?php 
       $is_visible = $product && $product->is_visible(); 

       echo apply_filters('woocommerce_order_item_name', $is_visible ? sprintf('<a href="%s">%s</a>', get_permalink($item['product_id']), $item['name']) : $item['name'], $item, $is_visible); 
       echo apply_filters('woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf('&times; %s', $item['qty']) . '</strong>', $item); 

       do_action('woocommerce_order_item_meta_start', $item_id, $item, $order); 

       $order->display_item_meta($item); 
       $order->display_item_downloads($item); 

       do_action('woocommerce_order_item_meta_end', $item_id, $item, $order); 

       //PERHAPS RIGHT HERE ADD THE SESSION 
       // IT IS IMPORTANT TO PASS IN THE PRODUCT-ID: $item['product_id'] SO THAT THE CORRECT FIELD IS RETRIEVED. 
       echo "<p class='session'><strong>" .__("Session: ", "ttd") . "</strong><em class='session-value'>" . get_field($sessions, $item['product_id']) . "</em></p>"; 
       //OR THIS WAY 
       echo "<p class='session'><strong>" .__("Session: ", "ttd") . "</strong><em class='session-value'>" . get_post_meta($item['product_id'], $sessions, true) . "</em></p>"; 
      ?> 
     </td> 
     <td class="product-total"> 
      <?php echo $order->get_formatted_line_subtotal($item); ?> 
     </td> 
    </tr> 
    <?php if ($show_purchase_note && $purchase_note) : ?> 
     <tr class="product-purchase-note"> 
      <td colspan="3"><?php echo wpautop(do_shortcode(wp_kses_post($purchase_note))); ?></td> 
     </tr> 
    <?php endif; ?> 

或者,你也可以做它是這樣的:

<?php 
     /** 
     * Order Item Details 
     * 
     * This template can be overridden by copying it to yourtheme/woocommerce/order/order-details-item.php. 
     * 
     */ 

     if (! defined('ABSPATH')) { 
      exit; 
     } 

     if (! apply_filters('woocommerce_order_item_visible', true, $item)) { 
      return; 
     } 
     $sessions = "sessions";  //DEFINE THE SESSIONS STRING... 
    ?> 
    <tr class="<?php echo esc_attr(apply_filters('woocommerce_order_item_class', 'order_item', $item, $order)); ?>"> 
     <td class="product-name"> 

      <?php 
       $is_visible = $product && $product->is_visible(); 

       echo apply_filters('woocommerce_order_item_name', $is_visible ? sprintf('<a href="%s">%s</a>', get_permalink($item['product_id']), $item['name']) : $item['name'], $item, $is_visible); 
       echo apply_filters('woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf('&times; %s', $item['qty']) . '</strong>', $item); 

       do_action('woocommerce_order_item_meta_start', $item_id, $item, $order); 

       $order->display_item_meta($item); 
       $order->display_item_downloads($item); 

       do_action('woocommerce_order_item_meta_end', $item_id, $item, $order); 
      ?> 
     </td> 
     <td class="product-session"> 
      <!-- ALTERNATIVELY ADD THE SESSION RIGHT HERE ON A UNIQUE COLUMN--> 
      <!-- IT IS IMPORTANT TO PASS IN THE PRODUCT-ID: $item['product_id'] SO THAT THE CORRECT FIELD IS RETRIEVED.--> 
      <p class='session'> 
       <strong><?php echo __("Session: ", "ttd"); ?></strong> 
       <em class='session-value'><?php the_field($sessions, $item['product_id']); ?></em> 
      </p> 
      <!-- OR THIS WAY --> 
      <p class='session'> 
       <strong><?php echo __("Session: ", "ttd"); ?></strong> 
       <em class='session-value'><?php echo get_post_meta($item['product_id'], $sessions, true); ?></em> 
      </p> 
     </td> 
     <td class="product-total"> 
      <?php echo $order->get_formatted_line_subtotal($item); ?> 
     </td> 
    </tr> 
    <?php if ($show_purchase_note && $purchase_note) : ?> 
     <tr class="product-purchase-note"> 
      <td colspan="3"><?php echo wpautop(do_shortcode(wp_kses_post($purchase_note))); ?></td> 
     </tr> 
    <?php endif; ?> 

但是,然後找到文件/模板/順序/次序-details.php。選擇第1行到35歲及以上它粘貼下面的代碼:

<?php 
/** 
* Order details 
* 
* This template can be overridden by copying it to yourtheme/woocommerce/order/order-details.php. 
* 
* HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer). 
* will need to copy the new files to your theme to maintain compatibility. We try to do this. 
* as little as possible, but it does happen. When this occurs the version of the template file will. 
* be bumped and the readme will list any important changes. 
* 
* @see   http://docs.woothemes.com/document/template-structure/ 
* @author WooThemes 
* @package WooCommerce/Templates 
* @version 2.5.3 
*/ 

if (! defined('ABSPATH')) { 
    exit; 
} 

$order = wc_get_order($order_id); 

$show_purchase_note = $order->has_status(apply_filters('woocommerce_purchase_note_order_statuses', array('completed', 'processing'))); 
$show_customer_details = is_user_logged_in() && $order->get_user_id() === get_current_user_id(); 
?> 
<h2><?php _e('Order Details', 'woocommerce'); ?></h2> 
<table class="shop_table order_details"> 
    <thead> 
    <tr> 
     <th class="product-name"><?php _e('Product', 'woocommerce'); ?></th> 
     <th class="product-session"><?php _e('Session', 'your_ttd'); ?></th> <!-- HERE WE ARE ADDING A COLUMN TO THE HEAD AS WELL TO MATCH...--> 
     <th class="product-total"><?php _e('Total', 'woocommerce'); ?></th> 
    </tr> 
    </thead> 
    <tbody> 

希望這有助於...

相關問題