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.