我已经建立了一个插件,增加了从WooCommerce的订单费用。我使用带有order_item_type ' fee‘的wc_add_order_item方法将此费用添加到订单中。我遇到的问题是,当客户下订单时,费用没有显示在发送给客户的电子邮件中。如果我理解正确的话,WooCommerce通常会用$order->get_order_item_totals();将所有的费用和运费从email-order-details.php的<tfoot>
中相加,然后循环通过它们。
奇怪的是,当我试图寻找解决方案时,我遇到了'woocommerce_order_status_pending_to_processing_notification‘hook,这个钩子(如果我再次正确理解的话)在电子邮件发送给用户之前触发。在这个钩子的回调中,您将拥有一个订单ID,我在我的functions.php中从我的主题调用了这个钩子。在回调中,我搜索了正确的顺序,并用它检查了$ order ->get_order_item_total()中的内容。我以为只有基本的东西,我的附加费用不会显示出来,但它出现了。
在电子邮件发送给客户之前,我的费用在$order->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
}
}
发布于 2018-06-10 04:04:56
我不知道您使用了哪些代码将费用添加到您的订单中,但我已经在我的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, '' );
}
使用上述任何代码,费用或附加费将出现在电子邮件通知中。
https://stackoverflow.com/questions/50703271
复制相似问题