主页 WordPress专区 woocommerce_get_order_item_totals钩子的注意点
狄默默斯基
发表于2017-01-05 11:05:24    只看楼主 楼主
头衔:  精益求精
注册时间: 2014-02-09
用户组: 日常管理组
发帖数:  578
金币数:  310
短消息
废话不多说,先看代码
function my_custom_field_display_before_shipping( $total_rows, $order ){
     
    $my_custom_field_1 = get_post_meta( $order->id, '_my_custom_field_1', true );
    $new_total_rows = array();
    if( !empty($my_custom_field_1) ){
        $new_row['my_custom_field_1'] = array(
            'label' => '送货时间:',
            'value' => $my_custom_field_1
        );
 
        // Insert $new_row after shipping field
        $total_rows = array_merge( array_splice( $total_rows,0,2), $new_row, $total_rows );
    }
    return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'my_custom_field_display_before_shipping', 10, 2 );
其中这个钩子woocommerce_get_order_item_totals在函数中是这样子的,
File name: woocommerce/includes/abstracts/abstract-wc-order.php
public function get_order_item_totals( $tax_display = '' ) {

    if ( ! $tax_display ) {
      $tax_display = $this->tax_display_cart;
    }

    $total_rows = array();

    if ( $subtotal = $this->get_subtotal_to_display( false, $tax_display ) ) {
      $total_rows['cart_subtotal'] = array(
        'label' => __( 'Subtotal:', 'woocommerce' ),
        'value'  => $subtotal
      );
    }

    if ( $this->get_total_discount() > 0 ) {
      $total_rows['discount'] = array(
        'label' => __( 'Discount:', 'woocommerce' ),
        'value'  => '-' . $this->get_discount_to_display( $tax_display )
      );
    }

    if ( $this->get_shipping_method() ) {
      $total_rows['shipping'] = array(
        'label' => __( 'Shipping:', 'woocommerce' ),
        'value'  => $this->get_shipping_to_display( $tax_display )
      );
    }

    if ( $fees = $this->get_fees() ) {
      foreach ( $fees as $id => $fee ) {

        if ( apply_filters( 'woocommerce_get_order_item_totals_excl_free_fees', $fee['line_total'] + $fee['line_tax'] == 0, $id ) ) {
          continue;
        }

        if ( 'excl' == $tax_display ) {

          $total_rows[ 'fee_' . $id ] = array(
            'label' => ( $fee['name'] ? $fee['name'] : __( 'Fee', 'woocommerce' ) ) . ':',
            'value'  => wc_price( $fee['line_total'], array('currency' => $this->get_order_currency()) )
          );

        } else {

          $total_rows[ 'fee_' . $id ] = array(
            'label' => $fee['name'] . ':',
            'value'  => wc_price( $fee['line_total'] + $fee['line_tax'], array('currency' => $this->get_order_currency()) )
          );
        }
      }
    }

    // Tax for tax exclusive prices.
    if ( 'excl' === $tax_display ) {

      if ( get_option( 'woocommerce_tax_total_display' ) == 'itemized' ) {

        foreach ( $this->get_tax_totals() as $code => $tax ) {

          $total_rows[ sanitize_title( $code ) ] = array(
            'label' => $tax->label . ':',
            'value'  => $tax->formatted_amount
          );
        }

      } else {

        $total_rows['tax'] = array(
          'label' => WC()->countries->tax_or_vat() . ':',
          'value'  => wc_price( $this->get_total_tax(), array( 'currency' => $this->get_order_currency() ) )
        );
      }
    }

    if ( $this->get_total() > 0 && $this->payment_method_title ) {
      $total_rows['payment_method'] = array(
        'label' => __( 'Payment Method:', 'woocommerce' ),
        'value' => $this->payment_method_title
      );
    }

    if ( $refunds = $this->get_refunds() ) {
      foreach ( $refunds as $id => $refund ) {
        $total_rows[ 'refund_' . $id ] = array(
          'label' => $refund->get_refund_reason() ? $refund->get_refund_reason() : __( 'Refund', 'woocommerce' ) . ':',
          'value'  => wc_price( '-' . $refund->get_refund_amount(), array( 'currency' => $this->get_order_currency() ) )
        );
      }
    }

    $total_rows['order_total'] = array(
      'label' => __( 'Total:', 'woocommerce' ),
      'value'  => $this->get_formatted_order_total( $tax_display )
    );

    return apply_filters( 'woocommerce_get_order_item_totals', $total_rows, $this );
  }
注意第2个参数带的是$this,也就是说,带的是这个订单对象,
所以取ID,直接$order->id
引用回复  你好陌生人 ~ 千万别说爱 ~ 让我保持等的姿势继续期待

回复人
回复内容

Powered BY YouYaX
个人自主开发论坛,从2010年10月份开发至今!

操作管理