WooCommerce 上的购物车总重量和运费重新计算

时间:2021-05-06 01:13:08

标签: php woocommerce

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

 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
}

0 个答案:

没有答案