【发布时间】:2020-01-03 16:28:00
【问题描述】:
我正在尝试根据产品类别为用户选择的运输方式添加额外费用(使用 $multiplier)。
所以,我需要检查购物车中的任何产品是否在 $product_category_id 上。 如果是这样,那么我需要将选定的运费乘以 $multiplier 值。
此 $multiplier 应应用于按送货方式计算的总运费(而不是购物车中的每件商品)。
add_filter( 'woocommerce_package_rates','overwrite_shipping_cost', 100, 2 );
function overwrite_shipping_cost($rates, $package ) {
global $woocommerce;
if ( get_field( 'multiplier', 'option' ) ) :
$multiplier = get_field( 'multiplier', 'option' );
else :
$multiplier = 1;
endif;
$product_category_id = array( 86, 21, 47 );
$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' );
$product_category_id_check = false;
foreach( $products as $cart_item ){
$product_id = $cart_item['product_id'];
}
foreach ( $product_category_id as $key => $value ) {
if ( in_array( $value, $product_cats_ids ) ) {
$product_category_id_check = true;
}
}
if ( ! is_admin() && $product_category_id_check ) {
// If Shiptor 1
if ( isset( $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal'] )) {
$rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost = $rates['shiptor-shiptor:14:delivery-point_shiptor_terminal']->cost * $multiplier;
}
// If Shiptor 2
if ( isset( $rates['shiptor-shiptor:14:to-door_shiptor_courier'] )) {
$rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost = $rates['shiptor-shiptor:14:to-door_shiptor_courier']->cost * $multiplier;
}
//If anothee shipping method
if ( isset( $rates['rpaefw_post_calc:16'] )) {
$rates['rpaefw_post_calc:16']->cost = $rates['rpaefw_post_calc:16']->cost * $multiplier;
}
}
return $rates;
}
执行代码后没有结果。我在代码中提供的运输方式没有应用 $multiplier。
【问题讨论】:
标签: woocommerce