【问题标题】:Add a custom fee for percentage and fixed cost to specific payment gateway in Woocommerce为 Woocommerce 中的特定支付网关添加百分比和固定成本的自定义费用
【发布时间】:2020-06-01 19:04:31
【问题描述】:

在 WooCommerce 中,我需要为特定支付网关应用自定义手续费。

百分比成本的自定义手续费 以及按固定成本进行的自定义处理。

我有这些代码来自这里:


百分比成本 - 函数

/************************************************************/
/*  PERCENTACE COST 
**/
// Add a custom fee based o cart subtotal
// Add a custom fee based o cart subtotal

add_action( 'woocommerce_cart_calculate_fees', 'custom_percentage_fee', 20, 1 );
function custom_percentage_fee ( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
        return; // Only checkout page

    $payment_method = WC()->session->get( 'chosen_payment_method' );

    if ( 'cod' == $payment_method ) {
        $surcharge = $cart->subtotal * 0.025;
        $cart->add_fee( 'Percentage Cost', $surcharge, true );
    }
}

// jQuery - Update checkout on methode payment change  
add_action( 'wp_footer', 'custom_checkout_jqscript' );
function custom_checkout_jqscript() {
    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
        return; // Only checkout page
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('form.checkout').on('change', 'input[name="payment_method"]', function(){
            $(document.body).trigger('update_checkout');
        });
    });
    </script>
    <?php
}

结果前端





固定成本 - 功能

/************************************************************/
/*  FIXED COST 
**/
// Add a custom fee based o cart subtotal
/*
add_action( 'woocommerce_cart_calculate_fees', 'custom_fixed_fee', 10, 1 );
function custom_fixed_fee ( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( 'cod' === WC()->session->get('chosen_payment_method') ) {
        $fee = 0.31;
        $cart->add_fee( 'Fixed Cost', $fee, true );
    }
}

// jQuery - Update checkout on methode payment change
add_action( 'wp_footer', 'custom_checkout_jqscript' );
function custom_checkout_jqscript() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('form.checkout').on('change', 'input[name="payment_method"]', function(){
            $(document.body).trigger('update_checkout');
        });
    });
    </script>
    <?php
    endif;
}

结果前端





这两个不同的功能可以完美地分开工作。

现在,我想将这两个函数结合起来得到两个成本的总和。

类似这样的:



提前感谢您的帮助!



添加其他支付系统的功能演变:

add_action( 'woocommerce_cart_calculate_fees', 'custom_percentage_fee', 20, 1 );
function custom_percentage_fee ( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

     if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
        return; // Only checkout page

    $payment_method = WC()->session->get( 'chosen_payment_method' );

     if ( 'stripe' == $payment_method ) {
        $surcharge = $cart->subtotal * 0.025;
	    $fee = 0.31;
	} 
     if ( 'paypal_credit_card_rest' == $payment_method ) {
        $surcharge = $cart->subtotal * 0.036;
	    $fee = 0.35;
	}
     if ( 'paypal_express' == $payment_method ) {
        $surcharge = $cart->subtotal * 0.036;
	    $fee = 0.35;
	}
     if ( 'bacs' == $payment_method ) {
        return;
	}
  
      $together = $surcharge + $fee;
      $cart->add_fee( 'Plus Cost', $together, true );
}


// jQuery - Update checkout on methode payment change  
add_action( 'wp_footer', 'custom_checkout_jqscript' );
function custom_checkout_jqscript() {
    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
        return; // Only checkout page
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('form.checkout').on('change', 'input[name="payment_method"]', function(){
            $(document.body).trigger('update_checkout');
        });
    });
    </script>
    <?php
}

函数返回以下错误:

注意:未定义变量:/MY-FOLDER..../sn-p-ops.php(446) 中的附加费:第 79 行的 eval() 代码

注意:未定义变量:/MY FOLDER..../sn-p-ops.php(446) 中的费用:第 79 行的 eval() 代码

就在这里:

  • $together = $surcharge + $fee;


结果前端


我的代码肯定是错的......

提前致谢。

【问题讨论】:

  • 如果满足 if 条件,将设置变量。但是,如果不是这种情况,您将收到错误消息“未定义的变量”。您可以通过使用 if / elseif / else 来解决这个问题,其中变量是在 else 条件中设置的。您还可以使用 php isset 函数或为 if 条件设置具有标准值的变量。

标签: php wordpress woocommerce payment-gateway hook-woocommerce


【解决方案1】:

这取决于我还是您发布的两个示例之间的差异真的很小?你只是这个意思还是我完全错了?

if ( 'cod' == $payment_method ) {
    $surcharge = $cart->subtotal * 0.025;
    $fee = 0.31;

    $together = $surcharge + $fee;
    $cart->add_fee( 'Plus Cost', $together, true );
}

【讨论】:

  • 好的,L7c1f3r 先生,谢谢,您的功能有效。我有问题,我很快就会尝试向他们展示我的进化。
猜你喜欢
  • 2018-11-15
  • 1970-01-01
  • 2018-09-24
  • 2017-03-02
  • 2017-08-05
  • 2014-06-06
  • 1970-01-01
  • 2016-08-20
  • 2023-03-03
相关资源
最近更新 更多