【问题标题】:Cart total Weight and Shipping Recalculation on WooCommerceWooCommerce 上的购物车总重量和运费重新计算
【发布时间】:2021-05-06 01:13:08
【问题描述】:

我正在尝试使用 sn-p 将我的自定义箱子重量添加到订单总重量中,并且它目前工作正常。但是,不会根据重新计算的 sn-p 重量重新计算运输方式。任何想法如何强制重新计算运费?

 add_filter( 'woocommerce_cart_contents_weight', 'filter_wc_cart_contents_weight', 10000 );
function filter_wc_cart_contents_weight( $weight ) {
    //box testing sizes (kg)
    $Envelope = 2;
    $Small_parcel = 10;
    $Medium_parcel = 50;

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        $total_height += $cart_item['data']->get_height() * $cart_item['quantity'];
        }
    if( $total_height <= 8)  {
            $weight += $Envelope;
    }
    if( $total_height >= 8 && $total_height <=61)  {
            $weight += $Small_parcel;
    }
    return $weight;
}


//show calculated weight at checkout

add_action( 'woocommerce_cart_totals_after_order_total', 'display_wc_cart_total_weight_after_order_total' );
function display_wc_cart_total_weight_after_order_total() {
    ?>
    <tr class="order-total">
        <th><?php esc_html_e( 'Total weight', 'woocommerce' ); ?></th>
        <td data-title="<?php esc_attr_e( 'Total weight', 'woocommerce' ); ?>"><?php echo wc_format_weight( WC()->cart->get_cart_contents_weight() ); ?></td>
    </tr>
    <?php
}

【问题讨论】:

    标签: php woocommerce


    【解决方案1】:

    我终于找到了办法:

    以下方法允许在结账时添加包裹重量 将要使用的包裹的重量除以购物车物品。这样,购物车的总重量也包括箱子重量,并且运输选项可以反映订单总重量的正确价格。

        //Case scenario for selling books
        
        add_action( 'woocommerce_before_calculate_totals', 'add_custom_weight', 10, 1 );
        
        function add_custom_weight( $cart ) {
            
            if ( is_admin() && ! defined( 'DOING_AJAX' ) )
                return;
            if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
                return;
            
            //box sizes (kg)
            $Small_envelope = 0.095;
            $Medium_envelope = 0.11;
            $Large_envelope = 0.2;
            $Small_box = 0.4;
            $Medium_box = 0.6;
            $DHLno5 = 0.9;
            $DHLno6 = 1.5;
            
        // Loop through cart items and get total height and max width 
        // In my case, max width is needed in case there is a book in the cart larger than the normal A5 size
          
            foreach(WC()->cart->get_cart() as $cart_item ) {
                    $total_height += $cart_item['data']->get_height() * $cart_item['quantity'];
                    $temp_width = $cart_item['data']->get_width();
                    if ($temp_width > $max_width){
                        $max_width = $temp_width;
                    }
                $cart_items_qty += $cart_item['quantity'];
            }
            
        //Check which box should be used based on total order height
            if( $total_height <= 2.7)  {
                    $extra_weight = $Small_envelope; // Consider also envelope height and wrapping padding
                } 
            if( $total_height > 2.7 && $total_height <=8)  {
                    $extra_weight = $Medium_envelope;
                }
            if( $total_height > 8 && $total_height <=16 && $max_width < 19)  {
                    $extra_weight = $Large_envelope;
                }
        // If a box can fit two columns of books, then the box can fit contents double it's normal height
        // I have an extra check here in case there was a larger book in width than 19cm, 
        // to use rather a small box for the order instead of a Large envelope
            if( $total_height > 8 && $total_height <=16 && $max_width > 19) {
                $extra_weight = $Small_box;
            }
            if( $total_height > 16 && $total_height <=22)  {
                    $extra_weight = $Small_box;
                }
            if( $total_height > 22 && $total_height <=38)  {
                    $extra_weight = $Medium_box;
                }
            if( $total_height > 38 && $total_height <=60)  {
                $extra_weight = $DHLno5;
                }
            if( $total_height > 60 )  {
                $extra_weight = $DHLno6;
                }
            
        //I divide the $extra_weight (box weight), by the number of cart items
        //Then loop through cart items again to add the divided weight to each item.
            $xtr_weight_per_item = $extra_weight / $cart_items_qty;
            foreach ( $cart->get_cart() as $cart_item ) {
                $cart_item['data']->set_weight( $cart_item['data']->get_weight()+$xtr_weight_per_item);
            }
        }
    //show calculated weight at checkout for testing
    add_action( 'woocommerce_cart_totals_after_order_total', 'display_wc_cart_total_weight_after_order_total' );
    function display_wc_cart_total_weight_after_order_total() {
        ?>
        <tr class="order-total">
            <th><?php esc_html_e( 'Total weight', 'woocommerce' ); ?></th>
            <td data-title="<?php esc_attr_e( 'Total weight', 'woocommerce' ); ?>"><?php echo wc_format_weight( WC()->cart->get_cart_contents_weight() ); ?></td>
        </tr>
        <?php
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-23
      • 2019-07-27
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 2023-03-13
      • 2018-02-05
      • 1970-01-01
      相关资源
      最近更新 更多