Woocommerce

How To Add Shipping Fees To Product Price Dynamically While Adding To Cart

In this article, we will learn to add shipping fees to product prices dynamically while adding to cart.

For Adding custom shipping fees to product price simply add woocommerce ‘woocommerce_before_calculate_totals’ hook in your child theme’s fuction.php file.

Note: Please don’t add code to the parent theme’s fuction.php file. cause when you update the parent theme this file will be wiped entirely.

Place below code to your fuction.php.

function calculate_shipping_fees( $cart_price ) {
        $shipping_fee = 10;
        foreach ( $cart_price->cart_contents as $key => $value ) {

                $product_price =  $value['data']->get_price();
                $total_price = $product_price + $shipping_fee;
                $value['data']->set_price($total_price);
            }
    }
add_action( 'woocommerce_before_calculate_totals', 'calculate_shipping_fees');

Now, when you add the product to the cart you will see the price difference.

your cart price will be different from the product price.


You can add any extra fees and tax on product price while adding to the cart by adding the ‘woocommerce_before_calculate_totals’ hook.

Here, we can go with our custom fees to the product price.

Also, Check For Add custom currency with a symbol in woocommerce.

Fenal Kalathiya

Junior WordPress Developer. Good skills in WordPress, PHP, HTML, CSS, and jQuery.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago