WooCommerce不会向发送给客户的电子邮件显示自定义费用

时间:2018-06-05 15:03:20

标签: php wordpress woocommerce hook-woocommerce

我已经建立了一个插件,可以从WooCommerce的订单中加收费用。我使用带有order_item_type&#39;费用的wc_add_order_item方法将此费用添加到订单中。我遇到的问题是,当该客户下订单时,该费用不会显示在发送给客户的电子邮件中。如果我理解正确,WooCommerce通常会在<tfoot>中使用$ order-&gt; get_order_item_totals()从email-order-details.php中添加费用和运费;然后通过它们循环。

奇怪的是,当我试图寻找解决方案时,我遇到了&#39; woocommerce_order_status_pending_to_processing_notification&#39; hook,此挂钩(如果我理解正确)在电子邮件发送给用户之前触发。在这个钩子的回调中,你将拥有一个订单ID,我可以从我的主题中调用我的functions.php中的这个钩子。在回调中,我搜索了正确的顺序,并使用它来检查$ order-&gt; get_order_item_totals()中的内容。我预计只有基本的东西,我的增加的费用不会出现,除非它。

在将电子邮件发送给客户之前,虽然我的费用在$ order-&gt; get_order_item_totals()中可见,但是WooCommerce不会在email-order-details.php中循环播放,这怎么可能?或者我错过了什么?有什么想法吗?

最终目标是在发送给客户的电子邮件中收取我的自定义费用。

作为参考,这是email-order-details.php中的循环:

$totals = $order->get_order_item_totals();

if ( $totals ) {
    $i = 0;
    foreach ( $totals as $total ) {
        $i++;
        ?>
        <tr>
            <th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['label'] ); ?></th>
            <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['value'] ); ?></td>
        </tr>
        <?php
    }
}

1 个答案:

答案 0 :(得分:1)

我不知道您使用哪些代码将费用添加到您的订单中,但我已经为我的woocommerce订单添加了服务费,并且它还显示在woocommerce电子邮件通知中。

尝试将此代码添加到functions.php或从中创建插件。您可以根据需要编辑费用金额。这是代码

/**
 * Add a standard $ value service fee to all transactions in cart / checkout
 */
add_action( 'woocommerce_cart_calculate_fees','wc_add_svc_fee' ); 
function wc_add_svc_fee() { 
global $woocommerce; 

if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
return;


// change the $fee to set the Service Fee to a value to suit
$fee = 1.00;


    $woocommerce->cart->add_fee( 'Service Fee', $fee, true, 'standard' );  

}

如果您希望费用或收入是订单总额的百分比,请使用以下代码

/**
 * Add a 1% surcharge to your cart / checkout
 * change the $percentage to set the surcharge to a value to suit
 */
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 0.01;
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
    $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );

}

使用上述任何代码,费用或附加费将显示在电子邮件通知中。

相关问题