当该运输类别的购物车中的总金额大于或等于150美元时,我需要删除该运输类别。我在此链接Change shipping class based on cart items shipping class count in Woocommerce
中找到了// Updating cart item price
add_action( 'woocommerce_before_calculate_totals', 'change_change_shipping_classes', 30, 1 );
function change_change_shipping_classes( $cart ) {
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
return;
// HERE define your shipping class SLUG
$mailbox_shipping_class = 'STOCK';
$mailbox_count = $item_price = $item_qty = 0;
$found = false;
foreach( WC()->cart->get_cart() as $cart_item ){
$item_shipping_class_id = $cart_item['data']->get_shipping_class_id();
if( in_array( $item_shipping_class_id, $class ) ){
$found = true; /* Target shipping class found */
$item_price += $cart_item['data']->get_price(); /* Sum line item prices that have target shipping class */
$item_qty += $cart_item['quantity']; /* Sum line item prices that have target shipping class */
$item_total = $item_price * $item_qty; /* Get total for all products with same shipping class (There might be a better way to get this total) */
}
}
if( $found ) {
if ( $item_total >=150) {
$cart_item['data']->set_shipping_class_id('0');
}
}
}
我需要而不是使用运输类别中的物品数量,而是使用推车中运输类别中的总计(以美元为单位)。谢谢
答案 0 :(得分:0)
// Updating cart item price
add_action( 'woocommerce_before_calculate_totals', 'change_change_shipping_classes', 30, 1 );
function change_change_shipping_classes( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
/* HERE define your shipping class to find */
$class = array(930);
/* Checking in cart items */
$found = false;
$item_price = $item_qty = 0;
foreach( WC()->cart->get_cart() as $cart_item ){
$item_shipping_class_id = $cart_item['data']->get_shipping_class_id();
if( in_array( $item_shipping_class_id, $class ) ){
$found = true; /* Target shipping class found */
$item_price += $cart_item['data']->get_price(); /* Sum line item prices that have target shipping class */
$item_qty += $cart_item['quantity']; /* Sum line item prices that have target shipping class */
$item_total = $item_price * $item_qty; /* Get total for all products with same shipping class (There might be a better way to get this total) */
}
}
if( $found ) {
if( $item_total >= 150 ) {
$cart_item['data']->set_shipping_class_id('932');
}
}
}