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 文件中。經過測試并有效。
刷新運輸緩存:
此代碼已保存在您的functions.php 文件中。
在運輸區(qū)域設置中,禁用/保存任何運輸方式,然后啟用返回/保存。
你已經完成了,你可以測試它。
- 1 回答
- 0 關注
- 102 瀏覽
添加回答
舉報