下单后按付款方式更新订单总价
我创建了一种特定的付款方式来管理特定产品订单的批准或拒绝。
订单获得批准后,我将订单设置为“等待付款”状态,并向客户发送包含付款链接的电子邮件。
问题是,对于某些付款方式,我必须应用折扣。
我可以在下订单之前应用折扣(我将其用于不需要批准的订单),但我不此刻不知道如何重新计算总数。
为此,我使用以下代码:
add_action( 'woocommerce_cart_calculate_fees','payment_method_discount', 20, 1 );
function payment_method_discount( $cart_object ) {
if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
foreach($cart_object->cart_contents as $cart_item_id => $cart_item) {
$payment_method_arr = array('ppcp-gateway');
foreach($payment_method_arr as $payment_method) {
$percent = 25;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $payment_method == $chosen_payment_method ) {
WC()->cart->add_fee( 'PayPal fee', 0 - $discount );
}
}
}
}
触发我使用的更新:
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods', 20, 1 );
function refresh_payment_methods() {
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
我还尝试将函数refresh_ payment_methods添加到wp_footer挂钩。
I've created a specific method of payment to manage approval or decline of orders for specific products.
Once order was approved I set order in status "Waiting for payment" and send an email to customer with link to pay.
The problem is that for some payment method I have to apply a discount.
I'm able to apply discount before order was placed ( I use it for order that doesn't require approval), but I don't know how to recalcultate the total in this moment.
For do that I use this code:
add_action( 'woocommerce_cart_calculate_fees','payment_method_discount', 20, 1 );
function payment_method_discount( $cart_object ) {
if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
foreach($cart_object->cart_contents as $cart_item_id => $cart_item) {
$payment_method_arr = array('ppcp-gateway');
foreach($payment_method_arr as $payment_method) {
$percent = 25;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $payment_method == $chosen_payment_method ) {
WC()->cart->add_fee( 'PayPal fee', 0 - $discount );
}
}
}
}
To trigger the update I used:
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods', 20, 1 );
function refresh_payment_methods() {
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
I've tried also to add the function refresh_payment_methods to wp_footer hook.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论