亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據所選的支付網關和用戶角色添加費用

根據所選的支付網關和用戶角色添加費用

PHP
守著星空守著你 2023-09-22 14:58:37
我正在使用檢查 WooCommerce 用戶角色和支付網關,如果它們匹配 - 應用費用答案代碼,并且根據用戶角色添加費用,但是當客戶確認付款時,不收取費用。就我而言,支付網關是 MercadoPago,在我的國家,其功能類似于“Paypal”。事實是費用被忽略了。任何想法?
查看完整描述

1 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

也許您需要的是找出 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 文件中。

https://img3.sycdn.imooc.com/650d3b500001576005090278.jpg

找到您要查找的相關付款方式 ID 后,請刪除此代碼。


我重新訪問了檢查 WooCommerce 用戶角色和支付網關,如果它們匹配 - 應用費用答案代碼,處理多個支付 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 支付插件不接受費用。


查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 100 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號