skip to Main Content

In my Woocommerce store I want to calculate shipping charge based on product count and cart value eg if product count is more than 3 and cart value is less than 900 then shopping charge is 80. I want to do this type of calculations. Is it possible?

2

Answers


  1. Try this one

    add_filter('woocommerce_package_rates','shipping_charge_product_count_cart_value',100,2);
    function shipping_charge_product_count_cart_value($rates, $package) {
    
        global $woocommerce;
    
        // Get cart items
        $cart_items = $woocommerce->cart->get_cart();
        $count_for_apply_shipping_charge = 0;
    
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if($cart_item['quantity'] > 3 && $cart_item['data']->get_price() < 900){
                $count_for_apply_shipping_charge += 1;
            }
          
        }
    
        if($count_for_apply_shipping_charge >= 1) {
            foreach ($rates as $rate) {
    
                //Set the price
                $rate->cost = 80 * $count_for_apply_shipping_charge;
    
            }
    
        }
    
        return $rates;
    }
    
    Login or Signup to reply.
  2. Are you looking for something like that? Here I add an example for the Florida state of the USA

    if ( !empty($_POST['post_data'])  ) {
            parse_str($_POST['post_data'], $post_data);
            if ( !empty($post_data['shipping_state']) ) {
                $shipping_state = sanitize_text_field($post_data['shipping_state']);
                if ($shipping_state == 'FL') {
                    $cart->add_fee('Florida Shipping', 25.00);
                }
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search