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

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

基于 WooCommerce 中購物車小計百分比的累進統一費率

基于 WooCommerce 中購物車小計百分比的累進統一費率

PHP
長風秋雁 2023-09-30 15:58:43
在 WooCommerce 中,我嘗試根據購物車小計的 10% 設置漸進運輸成本,最低成本為 5 美元,最高成本為 11 美元。這是我的代碼嘗試:add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates', 10, 2 );function woocommerce_package_rates( $rates, $package ) {    // Make sure flat rate is available    if ( isset( $rates['flat_rate:24'] ) ) {        // Set the cost to $5        $rates['flat_rate:24']->cost = 5;    }    $cart_subtotal = $WC()->cart->subtotal    if ($cart_subtotal >50)    $percentage = 0.10;  // Percentage (10%) in float    $percentage_fee = ( WC()->cart->subtotal >+ WC()->cart->get_shipping_total()) * $percentage;    }     });    return $rates;}此代碼在網站上給出了嚴重錯誤。我對 WordPress 自定義編碼很陌生,您可能會從下面的代碼中看出。另外,我希望顯示的標簽顯示“USPS”而不是“統一費率”。例如,如果購物車小計為 60 美元,則他們需要支付 6 美元的統一運費(小計的 10%)。我錯過了什么或做錯了什么?
查看完整描述

1 回答

?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

您提供的代碼中有很多錯誤...以下內容將允許您根據購物車小計百分比設置從5$到最大值的運費。11$

首先,您需要在統一費率設置中設置成本5(以及“UPS”作為標簽)。

https://img1.sycdn.imooc.com//6517d56b0001964006470204.jpg

然后使用這段代碼:


add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates', 10, 2 );

function woocommerce_package_rates( $rates, $package ) {

    $max_cost   = 11; // Here set the max cost for the shipping method

    $percentage = 10; // Percentage to apply on cart subtotal

    $subtotal   = WC()->cart->get_subtotal(); // Cart subtotal without taxes


    // Loop through shipping rates

    foreach ( $rates as $rate_key => $rate ) {

        // Targetting specific flate rate shipping method

        if ( 'flat_rate:14' === $rate_key ) {

            $has_taxes = false;

            $base_cost = $rate->cost; // 5$ from this shipping method cost setting

            $new_cost  = $subtotal * $percentage / 100; // Calculation


            if( $new_cost > $base_cost ) {

                // 1. Rate cost

                if ( $new_cost < $max_cost ) {

                    $rates[$rate_key]->cost = $new_cost;

                    $rate_operand = $new_cost / $base_cost; // (for taxes if enabled)

                } else {

                    $rates[$rate_key]->cost = $max_cost;

                    $rate_operand = $max_cost / $base_cost; // (for taxes if enabled)

                }

                // 2. Taxes rate cost (if enabled)

                foreach ($rates[$rate_key]->taxes as $key => $tax){

                    if( $tax > 0 ){

                        // New tax calculated cost

                        $taxes[$key] = $tax * $rate_operand;

                        $has_taxes = true;

                    }

                }

                // Set new taxes cost

                if( $has_taxes ) {

                    $rates[$rate_key]->taxes = $taxes;

                }

            }

        }

    }

    return $rates;

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。

刷新運輸緩存:

  1. 此代碼已保存在您的functions.php 文件中。

  2. 在運輸區域設置中,禁用/保存任何運輸方式,然后啟用返回/保存。

    你已經完成了,你可以測試它。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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