在 Woocommerce 中,如果购物车项目具有分配给相关产品的特定运输类别,我会尝试添加运费。我希望将此运费乘以购物车商品数量...</p>
当将产品添加到购物车并且数量增加并且额外的运费也增加时,我可以使用此功能。但是,如果我添加其他具有相同运输等级的产品并增加数量,则额外费用不会增加。
这是我的代码:
// Add additional fees based on shipping class
function woocommerce_fee_based_on_shipping_class( $cart_object ) {
global $woocommerce;
// Setup an array of shipping classes which correspond to those created in Woocommerce
$shippingclass_dry_ice_array = array( 'dry-ice-shipping' );
$dry_ice_shipping_fee = 70;
// then we loop through the cart, checking the shipping classes
foreach ( $cart_object->cart_contents as $key => $value ) {
$shipping_class = get_the_terms( $value['product_id'], 'product_shipping_class' );
$quantity = $value['quantity'];
if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_dry_ice_array ) ) {
$woocommerce->cart->add_fee( __('Dry Ice Shipping Fee', 'woocommerce'), $quantity * $dry_ice_shipping_fee ); // each of these adds the appropriate fee
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_fee_based_on_shipping_class' ); // make it all happen when Woocommerce tallies up the fees
我怎样才能使它也适用于其他购物车项目?