I have the code following placed in functions.php
function shipping_weight_fee( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $extraFee = 0; $total_weight = 0; // Loop though each cart item foreach ( $cart->get_cart() as $cart_item ) { // Get product id $product_id = $cart_item['product_id']; // Get weight $product_weight = $cart_item['data']->get_weight(); // NOT empty if ( ! empty( $product_weight ) ) { // Quantity $product_quantity = $cart_item['quantity']; // Add to total $total_weight += $product_weight * $product_quantity; } } if ( $total_weight > 10 ) { $extraFee = 20; } } add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 10, 1 ); How can I get $extraFee from first code snippet to work in following code: I tried with global but it does not work. I believe the second code runs first but I am not sure.
add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 20, 2 ); function custom_shipping_costs( $rates, $package ) { foreach( $rates as $rate_key => $rate ){ // Excluding free shipping methods if( $rate->method_id != 'free_shipping'){ // Set rate cost $rates[$rate_key]->cost += $extraFee + 3; } } return $rates; } Please help, thank You!
https://stackoverflow.com/questions/66511114/add-an-extra-cost-based-on-cart-items-weight-to-woocommerce-shipping-rates March 07, 2021 at 06:06AM
没有评论:
发表评论