2016-04-30 2 views
0

У меня возникли проблемы с попыткой отобразить поле, которое я создал в таблице шаблона деталей элементов заказа woocommerce, и я не очень хорошо разбираюсь в PHP. Я создал поле, называемое сеансами, и зарегистрировал его как тип сообщения продукта.Как отобразить настраиваемое поле в шаблоне деталей заказа 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

редактировать шаблон файл 'сор-контент \ Themes \ - THEMENAME - \ WooCommerce \ Кассовых \ form-checkout.php' и отбросить там поле. Вы используете acf? –

+0

yes Я использую acf – clestcruz

+0

, чем вы можете использовать [get_field] (https://www.advancedcustomfields.com/resources/get_field/) '$ field = get_field (« session », 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> 

Надеется, что это помогает ...

Смежные вопросы