gpt4 book ai didi

php - 在 Woocommerce 谢谢页面和电子邮件的订单总计中添加一个新行

转载 作者:行者123 更新时间:2023-12-05 04:06:55 24 4
gpt4 key购买 nike

我在电子邮件和感谢页面中添加一行的代码有问题。

add_filter( 'woocommerce_get_order_item_totals', 'bbloomer_add_recurring_row_email', 10, 2 );
function bbloomer_add_recurring_row_email( $total_rows, $myorder_obj ) {
$total_rows['recurr_not'] = array(
'label' => __( 'Rec:', 'woocommerce' ),
'value' => 'blabla'
);
return $total_rows;
}

我有一个功能可以在购物车中添加“Total excl. VAT”行:

<?php 
global $woocommerce;
$frais = 1.01;
echo '<tr class ="totalht">
<th>'. __( 'Total HT', 'woocommerce' ) .'</th>
<td data-title=" '. __( 'Total HT', 'woocommerce' ) .' ">'
. wc_price( ( $woocommerce->cart->cart_contents_total * $frais ) + $woocommerce->cart->shipping_total ) .'<span class="ht-panier">HT</span></td>
</tr>';
?>

运行良好:https://prnt.sc/irglky

但是当我修改第一个函数时:

add_filter( 'woocommerce_get_order_item_totals', 'bbloomer_add_recurring_row_email', 5, 2 );
function bbloomer_add_recurring_row_email( $total_rows, $myorder_obj ) {
global $woocommerce;
$frais = 1.01;
$price_excl_vat = wc_price( ( $woocommerce->cart->cart_contents_total * $frais ) + $woocommerce->cart->shipping_total );
$total_rows['recurr_not'] = array(
'label' => __( 'Total HT :', 'woocommerce' ),
'value' => $price_excl_vat
);

return $total_rows;
}

它不工作 on the Thank you page
但它在工作on email ...

有人可以向我解释为什么它适用于电子邮件但不适用于“感谢页面”吗?

最佳答案

更新:因为这个 Hook 是针对订单数据的,但不是购物车数据,所以你应该试试这个,我在最后一行之前设置了额外的行:

add_filter( 'woocommerce_get_order_item_totals', 'add_custom_order_totals_row', 30, 3 );
function add_custom_order_totals_row( $total_rows, $order, $tax_display ) {
$costs = 1.01;

// Set last total row in a variable and remove it.
$gran_total = $total_rows['order_total'];
unset( $total_rows['order_total'] );

// Insert a new row
$total_rows['recurr_not'] = array(
'label' => __( 'Total HT :', 'woocommerce' ),
'value' => wc_price( ( $order->get_total() - $order->get_total_tax() ) * $costs ),
);

// Set back last total row
$total_rows['order_total'] = $gran_total;

return $total_rows;
}

代码进入事件子主题(或事件主题)的 function.php 文件。 经过测试并有效

enter image description here


对于购物车,您应该使用此(因为不再需要 global $woocommerce;):

<?php 
$costs = 1.01;

echo '<tr class ="totalht">
<th>'. __( 'Total HT', 'woocommerce' ) .'</th>
<td data-title=" '. __( 'Total HT', 'woocommerce' ) .' ">'
. wc_price( ( WC()->cart->cart_contents_total * $costs ) + WC()->cart->shipping_total ) .'<span class="ht-panier">HT</span></td>
</tr>';
?>

关于php - 在 Woocommerce 谢谢页面和电子邮件的订单总计中添加一个新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49295493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com