簡體   English   中英

根據 WooCommerce 中的付款方式和購物車項目總計添加費用

[英]Add fee based on payment methods and cart items total in WooCommerce

我安裝了一個插件->“WooCommerce 的基於支付網關的費用和折扣”,它幫助我添加了兩項費用:

  • 卡支付 14,99 送貨費

  • 貨到付款 19,99 運費

問題是如果有人購買超過 300 件,我想免費送貨。 所以我必須取消額外費用這是我嘗試過的,但沒有任何反應:

function woo_remove_cart_fee() {

  $cart_items_total = WC()->cart->get_cart_contents_total();

    if ( $cart_items_total > 300 ) {
        $fees = 0 ;     
   } 

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_remove_fee' );

有任何想法嗎? 或者關於如何讓網關費用和免費送貨超過限制的任何想法?

謝謝。

您不能刪除插件根據購物車項目總安裝量添加的費用。

由於您的插件不處理最小或最大購物車金額條件,請先從中禁用費用(或禁用插件)並改用以下內容:

add_action( 'woocommerce_cart_calculate_fees', 'fee_based_on_payment_method_and_total' );
function fee_based_on_payment_method_and_total( $cart ) {
    if ( is_admin() && ! defined('DOING_AJAX') )
        return;
        
    $threshold_amount  = 300; // Total amount to reach for no fees
    
    $payment_method_id = WC()->session->get('chosen_payment_method');
    $cart_items_total  = $cart->get_cart_contents_total();

    if ( $cart_items_total < $threshold_amount ) {
        // For cash on delivery "COD"
        if ( $payment_method_id === 'cod' ) {
            $fee = 14.99;
            $text = __("Fee");
        } 
        // For credit cards (other payment gateways than "COD", "BACS" or "CHEQUE"
        elseif ( ! in_array( $payment_method_id, ['bacs', 'cheque'] ) ) {
            $fee = 19.99;
            $text = __("Fee");
        }
    }
    
    if( isset($fee) && $fee > 0 ) {
        $cart->add_fee( $text, $fee, false ); // To make fee taxable change "false" to "true"
    }
} 

以下代碼用於刷新付款方式更改的數據:

// 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文件。 測試和工作。

相關: 根據WooCommerce中的具體支付方式加費

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM