Php 为WooCommerce中选定的特定付款方式添加折扣
如果没有使用优惠券功能,我想为一种特定的付款方式(如“xyz”)申请15%的折扣 我想帮助确定要使用哪些挂钩。我希望达到的总体目标是:Php 为WooCommerce中选定的特定付款方式添加折扣,php,wordpress,woocommerce,checkout,discount,Php,Wordpress,Woocommerce,Checkout,Discount,如果没有使用优惠券功能,我想为一种特定的付款方式(如“xyz”)申请15%的折扣 我想帮助确定要使用哪些挂钩。我希望达到的总体目标是: if payment_method_hook == 'xyz'{ cart_subtotal = cart_subtotal - 15% } 客户不需要在此页面上查看折扣。我希望仅针对特定的付款方式正确提交折扣。您可以使用woocommerce\u cart\u calculate\u feesaction hook中的此自定义功能,该功能将为定义的付
if payment_method_hook == 'xyz'{
cart_subtotal = cart_subtotal - 15%
}
客户不需要在此页面上查看折扣。我希望仅针对特定的付款方式正确提交折扣。您可以使用
woocommerce\u cart\u calculate\u fees
action hook中的此自定义功能,该功能将为定义的付款方式提供15%的折扣
您需要在此函数中设置您的实际付款方式ID(如“bacs”、“cod”、“支票”或“paypal”)
第二个功能将在每次选择付款方式时刷新结帐数据
守则:
add_action( 'woocommerce_cart_calculate_fees','shipping_method_discount', 20, 1 );
function shipping_method_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// HERE Define your targeted shipping method ID
$payment_method = 'bacs';
// The percent to apply
$percent = 15; // 15%
$cart_total = $cart_object->subtotal_ex_tax;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $payment_method == $chosen_payment_method ){
$label_text = __( "Shipping discount 15%" );
// Calculation
$discount = number_format(($cart_total / 100) * $percent, 2);
// Add the discount
$cart_object->add_fee( $label_text, -$discount, false );
}
}
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
// jQuery code
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
add_action('woocommerce_cart_计算_费用','shipping_方法_折扣',20,1);
函数装运方法折扣($cart\u对象){
if(is_admin()&&!defined('DOING_AJAX'))返回;
//这里定义您的目标配送方式ID
$payment_method='bacs';
//要应用的百分比
$percent=15;//15%
$cart\u total=$cart\u object->subtotal\u exu tax;
$selected_payment_method=WC()->session->get('selected_payment_method');
如果($payment\u method==$selected\u payment\u method){
$label_text=uuuuuuuu(“运费折扣15%”);
//算计
$折扣=数量\格式(($cart\总计/100)*$percent,2);
//加上折扣
$cart\u object->add\u fee($label\u text,-$折扣,false);
}
}
添加操作(“付款前审查订单”、“刷新付款方法”);
函数刷新\付款\方法(){
//jQuery代码
?>
(函数($){
$('form.checkout')。在('change','input[name^=“payment_method”]”上,函数(){
$('body')。触发器('update_checkout');
});
})(jQuery);
这可能会帮助其他人。我需要检查2种方法的付款方式,并检查用户是否是特定角色
if (($chosen_payment_method == 'stripe' || $chosen_payment_method == 'paypal') && current_user_can('dealer')) {
美丽,正是我想要的。谢谢你,洛伊克!