WooCommerce COD 根据购物车金额收费

如果有人选择 COD 作为付款方式,我想收取额外费用。

假设如果购物车金额为 10 至 1000 INR,COD 费用将为 RS.50/-,如果购物车金额为 1001 至 2000 INR,则 COD 费用将为 RS.100-,依此类推。

我正在使用“在 Woocommerce 中为货到付款付款方式 (cod) 添加费用”答案代码,并且我需要更多自定义。


MMTTMM
浏览 123回答 1
1回答

一只甜甜圈

您可以使用 PHPswitch语句将购物车小计四舍五入到最接近的 1000 次方,然后以此为基础收取费用。例如,1001 到 2000(包括 2000)之间的任何内容的小计将四舍五入为 2000。这将由 switch 语句解释,并分配 100 的费用。switch 语句中的默认值将处理高于您添加到其中的任何情况的任何情况。// Add a custom fee based on cart subtotaladd_action( 'woocommerce_cart_calculate_fees', 'custom_cod_fee', 10, 1 );function custom_cod_fee ( $cart ) {&nbsp; &nbsp; if ( is_admin() && ! defined( 'DOING_AJAX' ) )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; if ( 'cod' === WC()->session->get('chosen_payment_method') ) {&nbsp; &nbsp; &nbsp; &nbsp; switch ( ceil( $cart->subtotal / 1000 ) * 1000&nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "1000": // 0 to 1000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fee = 50;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "2000": // 1001 to 2000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fee = 100;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "3000": // 2001 to 3000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fee = 150;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "4000": // 3001 to 4000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fee = 200;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "5000": // 4001 to 5000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fee = 250;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default: // anything higher, no fee&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fee = '';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ( !empty( $fee ) ) $cart->add_fee( 'COD Fee', $fee, true );&nbsp; &nbsp; }}显然,如果您更改支付网关,您将需要更新结账的代码:// jQuery - Update checkout on methode payment changeadd_action( 'wp_footer', 'custom_checkout_jqscript' );function custom_checkout_jqscript() {&nbsp; &nbsp; if ( is_checkout() && ! is_wc_endpoint_url() ) :&nbsp; &nbsp; ?>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; jQuery( function($){&nbsp; &nbsp; &nbsp; &nbsp; $('form.checkout').on('change', 'input[name="payment_method"]', function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(document.body).trigger('update_checkout');&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <?php&nbsp; &nbsp; endif;}
打开App,查看更多内容
随时随地看视频慕课网APP