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

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

for循環中的PHP循環

for循環中的PHP循環

PHP
桃花長相依 2022-11-04 16:51:25
幾個小時前我發布了這段代碼。我設法解決了其中一個問題,但我現在只有一個問題。此代碼運行良好,但我需要使用特定運輸類別將每件商品的成本相乘,然后將其添加到常規運輸成本中。例如,如果我在購物車中有 5 個產品:其中 2 個使用 shipping-class-1(產品額外運費 70 美元)其中 3 個使用 shipping-class-2(產品額外運費 50 美元)因此,如果(例如)常規運費為 120 美元,則運費應為(120 美元 + 290 美元)= 410 美元add_filter( 'woocommerce_package_rates', 'ph_add_extra_cost_based_on_shipping_class', 10, 2);if( ! function_exists('ph_add_extra_cost_based_on_shipping_class') ) {    function ph_add_extra_cost_based_on_shipping_class( $shipping_rates, $package ){        $handling_fee_based_on_shipping_class = array(            array(                'shipping_classes'  =>  array( 'shipping-class-1'),         // Shipping Class slug array                'adjustment'        => 70,                                  // Adjustment            ),            array(                'shipping_classes'  =>  array( 'shipping-class-2' ),                'adjustment'        =>  50,            ),        );        $shipping_method_ids = array( 'cologistics-shipping' );     // Shipping methods on which adjustment has to be applied        $adjustment = null;        foreach( $package['contents'] as  $line_item ) {            $line_item_shipping_class = $line_item['data']->get_shipping_class();            if( ! empty($line_item_shipping_class) ) {                foreach( $handling_fee_based_on_shipping_class as $adjustment_data ) {                    if( in_array( $line_item_shipping_class, $adjustment_data['shipping_classes']) ) {                        $adjustment = ( $adjustment_data['adjustment'] > $adjustment ) ? $adjustment_data['adjustment'] : $adjustment;                    }                }            }        }我將非常感謝您的幫助。
查看完整描述

1 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

這通常不需要任何代碼......您應該將您的運輸方式設置如下(所以首先刪除您的代碼并保存)

http://img1.sycdn.imooc.com//6364d4a30001a2de10250624.jpg

1) 因此,您將120在運輸方式中保留全球成本。

2)然后對于每個相關的所需運輸類別,您將在相應的字段中添加:

  • [qty]*70為了shipping-class-1

  • [qty]*50為了shipping-class-2

  • 計算類型: Per class: Charge shipping for each shipping class individually

這將起作用,并將通過根據購物車物品數量減少等級來增加成本。


對于基于您的代碼的自定義運輸方式,您應該使用以下方式處理商品數量:

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

if( ! function_exists('ph_add_extra_cost_based_on_shipping_class') ) {

    function ph_add_extra_cost_based_on_shipping_class( $shipping_rates, $package ){


        $handling_fee_based_on_shipping_class = array(

            array(

                'shipping_classes'  =>  array( 'shipping-class-1'),         // Shipping Class slug array

                'adjustment'        => 70,                                  // Adjustment

            ),

            array(

                'shipping_classes'  =>  array( 'shipping-class-2' ),

                'adjustment'        =>  50,

            ),

        );


        $shipping_method_ids = array( 'cologistics-shipping' );     // Shipping methods on which adjustment has to be applied


        $adjustment = 0;


        foreach( $package['contents'] as  $line_item ) {

            $line_item_shipping_class = $line_item['data']->get_shipping_class();

            if( ! empty($line_item_shipping_class) ) {

                foreach( $handling_fee_based_on_shipping_class as $adjustment_data ) {

                    if( in_array( $line_item_shipping_class, $adjustment_data['shipping_classes']) ) {

                        $adjustment += $adjustment_data['adjustment'] * $line_item['quantity'];

                    }

                }

            }

        }


        if( $adjustment > 0 ) {

            foreach( $shipping_rates as $shipping_rate ) {

                $shipping_method_id = $shipping_rate->get_method_id();

                if( in_array($shipping_method_id, $shipping_method_ids) ) {

                    $shipping_rate->set_cost( (float) $shipping_rate->get_cost() + $adjustment );

                }

            }

        }


        return $shipping_rates;

    }

}

它應該可以處理汽車項目數量的額外成本......


不要忘記在運輸設置中刷新運輸方式,然后禁用/保存并重新啟用/保存相關運輸區域中的任何運輸方式。


其他與鉤子相關的答案線程。woocommerce_package_rates


查看完整回答
反對 回復 2022-11-04
  • 1 回答
  • 0 關注
  • 134 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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