第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

增加 WooCommerce 購物車中每個類別計數的運輸成本

增加 WooCommerce 購物車中每個類別計數的運輸成本

PHP
瀟瀟雨雨 2023-09-22 17:33:50
我正在嘗試根據購物車中的類別數量(計數)來增加運費。到目前為止我有這個:add_filter( 'woocommerce_package_rates', 'vh_wc_shipping_costs', 20 );function vh_wc_shipping_costs( $rates ) {    $increase_cost = 50;    $cat_ids       = array();    $count         = 0;    foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {        $cat_ids = array_merge(            $cat_ids,            (array) $cart_item['data']->get_category_ids()        );        $count   = count( $cat_ids );    }    $i=0;    if ( $count >= 1 ) {        foreach ( $rates as $rate_key => $rate ) {            $i++; $i <= $count;            // Excluding free shipping methods            if ( 'free_shipping' !== $rate->method_id ) {                // Get old cost                $old_cost = $rates[ $rate_key ]->cost;                // Set rate cost                $rates[ $rate_key ]->cost = $old_cost + $increase_cost;            }        }    }    return $rates;}但由于某種原因,它不會增加每個額外類別的運輸成本。另外,我想補充一下額外費用。任何建議表示贊賞。
查看完整描述

1 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊


以下代碼將為購物車中找到的每個附加產品類別添加額外的運費(因此不適用于第一個):


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

function filter_shipping_rates_costs( $rates, $package ) {

? ? $step_cost = 50;

? ? $term_ids? = array();


? ? // Loop through cart items for the current shipping package

? ? foreach( $package['contents'] as $cart_item ){

? ? ? ? $term_ids = array_merge(

? ? ? ? ? ? $term_ids,

? ? ? ? ? ? (array) $cart_item['data']->get_category_ids()

? ? ? ? );

? ? }


? ? $terms_count = count( $term_ids );


? ? // Loop through shipping rates

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

? ? ? ? // Excluding free shipping methods

? ? ? ? if ( 'free_shipping' !== $rate->method_id && $terms_count > 1 ) {

? ? ? ? ? ? // Set rate cost

? ? ? ? ? ? $rates[$rate_key]->cost = $rate->cost + ($step_cost * ($terms_count - 1));

? ? ? ? }

? ? }


? ? return $rates;

}

現在,如果您想為購物車中找到的每個類別添加額外的運費,請使用以下命令:


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

function filter_shipping_rates_costs( $rates, $package ) {

? ? $step_cost = 50;

? ? $term_ids? = array();


? ? // Loop through cart items for the current shipping package

? ? foreach( $package['contents'] as $cart_item ){

? ? ? ? $term_ids = array_merge(

? ? ? ? ? ? $term_ids,

? ? ? ? ? ? (array) $cart_item['data']->get_category_ids()

? ? ? ? );

? ? }


? ? $terms_count = count( $term_ids );


? ? // Loop through shipping rates

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

? ? ? ? // Excluding free shipping methods

? ? ? ? if ( 'free_shipping' !== $rate->method_id && $terms_count > 0 ) {

? ? ? ? ? ? // Set rate cost

? ? ? ? ? ? $rates[$rate_key]->cost = $rate->cost + ($step_cost * $terms_count);

? ? ? ? }

? ? }


? ? return $rates;

}

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

刷新運輸緩存:

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

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

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


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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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