2014-06-25 128 views
1

以下是我正在尝试做的和在哪里。WooCommerce - 将产品类别添加到订单明细表

在订单的结账过程中,在插件WooCommerce中;当结账过程完成后,您将被带到一个页面。它显示订单详细信息的概述。用于输出此页面的模板文件是order-details.php。

这里是想什么我要补充

我想显示产品的产品类别,象这样:

enter image description here

这是代码我试图区添加到订单明细表的第一部分。

<tbody> 
    <?php 
    if (sizeof($order->get_items()) > 0) { 

     foreach($order->get_items() as $item) { 
      $_product  = apply_filters('woocommerce_order_item_product', $order->get_product_from_item($item), $item); 
      $item_meta = new WC_Order_Item_Meta($item['item_meta'], $_product); 

      ?> 
      <tr class="<?php echo esc_attr(apply_filters('woocommerce_order_item_class', 'order_item', $item, $order)); ?>"> 
       <td class="product-name"> 
        <?php 
         if ($_product && ! $_product->is_visible()) 
          echo apply_filters('woocommerce_order_item_name', $item['name'], $item); 
         else 
          echo apply_filters('woocommerce_order_item_name', sprintf('<a href="%s">%s</a>', get_permalink($item['product_id']), $item['name']), $item); 

         echo apply_filters('woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf('&times; %s', $item['qty']) . '</strong>', $item); 

这是我的理解为止;

  • 正在应用自定义查询。
  • 对于每个产品列出的细节都有回应。
  • WooCommerce类正被用于获取特定的产品元。

我的理解是我目前的知识水平的观察。

我希望的是,我可以使用类似

echo apply_filters('woocommerce_order_item_name', ' <strong class="product-category">' . e_($item['product_cat']) . '</strong>', $item); 

我无法找到更相关的过滤器,我真的不知道,[“product_cat”]是即使与此有关。这是我试图解决我的要求的一个例子。

正如你所看到的,我不全职工作于PHP。我尽可能多地学习。建议将不胜感激

+0

嗨,约翰。你可以编辑来解释你想要添加什么以及在哪里?截图不会是一个可怕的想法。;)尽管我倾向于认为我们将通过'woocommerce_order_item_name'过滤器将类别名称添加到项目名称。 – helgatheviking

+0

谢谢!我添加了一个截图。明白我可能会使用'woocommerce_order_item_name'。我现在要去检查一下。请让我知道,如果你认为我应该增加更多的问题 – JohnMcCarthy

回答

2

这是如何将产品类别添加到产品标题。我不是特别喜欢它是如何为我输出的,但是我正在测试它是否在可变订阅中,因此正在修改它自己的数据。这将去主题functions.php

function kia_woocommerce_order_item_name($name, $item){ 

    $product_id = $item['product_id']; 
    $tax = 'product_cat'; 

    $terms = wp_get_post_terms($product_id, $tax, array('fields' => 'names')); 

    if($terms && ! is_wp_error($terms)) { 
     $taxonomy = get_taxonomy($tax); 
     $name .= '<label>' . $taxonomy->label . ': </label>' . implode(', ', $terms); 
    } 

    return $name; 
} 
add_filter('woocommerce_order_item_name', 'kia_woocommerce_order_item_name', 10, 2); 

否则,你可以在order-details.php模板复制到自己的主题(所以yourtheme/woocommerce/order/order-details.php和直接添加代码。这将让您对您想要的类别出现更多的控制因为本节中没有大量的钩子,但是如果WooCommerce修改了这段代码,就会使您处于危险之中,因为主题使用的是过时的模板,所以我曾在几个网站上工作过。 ,一个例子是:

<?php 
/** 
* Order details 
* 
* @author  WooThemes 
* @package  WooCommerce/Templates 
* @version  2.2.0 
*/ 

if (! defined('ABSPATH')) exit; // Exit if accessed directly 

global $woocommerce; 

$order = new WC_Order($order_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-total"><?php _e('Total', 'woocommerce'); ?></th> 
     </tr> 
    </thead> 
    <tfoot> 
    <?php 
     if ($totals = $order->get_order_item_totals()) foreach ($totals as $total) : 
      ?> 
      <tr> 
       <th scope="row"><?php echo $total['label']; ?></th> 
       <td><?php echo $total['value']; ?></td> 
      </tr> 
      <?php 
     endforeach; 
    ?> 
    </tfoot> 
    <tbody> 
     <?php 
     if (sizeof($order->get_items()) > 0) { 

      foreach($order->get_items() as $item) { 
       $_product  = apply_filters('woocommerce_order_item_product', $order->get_product_from_item($item), $item); 
       $item_meta = new WC_Order_Item_Meta($item['item_meta'], $_product); 

       ?> 
       <tr class="<?php echo esc_attr(apply_filters('woocommerce_order_item_class', 'order_item', $item, $order)); ?>"> 
        <td class="product-name"> 
         <?php 
          if ($_product && ! $_product->is_visible()) 
           echo apply_filters('woocommerce_order_item_name', $item['name'], $item); 
          else 
           echo apply_filters('woocommerce_order_item_name', sprintf('<a href="%s">%s</a>', get_permalink($item['product_id']), $item['name']), $item); 

          echo apply_filters('woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf('&times; %s', $item['qty']) . '</strong>', $item); 

          $item_meta->display(); 

          // insert product category here 
          $tax = 'product_cat'; 
          $terms = wp_get_post_terms($_product->id, $tax, array('fields' => 'names')); 

          if($terms && ! is_wp_error($terms)) { 
           $taxonomy = get_taxonomy($tax); 
           echo '<strong>' . $taxonomy->label . ': </strong>' . implode(', ', $terms); 
          } 

          // end edit 

          if ($_product && $_product->exists() && $_product->is_downloadable() && $order->is_download_permitted()) { 

           $download_files = $order->get_item_downloads($item); 
           $i    = 0; 
           $links   = array(); 

           foreach ($download_files as $download_id => $file) { 
            $i++; 

            $links[] = '<small><a href="' . esc_url($file['download_url']) . '">' . sprintf(__('Download file%s', 'woocommerce'), (count($download_files) > 1 ? ' ' . $i . ': ' : ': ')) . esc_html($file['name']) . '</a></small>'; 
           } 

           echo '<br/>' . implode('<br/>', $links); 
          } 
         ?> 
        </td> 
        <td class="product-total"> 
         <?php echo $order->get_formatted_line_subtotal($item); ?> 
        </td> 
       </tr> 
       <?php 

       if ($order->has_status(array('completed', 'processing')) && ($purchase_note = get_post_meta($_product->id, '_purchase_note', true))) { 
        ?> 
        <tr class="product-purchase-note"> 
         <td colspan="3"><?php echo wpautop(do_shortcode($purchase_note)); ?></td> 
        </tr> 
        <?php 
       } 
      } 
     } 

     do_action('woocommerce_order_items_table', $order); 
     ?> 
    </tbody> 
</table> 

<?php do_action('woocommerce_order_details_after_order_table', $order); ?> 

<header> 
    <h2><?php _e('Customer details', 'woocommerce'); ?></h2> 
</header> 
<dl class="customer_details"> 
<?php 
    if ($order->billing_email) echo '<dt>' . __('Email:', 'woocommerce') . '</dt><dd>' . $order->billing_email . '</dd>'; 
    if ($order->billing_phone) echo '<dt>' . __('Telephone:', 'woocommerce') . '</dt><dd>' . $order->billing_phone . '</dd>'; 

    // Additional customer details hook 
    do_action('woocommerce_order_details_after_customer_details', $order); 
?> 
</dl> 

<?php if (! wc_ship_to_billing_address_only() && $order->needs_shipping_address() && get_option('woocommerce_calc_shipping') !== 'no') : ?> 

<div class="col2-set addresses"> 

    <div class="col-1"> 

<?php endif; ?> 

     <header class="title"> 
      <h3><?php _e('Billing Address', 'woocommerce'); ?></h3> 
     </header> 
     <address><p> 
      <?php 
       if (! $order->get_formatted_billing_address()) _e('N/A', 'woocommerce'); else echo $order->get_formatted_billing_address(); 
      ?> 
     </p></address> 

<?php if (! wc_ship_to_billing_address_only() && $order->needs_shipping_address() && get_option('woocommerce_calc_shipping') !== 'no') : ?> 

    </div><!-- /.col-1 --> 

    <div class="col-2"> 

     <header class="title"> 
      <h3><?php _e('Shipping Address', 'woocommerce'); ?></h3> 
     </header> 
     <address><p> 
      <?php 
       if (! $order->get_formatted_shipping_address()) _e('N/A', 'woocommerce'); else echo $order->get_formatted_shipping_address(); 
      ?> 
     </p></address> 

    </div><!-- /.col-2 --> 

</div><!-- /.col2-set --> 

<?php endif; ?> 

<div class="clear"></div> 
+0

这很好,非常感谢你的时间。添加到此...我们在哪里使用'wp_get_post_terms($ _product-> id,$ tax,array('fields'=>'names'));'我错在想我可以显示类别slug ''fields'=>'slug''而不是'=>'name''? – JohnMcCarthy

+0

好吧,经过一番搜索后,我发现'<?php wp_get_object_terms($ object_ids,$ taxonomy,$ args)?>'这导致我使用'fields'=>'slugs''然后显示产品类别slug @helgatheviking善意分享的功能。再次感谢。 – JohnMcCarthy

相关问题