下单后按付款方式更新订单总价

发布于 2025-01-21 04:02:31 字数 1446 浏览 0 评论 0原文

我创建了一种特定的付款方式来管理特定产品订单的批准或拒绝。

订单获得批准后,我将订单设置为“等待付款”状态,并向客户发送包含付款链接的电子邮件。

问题是,对于某些付款方式,我必须应用折扣。

我可以在下订单之前应用折扣(我将其用于不需要批准的订单),但我不此刻不知道如何重新计算总数。

为此,我使用以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文