【问题标题】:Change shipping class based on shipping class price items in cart in Woocommerce根据 Woocommerce 中购物车中的运输类别价格项目更改运输类别
【发布时间】:2020-04-27 21:55:26
【问题描述】:

当该运输类别的购物车中的总金额大于或等于 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');
        }
    }
}

我需要它而不是使用运输类别中的物品数量,而是使用购物车中的总额(以运输类别中的美元为单位)。谢谢

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:
    // 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');
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      相关资源
      最近更新 更多