简体   繁体   中英

Change shipping class based on shipping class price items in cart in Woocommerce

I need to delete the shipping class when the total in the cart of that shipping class is greater than or equal to 150 dollars. I have found in this link 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');
        }
    }
}

I need that instead of using the quantity of items from the shipping class I use the total in the cart in dollars from the shipping class. Thank you

// 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');
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM