将自定义字段添加到WooCommerce订单项

时间:2013-12-25 19:21:10

标签: php wordpress woocommerce

目前,订单中的WooCommerce订单项具有以下列:

  • 项目
  • 税级
  • 数量
  • 总计
  • 纳税

See screenshot

我的目标是添加一个额外的列(或元),其中包含产品状态的下拉字段。

有没有人对如何实现这一点有任何想法?

2 个答案:

答案 0 :(得分:2)

我正在对这张桌子进行一些重大调整,但我还没有想到这一点,但我知道这一点,如果你回顾一下 class-wc-meta-插件目录中的box-order-items.php ,你会发现这段代码:

// List order items
$order_items = $order->get_items( apply_filters( 'woocommerce_admin_order_item_types',  array( 'line_item', 'fee' ) ) );

foreach ( $order_items as $item_id => $item ) {

switch ( $item['type'] ) {
    case 'line_item' :
        $_product   = $order->get_product_from_item( $item );
        $item_meta  = $order->get_item_meta( $item_id );

        include( 'views/html-order-item.php' );
    break;
    case 'fee' :
        include( 'views/html-order-fee.php' );
    break;
}

do_action( 'woocommerce_order_item_' . $item['type'] . '_html', $item_id, $item );
}

请勿编辑此内容!!!

从这里开始,您可以看到该表格的组成部分。我相信(据我所知的WooCommerce到目前为止)你可以创建一个挂在那里的过滤器的函数: woocommerce_admin_order_item_types();

这就是我会做的事情:

# Admin Panel Updates
add_filter( 'woocommerce_add_order_item_meta', array( $this, 'display_order_item_meta' ), 10, 2 );
public function display_order_item_meta( $order_items ) {
    array_push($order_items,'event'); //Not sure how to manipulate this yet
    return $order_items;
}

看起来我们需要对动作挂钩(woocommerce_order_item_' . $item['type'] . '_html', $item_id, $item)做一些事情,或许还有以下几点:

add_filter( 'woocommerce_order_item_event_html', array( $this, 'display_event_item_meta' ), 10, 2 );

public function display_event_item_meta( $item_id, $item) {
    switch ( $item['type'] ) {
        case 'event' :
        include( 'views/html-order-event-item.php' );
            break;
    }
}

到目前为止,这是我最好的猜测,但我很肯定这是要解析的正确代码片段。

答案 1 :(得分:2)

如果其他人偶然发现这一点,我需要在编辑订单屏幕上的订单商品元框中添加一些自定义订单元。 2017年,这就是我解决这个难题的方法。

class-wc-meta-box-order-items.php下的文件includes/admin/meta-boxes自2014年以来略有变化,但确实包含模板文件html-order-items.php

在最后一个文件中,您将找到两个未记录的钩子 woocommerce_admin_order_item_headers ,您可以使用它来添加自定义列标题文本并访问$ order对象和< strong> woocommerce_admin_order_item_values ,它将您的自定义内容放在“成本”列之前,并且可以访问$ product,$ item和$ item_id。

因此,要添加自定义列,它将看起来像这样。

add_action( 'woocommerce_admin_order_item_headers', 'pd_admin_order_items_headers' );
function pd_admin_order_items_headers($order){
  ?>
  <th class="line_customtitle sortable" data-sort="your-sort-option">
    Custom Title
  </th>
  <?php
}

你的排序选项取决于你想要排序的数据。我在我的情况下使用了字符串。

与每个订单项中的内容相比。

add_action( 'woocommerce_admin_order_item_values', 'pd_admin_order_item_values' );
function pd_admin_order_item_values( $product, $item, $item_id ) {
  //Get what you need from $product, $item or $item_id
  ?>
  <td class="line_customtitle">
    <?php //your content here ?>
  </td>
  <?php
}

如果您需要在该元框中的不同位置内容,那么该模板文件中还有一些其他钩子和过滤器绝对值得一看。

相关问题