【问题标题】:Add a fee based on chosen payment gateways and user roles根据选择的支付网关和用户角色添加费用
【发布时间】:2020-08-07 13:49:11
【问题描述】:

我用的是Check WooCommerce User Role and Payment Gateway and if they match - Apply fee应答码,按用户角色加费用ok,但是当客户确认付款时,不收取费用。在我的例子中,支付网关是 MercadoPago,在我的国家,类似“Paypal”。

事实是费用被忽略了。有什么想法吗?

【问题讨论】:

  • 欢迎来到stackoverflow。请更具体地说明您的问题。描述你的问题,你尝试了什么,什么有效,什么无效。请不要被冒犯并阅读:stackoverflow.com/help/how-to-ask

标签: php wordpress woocommerce payment-method fee


【解决方案1】:

也许您需要找出为 mercado pago 启用的付款方式 ID。

以下代码将在结帐页面上为管理员显示付款 ID,以便在付款方式标题附近结帐可用的付款方式:

add_filter( 'woocommerce_gateway_title', 'display_payment_method_id_for_admins_on_checkout', 100, 2 );
function display_payment_method_id_for_admins_on_checkout( $title, $payment_id ){
    if( is_checkout() && ( current_user_can( 'administrator') || current_user_can( 'shop_manager') ) ) {
        $title .= ' <code style="border:solid 1px #ccc;padding:2px 5px;color:red;">' . $payment_id . '</code>';
    }
    return $title;
}

代码进入您的活动子主题(或活动主题)的functions.php 文件中。

找到您要查找的相关付款方式 ID 后,删除此代码。


我已经重新访问了Check WooCommerce User Role and Payment Gateway and if they match - Apply fee答案代码,处理多个付款ID

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

    if ( ! is_user_logged_in() )
        return;

    ## SETTINGS:
    $user_roles  = array( 'vendor', 'administrator' ); // Defined  user roles
    // Targeted payment method Ids
    $payment_ids = array('cod', 'paypal'); 
    $percentage  = 5; // Fee percentage

    $user           = wp_get_current_user();  // Get current WP_User Object
    $chosen_payment = WC()->session->get('chosen_payment_method'); // Choosen payment method
    $cart_subtotal  = $cart->subtotal; // Including taxes - to exclude taxes use $cart->get_subtotal()

    // For matched payment Ids and user roles
    if ( in_array( $chosen_payment, $payment_ids ) && array_intersect( $user->roles, $user_roles ) ) { 
        $fee_cost = $percentage * $cart_subtotal / 100;  // Calculation
        $cart->add_fee( __('Payment Fee'), $fee_cost, true ); // Add fee (incl taxes)
    }
}

现在缺少一些东西:以下将刷新付款方式选择的结帐:

// jQuery - Update checkout on payment method change
add_action( 'wp_footer', 'custom_checkout_jquery_script' );
function custom_checkout_jquery_script() {
    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;
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

显然 Mercado pago 支付插件不接受费用。

类似:Add fee based on specific payment methods in WooCommerce

【讨论】:

  • Loic,非常感谢您回答我的问题。我测试了代码,费用计算正确,添加到小计并在结帐时显示,但是 Mercadopago 不收取费用金额,它只收取小计不添加费用,
  • @MarceloGarcía 这不正常,然后是有什么东西在制造麻烦,它可能是一个插件,你的主题的自定义或你制作的其他代码......
  • 再次感谢您的帮助,经过一番研究我发现这是 MercadoPago 代码的问题,不幸的是它不允许额外费用...
  • 无论如何,由于此代码适用于任何其他付款方式,请accept回答,谢谢。
猜你喜欢
  • 2013-03-02
  • 2015-08-26
  • 2020-09-16
  • 2020-09-27
  • 2016-10-07
  • 2021-11-16
  • 2018-11-22
  • 2019-01-29
  • 1970-01-01
相关资源
最近更新 更多