我有一个自定义代码在wordpress网站,在那里我添加了一个费用,如果付款方式是现金的交付。我是通过woocommerce_cart_calculate_fees这样做的
add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee' );
function custom_handling_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$chosen_payment_id = WC()->session->get('chosen_payment_method');
if ( empty( $chosen_payment_id ) )
return;
$subtotal = $cart->subtotal;
// SETTINGS: Here set in the array the (payment Id) / (fee cost) pairs
$targeted_payment_ids = array(
'cod' => 1.5 // Fixed fee
);
// Loop through defined payment Ids array
foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
if ( $chosen_payment_id === $payment_id ) {
$cart->add_fee( __('Fee', 'woocommerce'), $fee_cost, false );
}
}
}但是,我的上述代码根本没有触发,以防
f 211
没有额外的费用从该页。但是当我从常规结帐页面运行这个过程时,就会增加费用。
发布于 2021-10-28 06:14:43
你上面的代码运行良好。我已经测试和工作,但为了增加收费基础上的支付网关,您必须触发update_checkout,以更新结帐的方法付款变化。
add_action( 'wp_footer', 'custom_script_for_add_fess_based_on_payment_gateway' );
function custom_script_for_add_fess_based_on_payment_gateway() {
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return;
?>
<script type="text/javascript">
(function($){
$('form.checkout').on('change', 'input[name="payment_method"]', function(){
$(document.body).trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}测试和工作

https://stackoverflow.com/questions/69744799
复制相似问题